0

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.

Magnilex
  • 11,584
  • 9
  • 62
  • 84
  • I can't understand what you want to do. You have `Window` which is a `JFrame`. Then in `Window` class you have 2 `JFrame` variables, `frame` and `frame2`. You have made `frame` and `frame2` visible which means 2 `JFrame`s are shown when you run the program. Then you have this shapes classes which are again `Frame`s. So what do you want to do? – Blip May 02 '15 at 12:50
  • In the `Window.actionPerformed` method you create components without doing anything with them (for example: `new FilledRectangle(x, y, width, height, this.color)`). You might want to add them to `frame2`? – Freek de Bruijn May 02 '15 at 12:51
  • 1
    1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). 3) Don't use `java.awt.Frame` when there is `javax.swing.JFrame`! 4) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 5) `public class Window extends JFrame..` Don't use the name of an AWT class for your class! – Andrew Thompson May 02 '15 at 12:58
  • I want to add this new FilledRectangle(x, y, width, height, this.color) into the `frame2`. – Alexandar Angelov May 02 '15 at 13:57
  • Don't extend `Frame` to do custom painting. Custom painting is done by overriding `paintComponent()` method of a JPanel (or JComponent) and then you add the panel to your frame. – camickr May 02 '15 at 14:53

0 Answers0