简体   繁体   中英

java repaint does not works inside while lop works in end of loop

After execution, the loop completes its circles and then the repaint method is being called,

Is there an alternative way of drawing the image?

public class GameCanvas extends JPanel implements KeyListener {

    private Tank tank1;
    private Tank tank2;
    private Rocket rocket;
    public Graphics2D g2;
    private Image img;
    public boolean fire;

    public GameCanvas() {
        tank1 = new Tank(0, 0);
        tank2 = new Tank(500, 500);
        rocket = new Rocket(50, tank1);
        init();
        fire = false;
    }

    public void init() {
        JFrame frame = new JFrame("Topak Tank");
        frame.setLayout(new FlowLayout());
        frame.setSize(1300, 740);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        this.setPreferredSize(new Dimension(1300, 740));
        frame.add(this);

        frame.addKeyListener(tank1);
        frame.addKeyListener(this);
    }

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        tank1.draw(g);
        // rocket.draw(g);

        g2 = (Graphics2D) g;

        if (fire == true) {
            try {
                img = ImageIO.read(getClass().getResource("rocket.png"));
                System.out.println(rocket.getYPos());
                g2.drawImage(img, rocket.getXPos(), rocket.getYPos(), null);
            } catch (Exception e) {
            }

        }
        repaint();
    }

    public void keyPressed(KeyEvent e) {

        switch (e.getKeyCode()) {
        case KeyEvent.VK_F: {
            try {
                int xPos = tank1.getXAxis();
                int yPos = tank1.getYAxis();
                fire = true;
                int delay = 10000;
                // img = ImageIO.read(getClass().getResource("rocket.png"));
                int count = 0;
                while (true) {
                    System.out.println(xPos);
                    System.out.println(yPos);
                    if (count == 5) {
                        break;
                    }
                    count++;
                    // g2.drawImage(img,xPos,yPos,null);

                    rocket.setYPos(yPos);

                    System.out.println("loop");

                    Timer t = new Timer(delay, null);
                    t.start();
                    yPos++;

                    this.repaint();// here i want to excute and draw image after every lop cycle
                    Thread.sleep(100);
                }
                System.out.println("Fired");
                fire = false;
            } catch (Exception io) {
                JOptionPane.showMessageDialog(null, "Could not found Rocket image");
            }

        }
            break;
        }

    }

    public void keyReleased(KeyEvent ke) {
    }

    public void keyTyped(KeyEvent ke) {
    }

    public static void main(String[] arg) {
        GameCanvas gc = new GameCanvas();
    }

}

Calling repaint() just tells the component to repaint itself whenever the EDT is available again. Since that entire loop is on the EDT, the component can't repaint itself until it's done.

You should probably be using a Timer or a Thread instead of tying up the EDT with that loop. You should NOT be calling sleep() on the EDT.

More info here: http://docs.oracle.com/javase/tutorial/uiswing/concurrency/

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