270 likes | 605 Views
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
E N D
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 myGrPgmextends JFrame { // extending JFrame gives us: // - windows // - paint // - use of Color }// end class myGrPgm
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 Color boxColor = Color.blue or Color boxColor = new Color( ); boxColor = Color.blue;object t to box Color.
Color class in Computer Memory Space Color boxColor = Color.blue uses library directly! JFrame library Color.blue Color.green Color.red Color.pink
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 public static void main(String args [ ]) { myGrPgm app = new myGrPgm( ); } // causes a constructor to be run
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 // 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 X direction ( 0, 0 ) ( 1024, 0 ) ( ?, ? ) y direction ( 0, 768 ) ( 1024, 768 )
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 public static void main(String args [ ]) { myGrPgm app = new myGrPgm( 400, 400 ); } supplied to constructor
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 • 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? • 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? • 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 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 • use our public color “pallette” • or use a primary color g.setColor( Color.green );
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? • 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 public static void main( String args [ ] ) { myGrPgm app = new myGrPgm( winHeight, winWidth ); }
demo see the myGrPrm example
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 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 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 ?