I have created four classes - three for shapes and one to display everything. And I want to add a shape to frame2
every time the button btn
is clicked. I have been searching for a while but I can't figure it out.
Here is my code:
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
public class Window extends JFrame implements ActionListener{
public JFrame frame = new JFrame("Prototype");
public JFrame frame2 = new JFrame("Container");
JLabel l1 = new JLabel(" " + "2.Choose a color: ");
JLabel l2 = new JLabel("Type x coordinates: ");
JLabel l3 = new JLabel("Type y coordinates: ");
JLabel l4 = new JLabel("Type width: ");
JLabel l5 = new JLabel("Type height: ");
public JTextField t1 = new JTextField(10);
public JTextField t2 = new JTextField(10);
public JTextField t3 = new JTextField(10);
public JTextField t4 = new JTextField(10);
private JButton btn = new JButton("Make It");
private JButton btn1 = new JButton("Show ArrayList");
JMenuBar mb = new JMenuBar();
JMenu mn = new JMenu("1.Choose a shape: ");
JRadioButtonMenuItem fr = new JRadioButtonMenuItem("Filled Rectangle", new ImageIcon("src/webicon.jpg"),true);
JRadioButtonMenuItem uo = new JRadioButtonMenuItem("Unfilled Oval", new ImageIcon("src/webcon2.png"));
JRadioButtonMenuItem fo = new JRadioButtonMenuItem("Filled Oval", new ImageIcon("src/webcon3.png"));
JColorChooser newColor;
Color color;
ArrayList<String> shapes = new ArrayList<String>();
public Window () {
ButtonGroup bg = new ButtonGroup();
bg.add(uo); mb.add(mn); mn.add(fo);
bg.add(fo); mn.add(uo); mn.add(fr);
bg.add(fr);
JPanel p = new JPanel();
p.setLayout(new BorderLayout(2,0));
p.add(l1, BorderLayout.NORTH);
newColor = new JColorChooser();
p.add(newColor, BorderLayout.CENTER);
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(4,2));
p1.add(l2); p1.add(t1);
p1.add(l3); p1.add(t2);
p1.add(l4); p1.add(t3);
p1.add(l5); p1.add(t4);
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(1,2));
p2.add(btn);
btn.addActionListener(this);
p2.add(btn1);
btn1.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setJMenuBar(mb);
frame.getContentPane().add(p, BorderLayout.NORTH);
frame.getContentPane().add(p1, BorderLayout.CENTER);
frame.getContentPane().add(p2, BorderLayout.SOUTH);
frame.setLocation(500,300);
frame.setVisible(true);
frame.setSize(500,520);
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
frame2.setSize(500,520);
// ...
}
public void actionPerformed(ActionEvent e) {
int x = Integer.parseInt(this.t1.getText());
int y = Integer.parseInt(this.t2.getText());
int width = Integer.parseInt(this.t3.getText());
int height = Integer.parseInt(this.t4.getText());
if(e.getSource() == btn){
if(fr.isSelected()){
this.color = this.newColor.getColor();
// ...
new FilledRectangle(x, y, width, height, this.color);
this.shapes.add("Filled Rectangle");
}
else if(fo.isSelected()){
this.color = this.newColor.getColor();
new FilledOval(x, y, width, height, this.color);
this.shapes.add("Filled Oval");}
else if(uo.isSelected()){
this.color = this.newColor.getColor();
new UnfilledOval(x, y, width, height, this.color);
this.shapes.add("Unfilled Oval");}
}
else if(e.getSource() == btn1){
for (int i = 0; i < this.shapes.size(); i++) {
System.out.println(this.shapes.get(i));
}
}
}
}
And this a class of a shape (for instance the FilledRectangle
):
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FilledRectangle extends Frame{
private int x, y, width, height;
private Color color;
public String name = "Filled Rectangle";
public FilledRectangle(int x, int y, int width, int height, Color color){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public void paint(Graphics g){
g.setColor(color);
g.fillRect(this.x, this.y, this.width, this.height);
}
}
I would appreciate if anyone can help me.