Event Dispatch Thread从逻辑线程中划分,防止阻塞UI

时间:2014-05-20 11:31:43

标签: java multithreading swing timer

我如何将逻辑与EDT分开,因为你可以看到逻辑和渲染与循环相关 以及我如何以一种漂亮的方式分离代码,我'我遇到了麻烦,试图找出怎么做。

有任何建议/帮助/建议吗?

主要课程

public class MainFrame {

    private static MainFrame mainFrame = null;
    private final JFrame frame;

    private MainFrame() {
    frame = new JFrame();
    frame.setUndecorated(true);
    frame.add(new GamePanel());
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    }

    public static final MainFrame getMainFrameInstance() {
    if (mainFrame == null) {
        mainFrame = new MainFrame();
    }
    return mainFrame;
    }

    public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        try {// set look and feel to nimbus
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
        }
        MainFrame.getMainFrameInstance();
        }
    });
    }

}

我启动游戏循环的gamePanel类

public class MyPanel extends JPanel implements Runnable {

    private static final long serialVersionUID = 1L;

    // thread and loop
    private Thread thread;
    private boolean running;
    private int FPS = 60;
    private long targetTime = 1000 / FPS;
    private long start;
    private long elapsed;
    private long wait;

    // image
    public BufferedImage image;
    // foo
    private Foo foo;

    private Render render = Render.getRenderManagerInstance();

    public MyPanel() {
    setPreferredSize(new Dimension(700, 700));
    setFocusable(true);
    requestFocus();
    }

    public void addNotify() {
    super.addNotify();
    if (thread == null) {
        thread = new Thread(this);
        thread.start();
    }
    }


    private  void initGraphic() {
    image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    foo = new Foo();
    running = true;

    }

    public void run() {
    initGraphic();

    // loop
    while (running) {
        start = System.nanoTime();
        foo.update();
        repaint();
        elapsed = System.nanoTime() - start;
        wait = (targetTime - elapsed / 1000000) - 8;
        if (wait <= 0)
        wait = 6;

        try {
        Thread.sleep(wait);
        } catch (Exception e) {
        e.printStackTrace();
        }

    }
    }

    @Override
    public void paintComponent(Graphics graphics) {
    super.paintComponent(graphics);
    graphics = (Graphics2D) image.getGraphics();
    ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    render.setRenderState((Graphics2D) graphics);
    }

1 个答案:

答案 0 :(得分:3)

你的gamePanel不应该进入无限循环。它应该初始化自己并定义一个由Swing timer定期调用的方法。更一般地说,每当你定期做某事时,你不应该试着跟踪时间,而应该定义在一个步骤中应该做什么并让计时器给你打电话。这样可以更好地利用系统资源,是一种很好的工程实践。