简体   繁体   中英

Pause Graphics?

So I have this JPanel Graphics code:

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. The thing is, is when I use

Thread.sleep(500);

After the second for loop, it just stops the whole thing forever. If I use

g.wait(500);

it keeps spamming

java.lang.IllegalMonitorStateException

in the console. And yes, it is surrounded with try/catch. How can I pause this?

On a swing Timer call repaint(50L, x*16, y*16, 16, 16); . Use fields for the current state ( x , y ),

and on every paintComponent draw only that state/tile.

1) use paintComponent() method for custom paintings intead of paint() . Read about custom paintings in java

2) Don't block EDT with Thread.sleep(500); or g.wait(500); .

Seems you need to use swing Timer for loading. 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);
        }

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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