简体   繁体   English

暂停图形?

[英]Pause Graphics?

So I have this JPanel Graphics code: 所以我有这个JPanel Graphics代码:

public void paint(Graphics g){
    super.paint(g);

    for(int y=0 ;y < 50; y++){
        for(int x = 0; x < 50; x++){
            if(m.getMaze(x, y).equals("g")){
                g.drawImage(m.getGround(), x * 16, y * 16,null);
            }
            if(m.getMaze(x, y).equals("w")){
                g.drawImage(m.getWall(), x * 16, y * 16,null);
            }
            if(m.getMaze(x, y).equals("S")){
                g.drawImage(m.getStart(), x * 16, y * 16,null);
            }
            if(m.getMaze(x, y).equals("E")){
                g.drawImage(m.getEnd(), x * 16, y * 16,null);
            }
        }
    }
}

And inside the for loop (the second one) I would like to pause it for half a second, so you can see it drawing each tile. 在for循环(第二个循环)中,我想将其暂停半秒,以便可以看到它绘制了每个图块。 The thing is, is when I use 事情是,当我使用

Thread.sleep(500);

After the second for loop, it just stops the whole thing forever. 在第二个for循环之后,它将永远停止整个过程。 If I use 如果我用

g.wait(500);

it keeps spamming 它一直垃圾邮件

java.lang.IllegalMonitorStateException

in the console. 在控制台中。 And yes, it is surrounded with try/catch. 是的,它周围有try / catch。 How can I pause this? 我该如何暂停?

On a swing Timer call repaint(50L, x*16, y*16, 16, 16); 在摆动计时器上调用repaint(50L, x*16, y*16, 16, 16); . Use fields for the current state ( x , y ), 使用字段表示当前状态( xy ),

and on every paintComponent draw only that state/tile. 并且在每个paintComponent上仅绘制该状态/平铺。

1) use paintComponent() method for custom paintings intead of paint() . 1)将paintComponent()方法用于来自paint()自定义绘画。 Read about custom paintings in java 了解有关Java中的自定义绘画的信息

2) Don't block EDT with Thread.sleep(500); 2)不要使用Thread.sleep(500);阻止EDT Thread.sleep(500); or g.wait(500); g.wait(500); .

Seems you need to use swing Timer for loading. 似乎您需要使用swing Timer进行加载。 Here is simple example: 这是简单的示例:

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class TestFrame extends JFrame {

    private Timer timer;

    public TestFrame() {
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void init() {
        final DrawPanel p = new DrawPanel();
        timer = new Timer(500, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if(p.drawingTiles < 16){
                    p.addDrawingTile();
                    p.repaint();
                } else {
                    timer.stop();
                    System.out.println("done");
                }
            }
        });
        timer.start();
        add(p);
    }

    public static void main(String args[]) {
        new TestFrame();
    }

    private class DrawPanel extends JPanel{

        protected int drawingTiles = 0;

        private int tiles[][] = new int[4][4];

        public DrawPanel(){
            Random r = new Random();
            for(int i=0;i<tiles.length;i++){
                for(int j=0;j<tiles.length;j++){
                    tiles[i][j] = r.nextInt(5);
                }
            }
        }

        public void addDrawingTile() {
            drawingTiles++;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int curTiles = 0;
            for(int i=0;i<tiles.length;i++){
                for(int j=0;j<tiles.length;j++){
                    if(curTiles <drawingTiles){
                        curTiles++;
                        g.drawString(tiles[i][j]+"", (j+1)*10, (i+1)*10);
                    }
                }
            }
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(50,50);
        }

    }
}

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

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