简体   繁体   English

JPanel上的椭圆形不会在其他位置绘制

[英]Oval on JPanel won't draw on different locations

I have a panel on which I have to draw balls, each time I press the mouse button. 每次按下鼠标按钮时,我都有一个必须画球的面板。 The balls are supposed to be drawn on the location pressed by the mouse. 这些球应该在鼠标按下的位置绘制。

The balls are drawn just fine, however they are drawn on the same location every time, so unless I "move" the window right and left, the drawn balls would not be seen. 球绘制得很好,但是每次都在同一位置绘制,因此除非我左右左右移动窗口,否则看不到绘制的球。

Here's my code: 这是我的代码:

GUIBalls

public class GUIBalls {
public  JFrame frame = new JFrame("Balls");
public  JPanel panel = new JPanel();
private ArrayList<Ball> b = new ArrayList<>();
private Random rnd = new Random();

public GUIBalls(){
    setFrame();
    //moveBalls();
}
public void setFrame(){
    this.frame.setBounds(0,0,400,400);
    this.frame.add(panel);
    panel.setBounds(0,0,400,400);
    this.panel.addMouseListener(new MouseListener() {
        @Override
        public void mouseClicked(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();
            createBalls(x,y);
        }

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }
    });
}
public void createBalls(int x, int y){
    Ball ball = new Ball(x,y,10,10);
    this.b.add(ball);
    ball.draw(panel);
    panel.repaint();
}
}

Ball

public class Ball extends JPanel {
int x;
int y;
int z;
int w;

public Ball(int x, int y, int z, int w) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.w = w;
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.fillOval(0, 0, z, w);

}

public void draw(JPanel panel) {
    panel.add(this);
    panel.setVisible(true);

}
}

For some odd reason, the balls are not being drawn on different locations. 出于某些奇怪的原因,这些球并未在不同的位置上绘制。

Pictures of what happens are attached. 随便附上图片。 在此处输入图片说明 在此处输入图片说明

Any Ideas? 有任何想法吗?

The problem with your code right now is that you have a combination of trying to do custom painting and trying to create a custom component . 现在,您的代码存在的问题是,您需要尝试进行custom painting和尝试创建custom component这两种组合在一起。 You need to decide which approach you want to use. 您需要确定要使用哪种方法。

For the basics of custom painting read the section from the Swing tutorial on Custom Painting that has a working example showing how to do this. 有关自定义绘画的基础知识,请阅读Swing教程“ 自定义绘画”中的部分 ,其中有一个工作示例,说明了如何执行此操作。 Well, it shows how to draw a square on a panel but you get the idea. 好吧,它显示了如何在面板上绘制正方形,但是您明白了。

If you want to keep adding objects to be painted, then you need to keep your objects in a ArrayList and then in the paintComponent() method you iterate through the List to paint each object. 如果要继续添加要绘制的对象,则需要将对象保留在ArrayList中,然后在paintComponent()方法中迭代列表以绘制每个对象。 This approach is demonstrated in Custom Painting Approaches . 此方法在“ 自定义绘画方法”中得到了证明。

If you want to create a Ball as a component, then you need to make sure you override the get preferred size to return the size of the Ball. 如果要创建一个Ball作为组件,则需要确保覆盖获取的首选尺寸以返回Ball的尺寸。 Then you always draw the oval at offset (0, 0) of the panel. 然后,您总是在面板的偏移(0,0)处绘制椭圆。 You then add the Ball component to the parent panel which uses a null layout. 然后,将Ball组件添加到使用空布局的父面板中。 Because you use a null layout you will position the Ball on the parent panel by using the setLocation(...) method. 因为使用的是空布局,所以将使用setLocation(...)方法将Ball放置在父面板上。 You will also need to use the setSize() method of the Ball component to be the preferred size. 您还需要使用Ball组件的setSize()方法作为首选大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM