More powerful graphics - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

More powerful graphics

Description:

... 1/360 of a full turn of 2 Pi radians so ... a circle, rotate 1/10th Pi, 20 times ... g2d.rotate( Math.PI / 10 ); // set random drawing color (R, G, B) g. ... – PowerPoint PPT presentation

Number of Views:67
Avg rating:3.0/5.0
Slides: 11
Provided by: cseBu
Category:
Tags: graphics | more | pi | powerful

less

Transcript and Presenter's Notes

Title: More powerful graphics


1
More powerful graphics
  • GeneralPath Class
  • A child (more powerful than) the Polygon class
  • Allows us to rotate and move (translate) polygons
  • Graphics2D Class
  • A child (more powerful than) the graphics class.
  • Remember void paint (Graphics g)
  • Math class
  • Math.random() - Returns a double value with a
    positive sign, greater than or equal to 0.0 and
    less than 1.0.
  • Math.PI -  The double value that is closer than
    any other to pi, the ratio of the circumference
    of a circle to its diameter.

2
The Star
  • generate twenty randomly colored stars equally
    spaced around a circle.

3
General Path
  • How would you know to use a the GeneralPath
    class.
  • You wouldnt.
  • Unless you were a geek, who reads Java textbooks
    as a hobby.
  • Or unless you studied graphics
  • GeneralPath is a way of keeping intricate points
    organized and ready for drawing
  • The Class names are almost always misleading
    (FlowLayout?)

4
Graphics2D
  • This Graphics2D class extends the Graphics class
    to provide more sophisticated control over
    geometry, coordinate transformations, color
    management, and text layout. This is the
    fundamental class for rendering 2-dimensional
    shapes, text and images on the Java(tm)
    platform.

5
This we know
  • Extend JFrame
  • Have a consructor to set up JFrame
  • Have a main that instantiates the JFrame child
    class
  • Have a paint method, that uses
  • GeneralPath (therefore, it can use Graphics2D)
  • Translate
  • Rotate
  • other GeneralPath and JFrame stuff

6
g.translate(200, 200)
  • Moves EVERYTHING in the window 200, 200 pixels
  • public void paint( Graphics g )
  • // a star
  • int xPoints 55, 67, 109, 73, 83, 55, 27,
    37, 1, 43
  • int yPoints 0, 36, 36, 54, 96, 72, 96, 54,
    36, 36
  • g.translate(200, 200)

7
Graphics2D g2d ( Graphics2D ) g
  • void paint (Graphics g)
  • Graphics2D g2d ( Graphics2D ) g
  • Graphics2D is a child (more powerful) of graphics
  • Called casting
  • Gives g all the capabilities if Graphics2D

8
GeneralPath star new GeneralPath()
  • int xPoints 55, 67, 109, 73, 83, 55, 27,
    37, 1, 43
  • int yPoints 0, 36, 36, 54, 96, 72, 96, 54,
    36, 36
  • star.moveTo( xPoints 0 , yPoints 0 )
  • for ( int count 1 count count ) // prepare a line from the last
    point to... star.lineTo( xPoints count ,
    yPoints count )
  • star.closePath()

9
g2d.rotate(
  • g2D.rotate( someAngle )
  • A single degree is 1/360 of a full turn of 2 Pi
    radians so1 (2 Pi/360) radians (pi/180)
    radians
  • Pi radians 180 degrees
  • Pi / 10 radians 18 degrees
  • (Pi / 10 radians) 20 360 degrees
  • So .. If we want to surround a circle, rotate
    1/10th Pi, 20 times

10
  • // rotate around origin and draw stars in random
    colors
  • for ( int count 1 count // rotate coordinate system
  • g2d.rotate( Math.PI / 10 ) //
    set random drawing color (R, G, B)
    g.setColor( new Color( ( int ) (
    Math.random() 256 ), ( int ) (
    Math.random() 256 ), ( int ) (
    Math.random() 256 ) ) ) // draw filled
    star g2d.fill( star )
Write a Comment
User Comments (0)
About PowerShow.com