call drawing method
843806Feb 1 2008 — edited Feb 8 2008hHello
I am trying to draw in a frame squares and circles when a button is pressed but i do not really know how to call the method for draw
could you help me please
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.*;
public class Frame extends JFrame implements ActionListener{
private static JButton drawCircle, drawRectangle, drawSquare;
public Frame()
{
JFrame frame = new JFrame("Workshop_1");
frame.setSize(400, 150);
Container content = frame.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout(1,1,0));
drawCircle=new JButton("Draw Circle");
content.add(drawCircle);
drawRectangle=new JButton("Draw Rectangle");
content.add(drawRectangle);
drawSquare=new JButton("Draw Square");
content.add(drawSquare);
drawSquare.addActionListener(this);
drawRectangle.addActionListener(this);
drawCircle.addActionListener(this);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == drawCircle )
{
}
if(e.getSource() ==drawRectangle)
{
}
if(e.getSource() ==drawSquare )
{
}
}
public void drawSquare(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Rectangle.Double myRectangle = new Rectangle.Double(60,90,98,98);
g2.draw(myRectangle);
g2.setColor(new Color(0,140,0));
g2.fill(myRectangle);
}
public void drawRextangle(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Rectangle.Double myRectangle = new Rectangle.Double(60,90,193,98);
g2.draw(myRectangle);
g2.setColor(new Color(0,140,0));
g2.fill(myRectangle);
}
public void drawCircle(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
Ellipse2D.Double circle = new Ellipse2D.Double(12,12,132,123);
g2.draw(circle);
g2.setColor(Color.GREEN);
g2.fill(circle);
}
}