绘画方法在while循环中不起作用 [英] paint method not working inside while loop in swing

查看:113
本文介绍了绘画方法在while循环中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个班级.我试着移动圆圈.当我在 Game.java 中输入 break 语句时,它会为我画一个圆圈,不动.没关系.但是,当我想移动圆圈时(通过删除 break 它什么也不显示.我不知道如何调试它.我发现当我使用 while 没有 break; 时,程序没有进入paint方法; 语句.
任何人都可以告诉我如何调试这样的问题.提前致谢

I have two classes. I try to move circle. When I type a break statement in Game.java it draws a circle for me without movement. That's fine. However when I want to move circle(by deleting break it displays nothing. I dont know how to debug that. I found out program is not going inside paint method when I use while without break; statement.
Can anybody also tell me the way how to debug problem like that. Thanks in advance

App.java

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class App extends JFrame {

    App() {

        JFrame jf = new JFrame(); 
        JPanel jp = new JPanel();
        jf.add(jp);
        jp.setLayout(new BorderLayout()); 
        jf.setSize(500, 500);
        jf.setTitle("Chain Reactor Game");
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setVisible(true);

        Game game = new Game();
        jp.add(game, BorderLayout.CENTER);

        MenuBar menuBar = new MenuBar();
        jf.setJMenuBar(menuBar.createMenuBar());
    }

    public static void main(String[] args) {

        /*
         * oracle recommendation to run swing applications in special thread    
         * they suggest it during thread implementation
         */

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new App();          
            }
        });
    }
}

Game.java

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JPanel;

public class Game extends JPanel {

    private static final long serialVersionUID = 1L;
    int x = 0;
    int y = 0;

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        System.out.println("im goint to filloval now");
        g2d.fillOval(x, y, 30, 30);
    }
    public Game() {

        while(true) {

            moveBall();
            repaint();
            try {
                System.out.println("inside thread");
                Thread.sleep(2000);
            } 
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        break;     // if i delete this line it not drawing. Why is that ?

        }
    }
    private void moveBall() {
        x = x + 1;
        y = y + 1;
    }
}

推荐答案

当你离开 break; 时,你有一个无限循环.它永远不会结束,这意味着行 Game game = new Game(); 永远不会返回,这意味着下一行将 Game 添加到 JPanel 永远不会运行,这就是为什么屏幕上什么也没有绘制的原因.要使其工作,您需要创建 GUI,然后执行绘图.

When you leave the break; in you have an infinite loop. It will never end, meaning that the line Game game = new Game(); will never return, meaning that the next line, which adds the Game to the JPanel will never run, which is why nothing is drawn on the screen. To get this to work, you need to create the GUI, and then perform the drawing.

实现此功能的一种方法是创建一个新的Thread 来执行连续绘图:

One way to get this to work is to create a new Thread to perform the continual drawing:

public Game() {
    (new Thread() {
        public void run() {
            draw();
        }
     ).start();
 }

private void draw() {
    while(true) {
        moveBall();
        repaint();
        try {
            System.out.println("inside thread");
            Thread.sleep(2000);
        } 
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

或者,由于您是在 Swing 线程上设置 GUI,您可以在主线程上运行绘图.

Alternatively, since you are setting up the GUI on the Swing thread, you could run the drawing on the main thread.

这篇关于绘画方法在while循环中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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