Java - draw circle after clicking button

后端 未结 1 1983
没有蜡笔的小新
没有蜡笔的小新 2021-01-26 09:27

I am trying to draw a circle with the press of a button in java. I put the System.out.println() inside the action method to make sure my code was working. The println shows up b

1条回答
  •  生来不讨喜
    2021-01-26 09:32

    You need to override the getPreferredSize() method of you custom component. By default the size is (0, 0) so there is nothing to paint.

    The revalidate() should be done on the panel, not the component.

    Your getX() and getY() methods make no sense. If you want to position the component on the panel you should be using the setLocation(...) method of the component.

    Edit:

    There are still lots of probolems. I'll try to explain them and then give a better (still not a great) example of how you might do this.

    circle = new Ellipse2D.Double(x, y, width, width);
    
    1. When you create the circle shape all the parameters have a value of 0 so there is nothing to paint. You can't just change the values of the variables x, y and width later and expect the circle to reflect those values.

    2. The size of the component is wrong. You can't just make an arbitrary size of 500 x 500. The preferred size should be the size of the circle.

    3. You can't just add the component to the panel because the panel uses a FlowLayout by default. This means the setLocation() method will be ignored. I changed your code to use a null layout. This means that the location you specify will now be used and you must also specify the size of the component.

    4. I changed your code to use an "anonymous inner class" for the ActonListener. This is more common then defining a class in the middle of another class.

    Here is the quick example:

    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class CircleComponent extends JPanel
    {
           Ellipse2D.Double circle;
    
           public CircleComponent(int radius)
           {
               circle = new Ellipse2D.Double(0, 0, radius, radius);
               setOpaque(false);
           }
    
           public Dimension getPreferredSize()
           {
                Rectangle bounds = circle.getBounds();
               return new Dimension(bounds.width, bounds.height);
           }
    
           public void paintComponent(Graphics g)
           {
               super.paintComponent(g);
               Graphics2D g2 = (Graphics2D) g;
               g2.setColor( getForeground() );
               g2.fill(circle);
    
           }
    /*
           public int getWidth(int aWidth)
           {
                width = aWidth;
                return width;
           }
    */
    
        public static void main(String[] args)
        {
                //  Create a panel using a null layout so we can add components at random positions
                final JPanel center = new JPanel();
                center.setLayout(null);
    
                  JButton button = new JButton("Draw");
                  button.addActionListener( new ActionListener()
                  {
                     public void actionPerformed(ActionEvent event)
                     {
                         String x = JOptionPane.showInputDialog("X Coordinate", "Enter an x coordinate");
                         int xCoord = Integer.parseInt(x);
                         String y = JOptionPane.showInputDialog("Y Coordinate", "Enter a y coordinate");
                         int yCoord = Integer.parseInt(y);
                         String width = JOptionPane.showInputDialog("Radius", "Enter the length of the radius");
                         int radius = Integer.parseInt(width);
                         CircleComponent component = new CircleComponent(radius);
                         component.setLocation(xCoord,yCoord);
                         component.setSize(component.getPreferredSize());
                         center.add(component);
                         center.repaint();
    
                     }
                  });
    
                  JFrame frame = new JFrame();
                  frame.add(center, BorderLayout.CENTER);
                  frame.add(button, BorderLayout.NORTH);
                  frame.setSize(500, 500);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setVisible(true);
        }
    }
    

    0 讨论(0)
提交回复
热议问题