Creating a graphics program
Download
1 / 27

Creating a graphics program - PowerPoint PPT Presentation


  • 264 Views
  • Updated On :

Creating a graphics program Color and Shapes Import libraries import javax.swing.*; // for JFrame import java.awt.*; // for Color and Graphics Program Framework public class myGrPgm extends JFrame { // extending JFrame gives us: // - windows // - paint

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Creating a graphics program' - jacob


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Creating a graphics program l.jpg

Creating a graphics program

Color and Shapes


Import libraries l.jpg
Import libraries

import javax.swing.*; // for JFrame

import java.awt.*; // for Color and Graphics


Program framework l.jpg
Program Framework

public class myGrPgmextends JFrame

{

// extending JFrame gives us:

// - windows

// - paint

// - use of Color

}// end class myGrPgm


Grab a color l.jpg
grab a color

public Color palette

= new Color ( 25, 120, 200 );

// “palette” is a Color object

// Color (red,green, blue);

note:

25, 120, 200 is ocean blue



A clarification l.jpg
a clarification

Color boxColor = Color.blue

or

Color boxColor = new Color( );

boxColor = Color.blue;object t to box Color.


Slide7 l.jpg

Color class in Computer Memory Space

Color boxColor = Color.blue

uses library directly!

JFrame library

Color.blue

Color.green

Color.red

Color.pink


Slide8 l.jpg

Color class in Computer Memory Space

Color boxColor = new Color( );

boxColor = Color.blue;

Color.blue

a copy!

Color.green

Color.red

Color.pink

ready for Color

Color.blue


Add the usual main l.jpg
add “the usual” main

public static void main(String args [ ])

{

myGrPgm app = new myGrPgm( );

}

// causes a constructor to be run


So far l.jpg
so far…

import javax.swing.*; // for JFrame

import java.awt.*; // for Color and Graphics

public class myGrPgm extends JFrame

{

public Color palette = new Color ( 25, 120, 200 );

// real work gets done here

public static void main(String args [ ])

{

myGrPgm app = new myGrPgm( );

}

} // end class myGrPgm


A constructor l.jpg
a constructor

// sets up a “Frame”

public myGrPgm( )

{

setSize( 400, 400 );

setLocation( 20, 40 );

setDefaultCloseOperation( EXIT_ON_CLOSE );

setVisible( true );

}

// uses JFrame methods, because WE are JFrame

// i.e. “this” class we’re in is a JFrame child


Graphics on a computer l.jpg
graphics on a computer

X direction

( 0, 0 )

( 1024, 0 )

( ?, ? )

y

direction

( 0, 768 )

( 1024, 768 )


A better constructor l.jpg
a better constructor

// sets up a “Frame”

public myGrPgm( int width, int height)

{

setSize( width, height );

setLocation( 20, 40 );

setDefaultCloseOperation( EXIT_ON_CLOSE );

setVisible( true );

}

// uses JFrame methods, because WE are JFrame

// i.e. “this” class we’re in is a JFrame child


Change main l.jpg
change main

public static void main(String args [ ])

{

myGrPgm app = new myGrPgm( 400, 400 );

}

supplied to constructor


So far15 l.jpg
so far…

import javax.swing.*; // for JFrame

import java.awt.*; // for Color and Graphics

public class myGrPgm extends JFrame

{

public Color palette = new Color ( 25, 120, 200 );

public myGrPgm( int width, int height)

{

setSize( width, height );

setLocation( 20, 40 );

setDefaultCloseOperation( EXIT_ON_CLOSE );

setVisible( true );

}

public static void main(String args [ ])

{

myGrPgm app = new myGrPgm( 400, 400 );

}

} // end class myGrPgm


Last method l.jpg
last method

  • being a JFrame, means that we can USE JFrame methods like “setLocation( … ”

  • being a JFrame, means that we can re-write methods that are IN JFrame for more power, like “paint(… ”


What is paint l.jpg
what is paint?

  • a method in JFrame

  • it’s a “void” method (returns nothing to the caller );

  • called by the computer automatically

  • that we re-write (“override”)

  • that someone else wrote

  • that uses a Graphics helper class

  • that puts colors and shapes in a JFrame


Graphics helper class l.jpg
Graphics helper class?

  • look up the Graphics class on the Sun Java website

  • It must be used by a paint method in a JFrame class

  • it contains variables and methods of its own, to color and draw shapes


Structure of a paint method l.jpg
structure of a paint method

public void paint( Graphics g )

// g is the Graphics copy (object)

{

// set a color

g. setColor( pallette );

// do something with the color

// g.fillRect( x, y, width, height );

g.fillRect( 50, 100, 200, 200 );

}


Setting a color l.jpg
setting a color

  • use our public color “pallette”

  • or use a primary color

    g.setColor( Color.green );


Helps to set the background l.jpg
helps to set the background

// make width and height public, and static

public static int winHeight = 400

public static int winWidth = 400;

// use in paint, or wherever you need window size:

public void paint( Graphics g ) {// set background g.setColor( Color.white ); g.fillRect( 0, 0, winHeight, winWidth );

g.setColor( palette );

g.fillRect( 50, 100, 200, 200 ); }


Static l.jpg
static?

  • whenever you decide something is unique, will never be anything with the same name anywhere in your program

  • main is static

  • any variable or method used by main must be static


Use winheight and winwidth l.jpg
use winHeight and winWidth

public static void main( String args [ ] ) {

myGrPgm app = new myGrPgm( winHeight, winWidth ); }


Slide24 l.jpg
demo

see the myGrPrm example


Using arrays to draw shapes l.jpg
Using Arrays to draw shapes

int xValues[] = { 245, 245, 260, 260, 230, 230 };

int yValues[] = { 250, 300, 300, 310, 310, 250 };

g.setColor( Color.yellow );

g.fillPolygon ( xValues, yValues, 6 );

// connects the dots


Arrays are lists of numbers l.jpg
Arrays are lists of numbers

int xValues[] = { 245, 245, 260, 260, 230, 230 };

xValue[ 0 ] is 245

xValue[ 1 ] is 245

xValue[ 2 ] is 260

xValue[ 3 ] is 260

xValue[ 4 ] is 230

xValue[ 5 ] is 230


A way of organizing variables l.jpg
A way of organizing variables

int myArray[ ] = { 41, 16, 55, 26, 30, 0 };

myArray is a list of 6 ints

The ints are “indexed” 0 thru 5

myArray[ 3 ] is 26

Example:

int x = 4;

myArray[ x ] is ?