Graphics Programming - PowerPoint PPT Presentation

1 / 67
About This Presentation
Title:

Graphics Programming

Description:

public static final int WIDTH = 300; public static final ... Fractal geometry is the geometry of deterministic chaos. Graphics Programming. 20. Mandelbrot Set ... – PowerPoint PPT presentation

Number of Views:61
Avg rating:3.0/5.0
Slides: 68
Provided by: rick107
Category:

less

Transcript and Presenter's Notes

Title: Graphics Programming


1
Graphics Programming
2
SimpleFrameTest JFrame
import javax.swing. public class
SimpleFrameTest public static void
main(String args) SimpleFrame
frame new SimpleFrame()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
E) frame.show() class
SimpleFrame extends JFrame public
SimpleFrame() setSize(WIDTH, HEIGHT)
public static final int WIDTH 300
public static final int HEIGHT 200
3
CenteredFrameTest
import java.awt. import java.awt.event. impor
t javax.swing. public class CenteredFrameTest
public static void main(String args)
CenteredFrame frame new
CenteredFrame() frame.setDefaultCloseOperat
ion( JFrame.EXIT_ON_CLOSE) frame.show()

class CenteredFrame extends JFrame public
CenteredFrame() // get screen
dimensions Toolkit kit
Toolkit.getDefaultToolkit() Dimension
screenSize kit.getScreenSize() int
screenHeight screenSize.height int
screenWidth screenSize.width // center
frame in screen setSize(screenWidth / 2,
screenHeight / 2) setLocation(screenWidth
/ 4, screenHeight / 4) // set frame icon
and title Image img kit.getImage("icon.gi
f") setIconImage(img)
setTitle("CenteredFrame")
4
CenteredFrameTest Output
5
NotHelloWorld
import javax.swing. import java.awt. public
class NotHelloWorld public static void
main(String args) NotHelloWorldFrame
frame new NotHelloWorldFrame()
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS
E) frame.show() / A frame
that contains a message panel / class
NotHelloWorldFrame extends JFrame public
NotHelloWorldFrame() setTitle("NotHelloWorld
") setSize(WIDTH, HEIGHT) // add
panel to frame NotHelloWorldPanel panel
new NotHelloWorldPanel() Container
contentPane getContentPane()
contentPane.add(panel) public static
final int WIDTH 300 public static final int
HEIGHT 200
6
JPanel
/ A panel that displays a message. / class
NotHelloWorldPanel extends JPanel public
void paintComponent(Graphics g)
super.paintComponent(g) g.drawString("Not
a Hello, World program", MESSAGE_X,
MESSAGE_Y) public static final int
MESSAGE_X 75 public static final int
MESSAGE_Y 100
7
DrawTest
import java.awt. import java.awt.geom. import
javax.swing. public class DrawTest public
static void main(String args) DrawFrame
frame new DrawFrame() frame.setDefaultClo
seOperation(JFrame.EXIT_ON_CLOSE)
frame.show() / A frame that
contains a panel with drawings / class DrawFrame
extends JFrame public DrawFrame()
setTitle("DrawTest") setSize(WIDTH,
HEIGHT) // add panel to frame
DrawPanel panel new DrawPanel()
Container contentPane getContentPane()
contentPane.add(panel) public static
final int WIDTH 400 public static final int
HEIGHT 400
8
DrawPanel Rectangle
/ A panel that displays rectangles and
ellipses. / class DrawPanel extends JPanel
public void paintComponent(Graphics g)
super.paintComponent(g) Graphics2D g2
(Graphics2D)g // draw a rectangle
double leftX 100 double topY 100
double width 200 double height
150 Rectangle2D rect new
Rectangle2D.Double(leftX, topY, width,
height) g2.draw(rect)
9
DrawPanel Ellipses and Lines
// draw the enclosed ellipse Ellipse2D
ellipse new Ellipse2D.Double()
ellipse.setFrame(rect) g2.draw(ellipse)
// draw a diagonal line g2.draw(new
Line2D.Double(leftX, topY, leftX
width, topY height)) // draw a circle
with the same center double centerX
rect.getCenterX() double centerY
rect.getCenterY() double radius 150
Ellipse2D circle new Ellipse2D.Double()
circle.setFrameFromCenter(centerX, centerY,
centerX radius, centerY radius)
g2.draw(circle)
10
FillTest
import java.awt. import java.awt.geom. import
javax.swing. public class FillTest public
static void main(String args) FillFrame
frame new FillFrame() frame.setDefaultClo
seOperation(JFrame.EXIT_ON_CLOSE)
frame.show() / A frame that
contains a panel with drawings / class FillFrame
extends JFrame public FillFrame()
setTitle("FillTest") setSize(WIDTH,
HEIGHT) // add panel to frame
FillPanel panel new FillPanel()
Container contentPane getContentPane()
contentPane.add(panel) public static
final int WIDTH 400 public static final int
HEIGHT 400
11
Filling in Colors
/ A panel that displays filled rectangles
and ellipses / class FillPanel extends JPanel
public void paintComponent(Graphics g)
super.paintComponent(g) Graphics2D
g2 (Graphics2D)g // draw a rectangle
double leftX 100 double topY 100
double width 200 double height
150 Rectangle2D rect new
Rectangle2D.Double(leftX, topY, width,
height) g2.setPaint(Color.red)
g2.fill(rect) // draw the enclosed
ellipse Ellipse2D ellipse new
Ellipse2D.Double() ellipse.setFrame(rect)
g2.setPaint(new Color(0, 128, 128)) // a
dull blue-green g2.fill(ellipse)
12
Generating Color Spectrum
13
Color Quantization
  • Suppose we have a gray scale color depth of 256
    quantization levels.
  • We want to render a 2D plane in column-major
    order and gradually fill in the color.
  • Given a 2D plane of mxn pixels, we need to map
    256 grey scale levels to mxn dots.
  • Need to divide a mxn dot sequence into 256
    segments, each of which will be mapped to one of
    the 256 gray scale levels

14
Setting Up JFrame
import java.util. import java.awt. import
java.awt.Graphics import javax.swing. public
class Spectrum0 public static void
main(String args) DrawFrame frame new
DrawFrame() frame.setDefaultCloseOperatio
n(JFrame.EXIT_ON_CLOSE) frame.show()
class DrawFrame extends JFrame
public DrawFrame() setTitle("Spectrum")
setSize(WIDTH, HEIGHT) Container
c getContentPane() DrawPanel panel
new DrawPanel() c.add(panel)
public static final int WIDTH200 public
static final int HEIGHTWIDTH
15
Filling in Color Levels
class DrawPanel extends JPanel public void
paintComponent(Graphics g)
super.paintComponent(g) float color0
float count0 System.out.println("
In paintComponent") for (int i0
iltgetWidth() i) for (int j0
jltgetHeight() j) count
color count /(getWidth()getHeight())
//g.setColor(Color.getHSBColor(c
olor,1.0f,1.0f)) g.setColor(new
Color(color,color,color))
g.drawRect(i,j,1,1)

16
Problem
  • The paintComponent() function of a JPanel will be
    called up automatically to update a window as
    needed.
  • The rendering of the 2D image is re-processed
    every time paintComponent() is called.
  • Slooooooow updates of an image.
  • Solution Use an image buffer.

17
BufferedImage
class DrawPanel extends JPanel private
BufferedImage bimg private Graphics2D bg2d
private boolean firstTimetrue public
void paintComponent(Graphics g)
super.paintComponent(g) if (firstTime)
//render image float
color0, count0 bimg new
BufferedImage(getWidth(), getHeight(),
BufferedImage.TYPE_INT_RGB) bg2d
bimg.createGraphics() for (int i0
iltgetWidth() i) for (int j0
jltgetHeight() j) count
color count
/(getWidth()getHeight())
bg2d.setColor(Color.getHSBColor(color,1.0f,1.0f))
//bg2d.setColor(new
Color(color,color,color))
bg2d.drawRect(i,j,1,1) //one pixel
firstTimefalse
g.drawImage(bimg, 0, 0,
this)
18
Exercise
  • Use BufferedImage to produce a color spectrum
    that displays one color per column (instead of
    per pixel).
  • Prompt the users for the width of the display
    window (JFrame).
  • Set the height to ¼ of the width.
  • Draw the spectrum in a JPanel, which is contained
    with the JFrame.

19
Fractals and Chaos
  • Simple rules, such as recurrence relations,
    reveal complex structures and behaviors that
    exists in Nature.
  • A simple recurrence rule may define a non-linear
    dynamic system in which small effects accumulate
    and produce drastic results.
  • Attractor is a simple state into which a system
    settles.
  • Chaos is a nondeterminstic system behaviors
    arising in a deterministic system because of
    sensitivity to initial conditions.
  • Fractal geometry is the geometry of deterministic
    chaos.

20
Mandelbrot Set
  • Consider a recurrence relation for complex
    numbers
  • where C is a constant on the complex plane, Zi is
    a complex number, and Z0 is 0.
  • If C is picked within the Mandelbrot Set, Zi
    eventually goes to infinity.
  • However, if C is picked within the Mandelbrot
    Set, Zi never leaves the set.

21
Drawing a Mandelbrot Set
class DrawPanel extends JPanel private
BufferedImage bimg private Graphics2D bg2d
private boolean firstTimetrue private
Complex cmplx new Complex() private static
final double XMAX2.0 private static final
double YMAX2.0 public void
paintComponent(Graphics g)
super.paintComponent(g) if (firstTime)
//render image float
color0, count0 bimg new
BufferedImage(getWidth(), getHeight(),
BufferedImage.TYPE_INT_RGB) bg2d
bimg.createGraphics() for (int i0
iltgetWidth() i) for (int j0
jltgetHeight() j)
cmplx.setReal(xCoordinate(i))
cmplx.setImag(yCoordinate(j))
color (float)getHops(cmplx)/MAXHOPS
bg2d.setColor(Color.getHSBColor(color0
.73f, 1.0f, 1.0f))
//bg2d.setColor(new Color(color,color,color))
bg2d.drawRect(i,j,1,1)

firstTimefalse
g.drawImage(bimg, 0, 0, this)
22
Drawing a Mandelbrot Set (cont.)
private double xCoordinate(int x) return
XMAX(x/(getWidth()/2.0) - 1) private
double yCoordinate(int y) return
-YMAX(y/(getHeight()/2.0) - 1)
private int getHops(Complex z) int i
Complex c z //shallow copy for speed
for (i0 iltMAXHOPS i) if
(z.abs()gt2) break z
z.square().add(c) return i
private int MAXHOPS32
23
Colorful Shapes
  • The various shapes provided in Shape or
    Graphics2D do not carry a color attribute.
  • Basically, to color a shape, you need to reset
    the color of Graphics or Graphics2D prior to
    draw() or fill().
  • We want to have shape objects that store color
    and some other attributes internally.
  • Let's start with a Colorful interface for any
    graphics object.

24
Colorful Interface
import java.awt. interface Colorful
public Color getFillColor() public Color
getDrawColor() public void
setFillColor(Color c) public void
setDrawColor(Color c) public void
fill(Graphics2D g2d) public void
draw(Graphics2D g2d)
25
Transformable Interface
public interface Transformable public void
rotate(double theta) public void
translate(double xDistance, double yDistance)
26
Class Hierarchy
27
Colorful Rectangle
import java.awt. import java.awt.geom. class
ColorfulRectangle extends Rectangle2D.Double
implements Colorful, Transformable protected
Color fillColor, drawColor //for simplicity,
should have been private protected double
theta0 public ColorfulRectangle(double
xcoor, double ycoor, double w, double h)
super(xcoor, ycoor, w, h) fillColor
Color.white //defaults drawColor
Color.black public
ColorfulRectangle(ColorfulRectangle rhs)
super(rhs.x, rhs.y, rhs.width, rhs.height)
fillColor rhs.fillColor drawColor
rhs.drawColor theta rhs.theta
28
Colorful Rectangle (cont.)
public Color getFillColor() return
fillColor public Color
getDrawColor() return drawColor
public void setFillColor(Color c)
fillColorc public void
setDrawColor(Color c) drawColorc
29
Colorful Rectangle (cont.)
public void translate(double xDistance, double
yDistance) x xDistance y
yDistance public void rotate(double
degree) theta Math.toRadians(degree)

30
Colorful Rectangle (cont.)
public void fill(Graphics2D g2d) Color
save g2d.getColor() g2d.setColor(fillCo
lor) g2d.rotate(theta, x, y)
//rotate at point (x,y), upper left corner
g2d.fill(this) g2d.setColor(save)
//restore g2d's color
g2d.rotate(-theta, x, y) //restore g2d's
orientation public void
draw(Graphics2D g2d) Color save
g2d.getColor() g2d.setColor(drawColor)
//g2d.rotate(theta, x, y)
//rotate at point (x,y), upper left corner
g2d.rotate(theta, getCenterX(), getCenterY())
g2d.draw(this) g2d.setColor(save)
//restore g2d's color
//g2d.rotate(-theta, x, y) //restore g2d's
orientation g2d.rotate(-theta,
getCenterX(), getCenterY())
31
ColorfulEllipse
import java.awt. import java.awt.geom. public
class ColorfulEllipse extends Ellipse2D.Double
implements Colorful, Transformable protected
Color fillColor, drawColor protected double
theta0 public ColorfulEllipse(double
centerX, double centerY, double xRadius, double
yRadius) super(centerX-xRadius,
centerY-yRadius, xRadius2, yRadius2)
fillColor Color.white //defaults
drawColor Color.black
32
ColorfulEllipse (cont.)
Same as ColorfulRectangle's
public Color getFillColor() return
fillColor public Color
getDrawColor() return drawColor
public void setFillColor(Color c)
fillColorc public void
setDrawColor(Color c) drawColorc
33
ColorfulEllipse (cont.)
Same as ColorfulRectangle's
public void translate(double xDistance, double
yDistance) x xDistance y
yDistance public void rotate(double
degree) theta Math.toRadians(degree)

34
ColorfulEllipse (cont.)
public void fill(Graphics2D g2d) Color
save g2d.getColor() g2d.setColor(fillCo
lor) g2d.rotate(theta, getCenterX(),
getCenterY()) //rotate at center
g2d.fill(this) g2d.setColor(save)
//restore g2d's color
g2d.rotate(-theta, getCenterX(), getCenterY())
//reset orientation public void
draw(Graphics2D g2d) Color save
g2d.getColor() g2d.setColor(drawColor)
g2d.rotate(theta, getCenterX(),
getCenterY()) //rotate at center
g2d.draw(this) g2d.setColor(save)
//restore g2d's color
g2d.rotate(-theta, getCenterX(), getCenterY())
//reset orientation
35
ColorfulCyclicPolygon
import java.awt. import java.awt.geom. public
class ColorfulCyclicPolygon extends
ColorfulEllipse private double
anglesInRadian public
ColorfulCyclicPolygon(double xcoor, double ycoor,
double radius,
double degrees, int n) super(xcoor,
ycoor, radius, radius) anglesInRadian
new doublen for (int i0 iltn i)
anglesInRadiani Math.toRadians(degree
si)
36
ColorfulCyclicPolygon (cont.)
public void fill(Graphics2D g2d)
Color save g2d.getColor()
g2d.setColor(fillColor)
g2d.rotate(theta, getCenterX(), getCenterY())
//rotate at center Polygon p
toPolygon() g2d.fill(p)
g2d.setColor(save) //restore g2d's
color g2d.rotate(-theta, getCenterX(),
getCenterY()) //reset orientation
public void draw(Graphics2D g2d) Color
save g2d.getColor() g2d.setColor(drawCo
lor) g2d.rotate(theta, getCenterX(),
getCenterY()) //rotate at center
Polygon p toPolygon() g2d.draw(p)
g2d.setColor(save) //restore
g2d's color g2d.rotate(-theta,
getCenterX(), getCenterY()) //reset
orientation
37
ColorfulCyclicPolygon (cont.)
public Polygon toPolygon() int xs
new intanglesInRadian.length int ys
new intanglesInRadian.length int i
double centerx xwidth/2 double
centery yheight/2 double radius
width/2 for (i0 iltanglesInRadian.lengt
h i) xsi Math.round((float)
(centerx radiusMath.sin(anglesInRadiani)))
ysi Math.round((float) (centery -
radiusMath.cos(anglesInRadiani)))
Polygon p new Polygon(xs, ys,
anglesInRadian.length) return p
38
Driver (excerpt)
ColorfulRectangle rect1 new
ColorfulRectangle(getWidth()/4.0,
getHeight()/4.0,
getWidth()/2.0, getWidth()/2.0)
rect1.draw(g2d) ColorfulRectangle rect2
new ColorfulRectangle(rect1)
rect2.setDrawColor(Color.red)
rect2.rotate(45) rect2.draw(g2d)
rect2.translate(getWidth()/8.0, 0.0)
rect2.draw(g2d)
39
Driver Excerpt (cont.)
ColorfulEllipse circle1 new
ColorfulEllipse(getWidth()/2.0, getHeight()/2.0,
getWidth()/4.0, getHeight()/4.0)
circle1.setDrawColor(Color.blue)
circle1.draw(g2d) ColorfulEllipse
ellipse1 new ColorfulEllipse(getWidt
h()/2.0, getHeight()/2.0, getWidth()/3.0,
getHeight()/4.0) ellipse1.setDrawColor(Co
lor.green) ellipse1.draw(g2d)
ellipse1.rotate(45) ellipse1.setDrawColor
(Color.yellow) ellipse1.draw(g2d)
ellipse1.translate(getWidth()/4.0,
0) ellipse1.draw(g2d)
40
Driver Excerpt (cont.)
//stars of david int n 3
double angles new doublen for
(int i0 iltn i) anglesi
i360/n ColorfulCyclicPolygon
triangle new ColorfulCyclicPolygon(40
, 40, 40, angles, n) triangle.draw(g2d)
triangle.rotate(180)
triangle.draw(g2d) triangle.translate(ge
tWidth()/2.0-40, 0) triangle.draw(g2d)
triangle.rotate(180)
triangle.draw(g2d)
41
Summary
  • interface I
  • public void f1()
  • public void f2()
  • public class D1 extends B1 implements I
  • private int x1, x2
  • public void f1()x1 0
  • pubic void f2() x2 0
  • //
  • public class D2 extends B2 implements I
  • private int x1, x2
  • public void f1()x1 0
  • pubic void f2() x2 0
  • //

42
Colorful Polygon
  • Polygon is a predefined class in Java however,
    it is neither colorful nor transformable.
  • Goals
  • Construct a ColorfulPolygon class.
  • Implement both Colorful and Transformation
    interfaces.
  • Derive ColorfulCyclicPolygon from ColorfulPolygon
    instead of ColorfulEllipse.

43
ColorfulPolygon (cont.)
  • Cautions
  • Polygon has a translate function that takes in
    two int's.
  • If the translate(double, double) abstract
    function from Transformation is implemented, a
    function call with int parameters will be
    ambiguous, e.g p.translate(2, 3).

44
ColorfulPolygon Constructors
public class ColorfulPolygon extends Polygon
implements Colorful, Transformable protected
Color fillColor, drawColor protected double
theta0 public ColorfulPolygon()
//this is needed! public
ColorfulPolygon(int xpoints, int ypoints, int
npoints) super(xpoints, ypoints,
npoints) //Polygon's Constructor
fillColor Color.white //defaults
drawColor Color.black
45
Colorful Methods
public Color getFillColor() return
fillColor public Color
getDrawColor() return drawColor
public void setFillColor(Color c)
fillColorc public void
setDrawColor(Color c) drawColorc
46
Filling
public double getCenterX() return
getBounds2D().getCenterX() //getBounds2D() from
Polygon //super.getBounds2D().getCenterX()
public double getCenterY()
return getBounds2D().getCenterY()
public void fill(Graphics2D g2d)
Color save g2d.getColor()
g2d.setColor(fillColor)
g2d.rotate(theta, getCenterX(), getCenterY())
//rotate at center g2d.fill(this)
g2d.setColor(save) //restore
g2d's color g2d.rotate(-theta,
getCenterX(), getCenterY()) //reset
orientation
47
Drawing and Transforming
public void draw(Graphics2D g2d) Color
save g2d.getColor() g2d.setColor(drawCo
lor) g2d.rotate(theta, getCenterX(),
getCenterY()) //rotate at center
g2d.draw(this) g2d.setColor(save)
//restore g2d's color
g2d.rotate(-theta, getCenterX(), getCenterY())
//reset orientation public void
translate(double xDistance, double yDistance)
super.translate(Math.round((float)xDistance),
Math.round((float)yDistance)) public
void rotate(double degree) theta
Math.toRadians(degree)
48
ColorfulCyclicPolygon
  • Why inherit from ColorfulPolygon instead of
    ColorfulEllipse?
  • What methods will have to be rewritten?
  • Can we spend minimal effort to achieve the
    desired results?

49
ColorfulCyclicPolygon Constructor
public class ColorfulCyclicPolygon extends
ColorfulPolygon private double
anglesInRadian private double centerX,
centerY public ColorfulCyclicPolygon(dou
ble xcoor, double ycoor, double radius,
double degrees, int n)
anglesInRadian new doublen
centerXxcoor centerYycoor for
(int i0 iltn i) anglesInRadiani
Math.toRadians(degreesi) npoints
n xpoints new intnpoints
ypoints new intnpoints for (int
i0 iltnpoints i) xpointsi
Math.round((float) (centerX radiusMath.sin(Math
.toRadians(degreesi)))) ypointsi
Math.round((float) (centerY
-radiusMath.cos(Math.toRadians(degreesi))))
//System.out.println(i"
"xpointsi"\t"ypointsi"\t"degreesi)

50
Polar Coordinates Transformation
?
(r sin ? , r cos ?)
Thus, (centerX r sin ? , centerY r cos ?)
centerY
centerX
51
But
centerX
centerY
(r sin ? , r cos ?)
?
52
First, we do vertical flipping
(r sin ? , -r cos ?)
?
?
(r sin ? , r cos ?)
53
Second, we do translation.
(r sin ? , -r cos ?)
Thus, (centerX r sin ? , centerY - r cos ?)
?
centerY
centerX
54
Done!
centerX
centerY
?
(r sin ? , -r cos ?)
Thus, (centerX r sin ? , centerY - r cos ?)
55
What else is needed?
public double getCenterX() return
centerX public double getCenterY()
return centerY
How is polymorphism achieved?
56
Simulating Inheritance using Composition
  • interface I
  • public void f1()
  • public void f2()
  • public class ConcreteI implements I
  • private int x1, x2
  • public void f1()x1 0
  • pubic void f2() x2 0

Only need to make one change.
57
Simulated Multiple Inheritance
  • public class D1 extends B1 implements I
  • protected ConcreteI c new ConcreteI()
  • public void f1()c.f1()
  • pubic void f2() c.f2()
  • //
  • public class D2 extends B2 implements I
  • protected ConcreteI c new ConcreteI()
  • public void f1()c.f1()
  • pubic void f2() c.f2()
  • //

58
New Colorful Shapes (Colorful1)
import java.awt. interface Colorful
public Color getFillColor() public Color
getDrawColor() public void
setFillColor(Color c) public void
setDrawColor(Color c) public void
fill(Graphics2D g2d) public void
draw(Graphics2D g2d) public interface
Transformable public void rotate(double
degree) public void translate(double
xDistance, double yDistance) public interface
ColorfulShape extends Colorful,
Transformable //need this to capture all
Colorful and Transformable Shapes
59
Concrete Class
import java.awt. //for Color import
java.awt.geom. //for Graphics etc. public
class ColorfulShapeClass implements Colorful,
Transformable private Color
drawColorColor.black private Color
fillColorColor.white private double
theta0 private Shape shape //for
callback private Rectangle2D boundingBox
public ColorfulShapeClass(Shape s)
shape s public Color
getFillColor() return fillColor
public Color getDrawColor() return
drawColor
60
Concrete Class (cont.)
public void setFillColor(Color c)
fillColorc public void
setDrawColor(Color c) drawColorc
public void translate(double xDistance,
double yDistance) //to be handled in
derived class public void rotate(double
degree) theta Math.toRadians(degree)

61
Concrete Class (cont.)
public void fill(Graphics2D g2d) Color
save g2d.getColor() g2d.setColor(fillCo
lor) boundingBox shape.getBounds2D()
g2d.rotate(theta, boundingBox.getCenterX(),
boundingBox.getCenterY()) //rotate at
center g2d.fill(shape)
g2d.setColor(save) //restore g2d's
color g2d.rotate(-theta,
boundingBox.getCenterX(), boundingBox.getCenterY()
) //reset orientation public
void draw(Graphics2D g2d) Color save
g2d.getColor() g2d.setColor(drawColor)
boundingBox shape.getBounds2D()
g2d.rotate(theta, boundingBox.getCenterX(),
boundingBox.getCenterY()) //rotate at
center g2d.draw(shape)
g2d.setColor(save) //restore g2d's
color g2d.rotate(-theta,
boundingBox.getCenterX(), boundingBox.getCenterY()
) //reset orientation
62
ColorfulRectangle
import java.awt. import java.awt.geom. class
ColorfulRectangle extends Rectangle2D.Double
implements ColorfulShape protected
ColorfulShapeClass colorfulShape public
ColorfulRectangle(double xcoor, double ycoor,
double w, double h) super(xcoor, ycoor,
w, h) colorfulShape new
ColorfulShapeClass(this) public
ColorfulRectangle(ColorfulRectangle rhs)
x rhs.x y rhs.y width
rhs.width height rhs.height
colorfulShape new ColorfulShapeClass(this)
public Color getFillColor() return
colorfulShape.getFillColor() public
Color getDrawColor() return
colorfulShape.getDrawColor()
63
ColorfulRectangle (cont.)
public void setFillColor(Color c)
colorfulShape.setFillColor(c) public
void setDrawColor(Color c)
colorfulShape.setDrawColor(c) public
void fill(Graphics2D g2d)
colorfulShape.fill(g2d) public void
draw(Graphics2D g2d) colorfulShape.draw(g2
d) public void rotate(double degree)
colorfulShape.rotate(degree)
public void translate(double xDistance, double
yDistance) x xDistance y
yDistance
64
ColorfulPolygon
import java.awt. import java.awt.geom. public
class ColorfulPolygon extends Polygon implements
ColorfulShape protected ColorfulShapeClass
colorfulShape public ColorfulPolygon()
//this is needed! public
ColorfulPolygon(int xpoints, int ypoints, int
npoints) super(xpoints, ypoints,
npoints) colorfulShape new
ColorfulShapeClass(this) public Color
getFillColor() return colorfulShape.getFil
lColor() public Color getDrawColor()
return colorfulShape.getDrawColor()
65
ColorfulPolygon (cont.)
public void setFillColor(Color c)
colorfulShape.setFillColor(c) public
void setDrawColor(Color c)
colorfulShape.setDrawColor(c) public
void fill(Graphics2D g2d)
colorfulShape.fill(g2d) public void
draw(Graphics2D g2d) colorfulShape.draw(g2
d) public void translate(double
xDistance, double yDistance)
super.translate(Math.round((float)xDistance),
Math.round((float)yDistance)) public
void rotate(double degree)
colorfulShape.rotate(degree)
66
ColorfulCyclicPolygon
public class ColorfulCyclicPolygon extends
ColorfulPolygon private double
anglesInRadian private Rectangle2D
boundingBox public ColorfulCyclicPolygon
(double xcoor, double ycoor, double radius,
double degrees, int
n) boundingBox new Rectangle2D.Double(xc
oor-radius, ycoor-radius, radius2, radius2)
npoints n xpoints new
intnpoints ypoints new
intnpoints double centerx
boundingBox.getX() boundingBox.getWidth()/2
double centery boundingBox.getY()
boundingBox.getHeight()/2 for (int i0
iltnpoints i) xpointsi
Math.round((float) (centerx radiusMath.cos(Math
.toRadians(degreesi)))) ypointsi
Math.round((float) (centery
radiusMath.sin(Math.toRadians(degreesi))))
colorfulShape new
ColorfulShapeClass(this)
67
ColorfulCyclicPolygon (cont.)
public void translate(double xDistance,
double yDistance) super.translate(xDistanc
e, yDistance) boundingBox.setRect(boundi
ngBox.getX()xDistance, boundingBox.getY()yDistan
ce,
boundingBox.getWidth(), boundingBox.getHeight())
public Rectangle2D getBounds2D()
return boundingBox
Write a Comment
User Comments (0)
About PowerShow.com