Topic: Name: _____________________________________________________ Period: _______ Date: _______________________ Lab01b: Intro to Graphics

advertisement
Topic:
Lab01b: Intro to Graphics
Objectives
Graphic methods
Parts of a Class
Circle class
Name: _____________________________________________________
Period: _______ Date: _______________________

I will continue work on writing methods that call the Graphics class methods
Recall the Graphic methods we used last class:
 setColor
 setFont
 drawString
 drawLine
 fillRect, drawRect
 fillOval, drawOval
 fillPolygon, drawPolygon



Data Variables – state or attributes
Constructors – how your initialize your objects
Methods – actions of your methods
Variables
 xCord
 yCord
 radius
Constructor
 3 arguments: (int x, int y, int rad)
Methods
 draw(Graphics g, int red, int green, int blue)
 drawShadedBlack ( Graphics g )
public
{
int
int
int
class Circle
xCord;
yCord;
radius;
public Circle(int x, int y, int r)
{
xCord=x;
yCord=y;
radius=r;
}
public void draw(Graphics g, int r, int gr, int b)
{
//create a new Color object
Color cc = new Color(r, gr, b);
//determine the upper left hand corner
int x = xCord-radius;
int y = yCord-radius;
g.setColor(cc);
g.fillOval(x, y, radius*2, radius*2);
}
public void drawShadeBlack(Graphics g)
{
double incVal = (double)255/radius;
int clr = 0;
int rad;
for (int k=0; k < radius; k++)
{
clr = (int)(k * incVal);
if (clr>255)
clr=255;
Color cc = new Color(clr, clr, clr);
g.setColor(cc);
rad = radius-k;
g.fillOval(xCord-rad, yCord-rad, rad*2, rad*2);
}
}
ToDo:
 Complete the Circle.java class – look at notes above.
 Complete drawShadeGreen, drawShadeRed, and drawShadeBlue using
drawShadeBlack as an example. Shading Black, you modified all the 3 color
components, in drawShadeXYZ(), only specify that color.
 Run the Panel01_3x4_RandomCircles panel, should run ok if you have the Color


Summary:
class implemented.
Write a Rectangle class
Copy the modify Panel01_3x4_RandomCircle to display Rectangles.
Download