I've been trying to write a program where a user clicks a button that will ask the user for an int input, and that will draw a new circle and display it on a new JFrame. No matter what I do, it won't show up on the JFrame, and I have to no idea what to do anymore. Here is the circle class. A lot of it is commented out because I don't know what to do.
I have an actionListener in my main class for a button called circle and it can call other methods and print things from the circle class if I add methods, but the shape won't draw. Any advice on what I'm doing wrong?
package shapecreator;
import javax.swing.*;
import java.awt.*;
public class Circle extends JPanel {
private int diameter;
private int x, y, w, h;
public Circle(){
//String radius = JOptionPane.showInputDialog("Enter the diameter");
//diameter = Integer.parseInt(radius);
//this.diameter = d;
//setCircleDimensions(d);
//this.diameter = d;
repaint();
//repaint();
}
public void setDimensions(int x1, int y1, int w1, int h1) {
this.x = x1;
this.y = y1;
this.w = w1;
this.h = h1;
repaint();
}
/*public void setCircleDimensions(int d) {
this.diameter = d;
}*/
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
String radius = JOptionPane.showInputDialog("Enter the diameter");
diameter = Integer.parseInt(radius);
g.fillOval(100, 200, diameter, diameter);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(850, 600);
}
public void display() {
JFrame f = new JFrame();
f.setBounds(200, 200, 850, 600);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
circle.addActionListener((ActionEvent e) -> {
String radius = JOptionPane.showInputDialog("Enter the diameter");
int d = Integer.parseInt(radius);
Circle c = new Circle();
c.setDimensions(100, 100, d, d);
c.repaint();
c.display();
//c.setCircleDimensions(100, 100, d);
//c.repaint();
//resultsPanel.add(c);
//frame.add(c);*/
});