import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
import javax.swing.JPanel;
public class DrawPanel extends JPanel
{
private Random randomNumbers = new Random();
//private MyLine firstLine;
//private MyOval firstOval;
//private MyRectangle firstRectangle;
public static int countL = 0;
public static int countO = 0;
public static int countR = 0;
MyShape [] myShapes = new MyShape[8];//randomNumbers.nextInt(5)];
int shapeType;
int x2;
int x1;
int y1;
int y2;
Color color;
public DrawPanel()
{
setBackground(Color.WHITE);
}
public int getx1()
{
return x1 = randomNumbers.nextInt(300);
}
public int getx2()
{
return x2 = randomNumbers.nextInt(300);
}
public int gety1()
{
return y1 = randomNumbers.nextInt(300);
}
public int gety2()
{
return y2 = randomNumbers.nextInt(300);
}
public int shapeType()
{
return shapeType = randomNumbers.nextInt(3);
}
public Color getColor()
{
return color = new Color(randomNumbers.nextInt(256), randomNumbers.nextInt(256), randomNumbers.nextInt(256)) ;
}
public void paintComponent (Graphics g)
{
super.paintComponent (g);
/*for each array element randomly create an object of different shape type*/
for (int i=0; i < myShapes.length; i++)
{
int ws = shapeType(); //decide which of the 3 shape objects is created
if (ws == 0)
{
countL++;
myShapes[i] = new MyLine( getx1(), getx2(), gety1(), gety2(), getColor() );
}
else if (ws == 1)
{
countO++;
myShapes [i] = new MyOval( getx1(), getx2(), gety1(), gety2(), getColor() );
}
else if (ws == 2)
{
myShapes[i] = new MyRectangle( getx1(), getx2(), gety1(), gety2(), getColor() );
countR++;
}
}
System.out.printf("%d %d %d\n", DrawPanel.countL, DrawPanel.countO, DrawPanel.countR);
for (int i=0; i < myShapes.length; i++)
{
myShapes[i].draw(g);
}
}
}
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.JLabel;
public class TestDraw {
/**
* @param args
* @return
*/
public static void main(String[] args)
{
DrawPanel panel = new DrawPanel();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
JLabel southLabel = new JLabel("South");
southLabel.setText(whatToSay() );
application.add(southLabel, BorderLayout.SOUTH);
application.setSize(300,300);
application.setVisible(true);
}
public static String whatToSay()
{
String wts;
wts = String.format("Lines %d Ovals %d Rectangles %d", DrawPanel.countL, DrawPanel.countO, DrawPanel.countR);
return wts;
}
}
import java.awt.Color;
import java.awt.Graphics;
public class MyOval extends MyShape
{
public MyOval(int x1, int x2, int y1, int y2, Color color)
{
super (x1,x2,y1,y2,color);
}
public void draw(Graphics g)
{
g.setColor( getColor() );
g.drawOval( getx1(), getx2(), getwdth(), gethght() );
}
}
import java.awt.Color;
import java.awt.Graphics;
public abstract class MyShape {
private int x1, x2, y1, y2, xStart, yStart, xEnd, yEnd, wdth, hght;
private Color myColor;
public MyShape()
{
x1 = 0; x2 = 0; y1 = 0; y2 = 0;
myColor = (Color.BLACK);
}
public MyShape (int x1, int x2, int y1, int y2, Color color)
{
setx1(x1);
setx2(x2);
sety1(y1);
sety2(y2);
setmyColor(color);
//for ovals and recatngles
xStart = Math.min(x1,x2);
yStart = Math.min(y1,y2);
xEnd = Math.max(x1,x2);
yEnd = Math.max(y1,y2);
wdth = xEnd - xStart;
hght = yEnd - yStart;
}
public void setx1(int x)
{
if (x >= 0 && x <= 250)
x1 = x;
else x1 = 0;
//return x1;
}
public void setx2(int x)
{
//if (x >= 0 && x <= 250)
x2 = x;
//else x2 = 0;
//return x1;
}
public void sety1(int y)
{
//if (y >= 0 && y <= 250)
y1 = y;
//else y1 = 0;
//return x1;
}
public void sety2(int y)
{
//if (y >= 0 && y <= 250)
y2 = y;
//else y2 = 0;
//return x1;
}
public void setmyColor(Color color)
{
myColor = color;
}
public int getx1()
{
return x1;
}
public int getx2()
{
return x2;
}
public int gety1()
{
return y1;
}
public int gety2()
{
return y2;
}
public Color getColor()
{
return myColor;
}
public int getwdth()
{
return wdth;
}
public int gethght()
{
return hght;
}
abstract void draw(Graphics g);
}