简体   繁体   English

Java:paint不会调用repaint

[英]Java: paint doesn't call with repaint

Let me be honest, I'm not really know what I'm doing. 老实说,我不太清楚自己在做什么。 I just moved from python to Java, and i'm still trying to get used to all the classes and the types things. 我刚从python转到Java,但我仍在尝试适应所有的类和类型的东西。

I decide to make a break with java concepts tutorials and start to get my hands dirty. 我决定在Java概念教程中休息一下,然后开始动手。 According to my understanding, I'm using swing to paint a ball on the screen and make it move. 据我了解,我正在使用秋千在屏幕上画一个球并使它移动。

I tried to design a ball object that handle the ball position and the screen bumping, but the ball doesn't moved at all. 我试图设计一个可以处理球位置和屏幕碰撞的球对象,但球根本不会移动。 When I turn on the debug I noticed that the paint() function get called only at creation, but not get called with repaint(). 当我打开调试时,我注意到paint()函数仅在创建时被调用,而没有通过repaint()被调用。

I got a feeling that I'm using a bad tutorial to do this stuff, its look like there is a better way to do it. 我感觉到我正在使用一个糟糕的教程来完成这些工作,它看起来像是有一种更好的方法。

Anyway, I will be glad to hear what you guys thinking. 无论如何,我将很高兴听到你们的想法。

Edit: After I saw your comments I notice that paint actually get called when I put sysout there. 编辑:看到您的评论后,我注意到当我将sysout放到那里时,paint实际上被调用了。 Its seems that the debugger doesn't jump to there before I put sysout in paint() . 在将sysout in paint()之前,调试器似乎并没有跳到那里。 My guess is that I'm not really changing the position of the ball. 我的猜测是我并没有真正改变球的位置。

@SuppressWarnings("serial")
public class Tennis extends JPanel {
    Ball ball = new Ball(50,50);

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        int[] position = ball.getPosition();
        g2d.fillOval(position[0],position[1], 30, 30);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("Mini Tennis");

        Tennis game = new Tennis();
        frame.add(game);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        while (true) {
            // just change the position and check for bump
            game.ball.move(game.getHeight(), game.getWidth());
            game.repaint();

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

Change paint() to paintComponent(), for an explanation of the differences therein see this . 将paint()更改为paintComponent(),有关其中差异的说明,请参见this

@Override
public void paintComponent(Graphics g){ //CHANGE HERE
    super.paintComponent(g);            //AND HERE
    Graphics2D g2d = (Graphics2D) g;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
    int[] position = ball.getPosition();
    g2d.fillOval(position[0],position[1], 30, 30);
}

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

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