// Program to demonstrate use of separate class for repetitive code
// Potential customers York City and Sheffield United
// J Harvey 2002
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class DrawCrowd2 extends Applet
{
Man M1;
public void init()
{
M1=new Man();
}
public void paint(Graphics g)
{
Color shirtColour;
boolean flipFlop = true;
for (int col=0; col< 500; col+=50)
for (int row=0; row <400; row+=100)
{
flipFlop = !flipFlop;
if (flipFlop)
shirtColour = Color.red;
else
shirtColour = Color.blue;
M1.drawMan(col,row, shirtColour); // at the moment we simply supply
//M1.drawMan(150,150); // parameters for position, but could
//M1.drawMan(220,75); // adapt to include size & colour
}
}
public class Man
{
private Graphics gfx;
public Man()
{
gfx=getGraphics();
}
public void drawMan(int topX, int topY, Color c)
{
gfx.setColor(Color.red);
gfx.fillOval(topX, topY, 40,40); // head
gfx.setColor(Color.blue);
gfx.fillOval(topX+10, topY+10, 4,4); // eyes
gfx.fillOval(topX+30, topY+10, 4,4);
gfx.setColor(c);
gfx.fillRect(topX,topY+40,40,40); // body
gfx.setColor(Color.black);
gfx.fillRect(topX-5, topY+40, 5, 43); // arms
gfx.fillRect(topX+40, topY+40, 5, 43);
gfx.fillRect(topX, topY+80, 5, 50); // legs
gfx.fillRect(topX+35, topY+80, 5, 50);
}
} // end class Man
} // end DrawCrowd |