0

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);*/
    });

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
nbaq
  • 45
  • 6
  • 1
    Don't put a `JOptionPane` in your `paintComponent` method - that's just asking for trouble – MadProgrammer Feb 10 '20 at 00:25
  • Use one and only one JPanel for all your drawings, add it to the GUI at the beginning, at GUI creation. Change the state of key fields of that JPanel on button press, and have it use those key fields to decide what shape to draw. – Hovercraft Full Of Eels Feb 10 '20 at 00:26
  • 1
    [Here's a great answer that does what you're trying to do](https://stackoverflow.com/a/14993942/522444) – Hovercraft Full Of Eels Feb 10 '20 at 00:29
  • `c.setDimensions(100, 100, d, d);` is an issue, since you're drawing the shape at `g.fillOval(100, 200, diameter, diameter);` - it's going to be off screen. Based on your code, there's no need to call `repaint`, as the component hasn't been realised on the screen yet – MadProgrammer Feb 10 '20 at 00:29
  • @MadProgrammer I'll make sure to remove the JOptionPane from there. I'm just not sure of how to get the user input and pass it through at all. – nbaq Feb 10 '20 at 00:37
  • @MadProgrammer How do I make the component realized on the screen? Or draw it on the new JFrame based on the user input. I was encouraged to make a setter and create the JFrame in the circle constructor, but I can't figure out how to even draw the shape on the frame, much less use the user input to do it. – nbaq Feb 10 '20 at 00:39
  • @HovercraftFullOfEels thank you for the reference! I can't figure out how to add the JPanel to my main GUI frame (which contains the buttons), and I can't figure out how to draw the shape either, especially using user input. I'm able to do other functions, but not draw. – nbaq Feb 10 '20 at 00:41
  • @nbaq "I'll make sure to remove the JOptionPane from there. I'm just not sure of how to get the user input and pass it through at all."* - Pass it via the constructor or via a setter method - this is a pretty common requirement in any programming language – MadProgrammer Feb 10 '20 at 00:43
  • @nbaq *"How do I make the component realized on the screen?"* A component will be "realised" when the container it's attached to is connected to a native peer (ie the frame). This is (generally) done when the frame is first made visible – MadProgrammer Feb 10 '20 at 00:43

0 Answers0