簡體   English   中英

Java中的while循環未調用Repaint()

[英]Repaint() is not being called in Java while loop

我正在嘗試用Java創建一個簡單的動畫,該動畫顯示一個藍色的球在500 x 500的窗口中水平移動。 該球應該以1px / 30ms的速度移動。 問題是,僅在while循環退出時才繪制窗口,而不是在while循環的每次迭代中繪制窗口。 這導致藍色球在其最終位置被繪制。 您能告訴我我在做什么錯嗎? 我也嘗試使用paintComponent()方法在EDT上執行此代碼,並得到相同的結果。 此外,正如其他文章所建議的那樣,當使用EDT和paintComponent()方法使用paintImmediately(0,0,getWidth(),getHeight())而不是repaint()時,我得到了相同的結果。 我正在嘗試不使用計時器來完成所有這些操作。

import javax.swing.*;
import java.awt.*;

class AnimationFrame extends JPanel {

    int ovalX = 50;
    long animDuration = 5000;
    long currentTime = System.nanoTime() / 1000000;
    long startTime = currentTime;
    long elapsedTime = currentTime - startTime;

    public AnimationFrame() {
        setPreferredSize(new Dimension(500, 500));
        runAnimation();
    }

    public void runAnimation() {
        while (elapsedTime < animDuration) {
            currentTime = System.nanoTime() / 1000000;
            elapsedTime = currentTime - startTime;
            System.out.println(elapsedTime);
            ovalX = ovalX + 1;
            try {
                Thread.sleep(30);
            }
            catch (Exception e) {
            }
            repaint();
        }
    }

    public void paint(Graphics g) {
        Rectangle clip = g.getClipBounds();
        g.setColor(Color.BLACK);
        g.fillRect(clip.x, clip.y, clip.width, clip.height);
        g.setColor(Color.BLUE);
        g.fillOval(ovalX, 250, 70, 70);
    }

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

    public static void createAndShowGUI() {
        JFrame mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.add(new AnimationFrame());
        mainFrame.pack();
        mainFrame.setVisible(true);
    }
}

我查看了您的代碼,發現您正在從要添加到“ mainFrame”的“ AnimationFrame”的構造函數中調用運行動畫的方法。

這樣做的問題在於,您試圖在對象完成構造之前進行動畫處理,必須先完成動畫處理才能將其添加到尚未在屏幕上可見的mainFrame中。

我對您的代碼進行了以下更改,現在我看到一個藍色的球在框架上移動。

import javax.swing.*;
import java.awt.*;

class AnimationFrame extends JPanel {

    int ovalX = 50;
    long animDuration = 5000;
    long currentTime = System.nanoTime() / 1000000;
    long startTime = currentTime;
    long elapsedTime = currentTime - startTime;

    public AnimationFrame() {
        setPreferredSize(new Dimension(500, 500));
        //i removed the call to runAnimation from here

    }

    public void runAnimation() {
        while (elapsedTime < animDuration) {
            currentTime = System.nanoTime() / 1000000;
            elapsedTime = currentTime - startTime;
            System.out.println(elapsedTime);
            ovalX = ovalX + 1;
            try {
                Thread.sleep(30);
            }
            catch (Exception e) {
            }
            repaint();
        }
    }

    @Override
    public void paint(Graphics g) {
        Rectangle clip = g.getClipBounds();
        g.setColor(Color.BLACK);
        g.fillRect(clip.x, clip.y, clip.width, clip.height);
        g.setColor(Color.BLUE);
        g.fillOval(ovalX, 250, 70, 70);
    }

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

    public static void createAndShowGUI() {
        JFrame mainFrame = new JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        AnimationFrame animationPanel = new AnimationFrame();
        mainFrame.add(animationPanel);
        mainFrame.pack();
        mainFrame.setVisible(true);
        //I made the call to runAnimation here now
        //after the containing frame is visible.
        animationPanel.runAnimation();
    }
}

您需要在單獨的線程中執行循環。 參見本教程-http://101.lv/learn/Java/ch10.htm

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM