Java - 单击按钮后绘制圆圈 [英] Java - draw circle after clicking button

查看:155
本文介绍了Java - 单击按钮后绘制圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在java中按下按钮画一个圆圈。我将System.out.println()放在action方法中以确保我的代码正常工作。 println显示但在任何地方都没有圆绘图。有什么建议?谢谢

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 but no circle drawing anywhere. Any Suggestions? Thank you

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class CircleViewer {


    public static void main(String[] args)
    {
        CircleComponent circle = new CircleComponent();


              JButton button = new JButton("Draw");
              final JPanel panel = new JPanel();
              panel.add(button);
              JFrame frame = new JFrame();

                  class addActionListener implements ActionListener
                  {
                     public void actionPerformed(ActionEvent event)
                     {
                         CircleComponent component = new CircleComponent();
                         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);
                         component.setLocation(xCoord,yCoord);
                         component.getWidth(radius);
                         panel.add(component);
                         System.out.println("CLICKED!");

                     }          
                  }

                  frame.add(panel);
                  ActionListener action = new addActionListener();
                  button.addActionListener(action);

                  frame.setSize(500, 500);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  frame.setVisible(true); 
          }

    }



import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JOptionPane;

/**
   This component lets the user draw a circle by clicking
   a button.
*/
public class CircleComponent extends JPanel 
{ 
       private int x;
       private int y;
       private int width;
       Ellipse2D.Double circle;

       public CircleComponent()
       {
           circle = new Ellipse2D.Double(x, y, width, width);
       }

       public Dimension getPreferredSize() 
       {
           return new Dimension(500,500);
       }

       public void paintComponent(Graphics g)
       {  
           super.paintComponent(g);
           Graphics2D g2 = (Graphics2D) g;
           g2.draw(circle);

       } 

       public int getWidth(int aWidth)
       {
            width = aWidth;
            return width;
       }

} 


推荐答案

您需要覆盖自定义组件的 getPreferredSize()方法。默认情况下,大小为(0,0),因此无需绘制任何内容。

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

revalidate()应该在面板上完成,而不是组件。

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

你的getX()和getY()方法毫无意义。如果要在组件上放置组件,则应使用组件的setLocation(...)方法。

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.

编辑:

仍有许多问题。我会尝试解释它们,然后给出一个更好的(仍然不是很好的)如何做到这一点的例子。

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. 创建圆形时,所有参数都有值为0所以没有什么可画的。您不能稍后更改变量x,y和宽度的值,并期望圆圈反映这些值。

  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.

组件的大小是错误。你不能只做500 x 500的任意大小。首选大小应该是圆圈的大小。

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.

你不能只是添加组件到面板,因为默认情况下面板使用FlowLayout。这意味着将忽略setLocation()方法。我将代码更改为使用null布局。这意味着现在将使用您指定的位置,并且还必须指定组件的大小。

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.

我将您的代码更改为使用匿名内部 classfor ActonListener。这比在另一个类的中间定义一个类更常见。

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.

这是一个简单的例子:

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

这篇关于Java - 单击按钮后绘制圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆