Hello,
I am writing a Java program where the first JFrame has choices for number of shapes, width, height and the type of shape (oval, rectangle or both) and a button is clicked that opens up another JFrame where the shapes are drawn with a time delay until the number of shapes equals the user input. Everything works fine except the JButton from the first JFrame is displayed at the top left corner of the second JFrame when the JButton on the first frame is pressed. Any idea how to fix this issue?
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.JFrame;
import javax.swing.JDialog;
import java.awt.Dimension;
import java.awt.Component;
public class Interactive extends JPanel implements ActionListener{
private int shape, x, y, width, height;
private String shapeType;
private Random r = new Random();
private Timer t;
private JTextField field1, field2, field3, field4;
private JButton button;
private JLabel label1, label2, label3, label4;
private JDialog dialog;
public Interactive(){
setLayout (new BoxLayout(this, BoxLayout.Y_AXIS));
label1 = new JLabel ("Number of Shapes", JLabel.CENTER);
label1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
field1 = new JTextField (20);
field1.setMaximumSize(new Dimension(200,20));
field1.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label2 = new JLabel ("Width", JLabel.CENTER);
label2.setAlignmentX(JLabel.CENTER_ALIGNMENT);
field2 = new JTextField (20);
field2.setMaximumSize(new Dimension(200,20));
field2.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label3 = new JLabel ("Height", JLabel.CENTER);
label3.setAlignmentX(JLabel.CENTER_ALIGNMENT);
field3 = new JTextField (20);
field3.setMaximumSize(new Dimension(200,20));
field3.setAlignmentX(JLabel.CENTER_ALIGNMENT);
label4 = new JLabel ("Shape Type", JLabel.CENTER);
label4.setAlignmentX(JLabel.CENTER_ALIGNMENT);
field4 = new JTextField (20);
field4.setMaximumSize(new Dimension(200,20));
field4.setAlignmentX(JLabel.CENTER_ALIGNMENT);
button = new JButton ("Submit");
button.addActionListener(this);
button.setAlignmentX(JLabel.CENTER_ALIGNMENT);
add(Box.createRigidArea(new Dimension(20, 10)));
add(label1);
add(Box.createRigidArea(new Dimension(20, 10)));
add(field1);
add(Box.createRigidArea(new Dimension(20, 10)));
add(label2);
add(Box.createRigidArea(new Dimension(20, 10)));
add(field2);
add(Box.createRigidArea(new Dimension(20, 10)));
add(label3);
add(Box.createRigidArea(new Dimension(20, 10)));
add(field3);
add(Box.createRigidArea(new Dimension(20, 10)));
add(label4);
add(Box.createRigidArea(new Dimension(20, 10)));
add(field4);
add(Box.createRigidArea(new Dimension(20, 10)));
add(button);
}
public void actionPerformed (ActionEvent e){
System.out.println ("Event Handler");
shape = Integer.parseInt(field1.getText());
width = Integer.parseInt(field2.getText());
height = Integer.parseInt(field3.getText());
String shapeType = field4.getText();
Draw d = new Draw(shape + 1, width, height, shapeType);
}
public static void main (String args[]){
JFrame f = new JFrame ("Jocelyn's Art");
Interactive d = new Interactive();
f.add(d);
f.setSize(500, 500);
f.setVisible (true);
f.setResizable(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.Timer;
import javax.swing.JFrame;
public class Draw extends JPanel implements ActionListener{
private int width, height,shape;
private int count = 0;
private String shapeType;
private Random r = new Random();
private JFrame f;
private Timer t;
public Draw (int shape, int width, int height, String shapeType){
this.width = width;
this.height = height;
this.shape = shape;
this.shapeType = shapeType;
f = new JFrame ("Jocelyn's Art");
f.add(this);
f.setSize(500, 500);
f.setVisible (true);
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
t = new Timer (100, this);
t.start();
}
public void actionPerformed (ActionEvent e){
repaint();
}
public void paintComponent (Graphics g){
int x = r.nextInt(300);
int y = r.nextInt(300);
count++;
if (shapeType.equalsIgnoreCase ("Oval")){
g.fillOval (x,y,width, height);
}
else if (shapeType.equalsIgnoreCase("Rectangle")){
g.fillRect (x,y,width,height);
}
else{
if (count % 2 == 1){
g.fillOval (x,y,width, height);
}
else{
g.fillRect (x,y,width,height);
}
}
if (this.shape == count){
t.stop();
}
}
}
Thank you