this is the main code which i will be using
// Program randomly draws shapes.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
public class TestDraw extends Frame implements ActionListener{
DrawToolCanvas myCanvas;
Label label;
private int x1;
private int x2;
private int y1;
private int y2;
public int value;
public TestDraw () {
//super("TestDraw");
// setSize (400,400);
// setVisible(true);
// initialize components
myCanvas = new DrawToolCanvas();
Panel south = getSouthPanel(myCanvas);
//configure frame
setTitle("Draw Shape Frame");
addWindowListener(closer);
add(getNorthPanel(), "North");
add(myCanvas, "Center");
add(south, "South");
setSize(400,400);
setLocation(100,100);
setVisible(true);
String user_number;
user_number = JOptionPane.showInputDialog("Enter the number of shapes you would like");
value = Integer.parseInt(user_number);
}
public void actionPerformed(ActionEvent e)
{
Button button = (Button)e.getSource();
String ac = button.getActionCommand();
int inc = 0;
if(ac.equals("Smaller"))
inc = -1;
if(ac.equals("Larger"))
inc = 1;
myCanvas.adjustSize(inc);
label.setText("size: " + myCanvas.size);
}
private Panel getNorthPanel()
{
Button
Smaller = new Button("Smaller"),
Larger = new Button("Larger");
Smaller.setActionCommand("Smaller");
Larger.setActionCommand("Larger");
Smaller.addActionListener(this);
Larger.addActionListener(this);
label = new Label("size: " + myCanvas.size, Label.CENTER);
Panel panel = new Panel(new GridLayout(1,0));
panel.add(Smaller);
panel.add(label);
panel.add(Larger);
return panel;
}
private Panel getSouthPanel(ActionListener l)
{
Panel myPanel = new Panel();
Button square = new Button("square");
square.setActionCommand("square");
Button circle = new Button("circle");
circle.setActionCommand("circle");
Button line = new Button("line");
line.setActionCommand("line");
Button randomshapes = new Button("random shapes");
randomshapes.setActionCommand("random shapes");
myPanel.add(square);
myPanel.add(circle);
myPanel.add(line);
myPanel.add(randomshapes);
square.addActionListener(l);
circle.addActionListener(l);
line.addActionListener(l);
randomshapes.addActionListener(l);
return myPanel;
}
public void paint (Graphics g) {
Graphics2D g2d = (Graphics2D) g;
int numShapes = 20;
int shapeType;
MyLine ml;
MyOval mo;
MyRectangle mr;
super.paint ( g2d );
// create and draw 20 shapes
for ( int i = 1; i <= numShapes; i++ ) {
// pick random shape type
shapeType = ( int ) ( Math.random() * 3 ) + 1;
// pick random coordinates
x1 = ( int ) ( Math.random() * 300 );
y1 = ( int ) ( Math.random() * 300 );
x2 = ( int ) ( Math.random() * 300 );
y2 = ( int ) ( Math.random() * 300 );
if (x2 < x1) {
int temp = x1;
x1 = x2;
x2 = temp;
}
if (y2 < y1) {
int temp = y1;
y1 = y2;
y2 = temp;
}
switch ( shapeType ) {
case 1:
/* Create a new MyLine object using the randomly chosen coordinates
as arguments to the constructor and assign the new object to a
MyLine variable */
ml = new MyLine(x1,y1,x2,y2);
/* Write a statement that tells the MyLine object to draw itself. */
ml.draw(g2d);
break;
case 2:
/* Create a new MyOval object using the randomly chosen coordinates
as arguments to the constructor and assign the new object to a
MyOval variable */
mo = new MyOval(x1,y1,x2,y2);
/* Write a statement that tells the MyOval object to draw itself. */
mo.draw(g2d);
break;
case 3:
/* Create a new MyRectangle object using the randomly chosen coordinates
as arguments to the constructor and assign the new object to a
Myrectangle variable */
mr = new MyRectangle(x1,y1,x2,y2);
/* Write a statement that tells the MyOval object to draw itself. */
mr.draw(g2d);
break;
} // end switch
} // end for
}
public static void main ( final String [] args ) {
TestDraw drawFrame = new TestDraw ();
//drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private WindowListener closer = new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
};
} //end class TestDraw
class DrawToolCanvas extends Canvas implements MouseListener, ActionListener
{
int x, y;
int size;
int delta;
boolean drawCircle;
boolean drawLine;
boolean drawSquare;
boolean drawRandom;
List shapes;
public DrawToolCanvas()
{
size = 100;
delta = 10;
x = -size;
y = -size;
drawCircle = false;
drawLine = false;
drawSquare = false;
drawRandom = false;
shapes = new ArrayList();
addMouseListener(this);
}
public void paint(Graphics g)
{
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
for(int j = 0; j < shapes.size(); j++)
{
Shape s = (Shape)shapes.get(j);
Color color = Color.red;
if(s instanceof Ellipse2D)
color = Color.green;
g2.setPaint(color);
g2.draw(s);
}
}
public void adjustSize(int inc)
{
size += inc * delta;
}
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("square")){
drawSquare = true;
drawCircle = false;
drawLine = false;
drawRandom = false;
}
else
if (event.getActionCommand().equals("line")){
drawLine = true;
drawCircle = false;
drawSquare = false;
drawRandom = false;
}
else
if (event.getActionCommand().equals("random shapes")){
drawRandom = true;
}
else
if (event.getActionCommand().equals("circle")){
drawCircle = true;
drawRandom = false;
}
}
public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
if(drawRandom)
{
int randomnumber = (int) Math.round((Math.random()*10)/3);
switch (randomnumber)
{
case 0:
shapes.add(new Ellipse2D.Double(p.x, p.y, size, size));
break;
case 1:
shapes.add(new Rectangle2D.Double(p.x, p.y, size, size));
break;
case 2:
shapes.add(new Line2D.Double(p.x, p.y, p.x+size, p.y+size));
break;
case 3:
shapes.add(new Ellipse2D.Double(p.x, p.y, size, size));
break;
}
}
else
if(drawCircle)
shapes.add(new Ellipse2D.Double(p.x, p.y, size, size));
else
if(drawSquare)
shapes.add(new Rectangle2D.Double(p.x, p.y, size, size));
else
if(drawLine)
shapes.add(new Line2D.Double(p.x, p.y, p.x+size, p.y+size));
repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}