i have code below that allows you to draw shapes (circles + squares), when you run the code you should be able to see what it does but what i need is instead of the user having to use the mouse to create the shape, i would like a popup to appear when the user clicks the "square" or "circle" button, asking for how many squares or circles would you like to draw, the user then types in the number and it auto draws that many shapes!!! im not very good at java so any help would be great.Thanks
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.List;
import java.util.ArrayList;
public class DTF extends Frame implements ActionListener
{
DrawToolCanvas myCanvas;
Label label;
public DTF()
{
// initialize components
myCanvas = new DrawToolCanvas();
Panel south = getSouthPanel(myCanvas);
// configure frame
setTitle("Draw Tool Frame");
addWindowListener(closer);
add(getNorthPanel(), "North");
add(myCanvas, "Center");
add(south, "South");
setSize(400,400);
setLocation(200,200);
setVisible(true);
}
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");
myPanel.add(square);
myPanel.add(circle);
square.addActionListener(l);
circle.addActionListener(l);
return myPanel;
}
private WindowListener closer = new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
};
public static void main(String[] args)
{
new DTF();
}
}
class DrawToolCanvas extends Canvas implements MouseListener, ActionListener
{
int x, y;
int size;
int delta;
boolean drawCircle;
List shapes;
public DrawToolCanvas()
{
size = 40;
delta = 5;
x = -size;
y = -size;
drawCircle = true;
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.blue;
g2.setPaint(color);
g2.draw(s);
}
}
public void adjustSize(int inc)
{
size += inc * delta;
}
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("square"))
drawCircle = false;
else if (event.getActionCommand().equals("circle"))
drawCircle = true;
}
public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
if(drawCircle)
shapes.add(new Ellipse2D.Double(p.x, p.y, size, size));
else
shapes.add(new Rectangle2D.Double(p.x, p.y, size, size));
repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.List;
import java.util.ArrayList;
public class DTF extends Frame implements ActionListener
{
DrawToolCanvas myCanvas;
Label label;
public DTF()
{
// initialize components
myCanvas = new DrawToolCanvas();
Panel south = getSouthPanel(myCanvas);
// configure frame
setTitle("Draw Tool Frame");
addWindowListener(closer);
add(getNorthPanel(), "North");
add(myCanvas, "Center");
add(south, "South");
setSize(400,400);
setLocation(200,200);
setVisible(true);
}
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");
myPanel.add(square);
myPanel.add(circle);
square.addActionListener(l);
circle.addActionListener(l);
return myPanel;
}
private WindowListener closer = new WindowAdapter()
{
public void windowClosing(WindowEvent event)
{
System.exit(0);
}
};
public static void main(String[] args)
{
new DTF();
}
}
class DrawToolCanvas extends Canvas implements MouseListener, ActionListener
{
int x, y;
int size;
int delta;
boolean drawCircle;
List shapes;
public DrawToolCanvas()
{
size = 40;
delta = 5;
x = -size;
y = -size;
drawCircle = true;
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.blue;
g2.setPaint(color);
g2.draw(s);
}
}
public void adjustSize(int inc)
{
size += inc * delta;
}
public void actionPerformed(ActionEvent event)
{
if (event.getActionCommand().equals("square"))
drawCircle = false;
else if (event.getActionCommand().equals("circle"))
drawCircle = true;
}
public void mousePressed(MouseEvent e)
{
Point p = e.getPoint();
if(drawCircle)
shapes.add(new Ellipse2D.Double(p.x, p.y, size, size));
else
shapes.add(new Rectangle2D.Double(p.x, p.y, size, size));
repaint();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
}