my program doesn't display the shapes after the button is clicked ..
can someone help me with this ..
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class DrawShapes extends JFrame {
public DrawShapes() {
JPanel panel = new JPanel();
final JButton circle = new JButton("Circle");
JButton square = new JButton("Square");
JButton rectangle = new JButton("Rectangle");
JButton lines = new JButton("Lines");
panel.setLayout(new FlowLayout());
panel.add(circle);
panel.add(square);
panel.add(rectangle);
panel.add(lines);
add(panel, BorderLayout.SOUTH);
//add((new Drawings(Drawings.CIRCLE)), BorderLayout.CENTER);
circle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
if(e.getSource().equals(circle))
add((new Drawings(Drawings.CIRCLE)), BorderLayout.CENTER);
}
});
square.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
add((new Drawings(Drawings.SQUARE)), BorderLayout.CENTER);
}
});
rectangle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
add((new Drawings(Drawings.RECTANGLE)), BorderLayout.CENTER);
}
});
lines.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
add((new Drawings(Drawings.LINE)), BorderLayout.CENTER);
}
});
public static void main (String [] args) {
DrawShapes frame = new DrawShapes();
frame.setTitle("Paint Application");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.JPanel;
public class Drawings extends JPanel {
public static final int CIRCLE = 1;
public static final int SQUARE = 2;
public static final int RECTANGLE = 3;
public static final int LINE = 4;
private int type = 1;
public Drawings(int type) {
this.type = type;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getSize().width;
int height = getSize().height;
switch(type) {
case CIRCLE:
g.setColor(Color.BLACK);
g.fillOval((int)(0.1*width), (int)(0.1*height), (int)(0.8*width), (int)(0.8*height));
break;
case SQUARE:
g.setColor(Color.BLUE);
g.fillRect((int)(0.1*width), (int)(0.1*height), (int)(0.8*width), (int)(0.8*height));
break;
case RECTANGLE:
g.setColor(Color.RED);
g.fillRect((int)(0.1*width), (int)(0.3*height), (int)(0.8*width), (int)(0.3*height));
break;
case LINE:
g.setColor(Color.BLACK);
g.drawLine(100,150,width-100, height-250);
g.drawLine(width-100,150,100,height-250);
break;
}
}
public void setType(int type) {
this.type = type;
repaint();
}
public int getType() {
return type;
}
}
thanks ..