Creating a graphics program - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Creating a graphics program

Description:

for Color and Graphics. Program Framework. public class myGrPgm extends JFrame ... 'palette' is a Color object // Color (red, green, blue ); note: 25, 120, 200 ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 28
Provided by: mik97
Category:

less

Transcript and Presenter's Notes

Title: Creating a graphics program


1
Creating a graphics program
  • Color and Shapes

2
Import libraries
  • import javax.swing. // for JFrame
  • import java.awt. // for Color and Graphics

3
Program Framework
  • public class myGrPgm extends JFrame
  • // extending JFrame gives us
  • // - windows
  • // - paint
  • // - use of Color
  • // end class myGrPgm

4
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
5
how colors are defined
6
a clarification
  • Color boxColor Color.blue
  • or
  • Color boxColor new Color( )
  • boxColor Color.blueobject t to box Color.

7
Color class in Computer Memory Space
  • Color boxColor Color.blue

uses library directly!
JFrame library
Color.blue
Color.green
Color.red
Color.pink
8
  • Color boxColor new Color( )
  • boxColor Color.blue

Color class in Computer Memory Space
Color.blue
a copy!
Color.green
Color.red
Color.pink
ready for Color
Color.blue
9
add the usual main
  • public static void main(String args )
  • myGrPgm app new myGrPgm( )
  • // causes a constructor to be run

10
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

11
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 were in is a JFrame child

12
graphics on a computer
X direction
( 0, 0 )
( 1024, 0 )
( ?, ? )
y direction
( 0, 768 )
( 1024, 768 )
13
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 were in is a JFrame child

14
change main
  • public static void main(String args )
  • myGrPgm app new myGrPgm( 400, 400 )

supplied to constructor
15
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

16
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(

17
what is paint?
  • a method in JFrame
  • its 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

18
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

19
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 )

20
setting a color
  • use our public color pallette
  • or use a primary color
  • g.setColor( Color.green )

21
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 )

22
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

23
use winHeight and winWidth
  • public static void main( String args )
  • myGrPgm app new myGrPgm( winHeight,
    winWidth )

24
demo
  • see the myGrPrm example

25
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

26
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

27
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 ?
Write a Comment
User Comments (0)
About PowerShow.com