重绘()不会被调用Java中的while循环 [英] Repaint() is not being called in Java while loop

查看:170
本文介绍了重绘()不会被调用Java中的while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Java创建一个简单的动画,显示一个蓝色球穿过500×500的窗口水平移动一部分的方式。球是应该在1像素/ 30毫秒的速率移动。问题是,是,当while循环退出,而不是while循环的每个迭代中,我打算窗口只被画。这导致蓝色球被涂在其最终位置。你能告诉我,我做错了什么吗?我也试着执行上的的paintComponent()方法EDT这个code和得到了同样的结果。此外,其他职位的建议使用paintImmediately(0,0,的getWidth()的getHeight()),而不是重绘()时,同时使用EDT和的paintComponent()方法,我得到了相同的结果。我试图做到这一切,而不使用计时器。

 进口的javax.swing *。
进口java.awt中的*。类AnimationFrame继承JPanel {    INT ovalX = 50;
    长animDuration = 5000;
    长currentTime的= System.nanoTime()/ 1000000;
    长STARTTIME = currentTime的;
    长ela​​psedTime = currentTime的 - 的startTime;    公共AnimationFrame(){
        集preferredSize(新尺寸(500,500));
        runAnimation();
    }    公共无效runAnimation(){
        而(elapsedTime< animDuration){
            currentTime的= System.nanoTime()/ 1000000;
            elapsedTime = currentTime的 - 的startTime;
            的System.out.println(elapsedTime);
            ovalX = ovalX + 1;
            尝试{
                视频下载(30);
            }
            赶上(例外五){
            }
            重绘();
        }
    }    公共无效漆(图形G){
        矩形夹= 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);
    }    公共静态无效的主要(字串[] args){
        createAndShowGUI();
    }    公共静态无效createAndShowGUI(){
        JFrame中的mainFrame =新的JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainFrame.add(新AnimationFrame());
        mainFrame.pack();
        mainFrame.setVisible(真);
    }
}


解决方案

我看着你的code,并注意到你调用从AnimationFrame的构造其中添加内运行动画的方法你的的mainFrame。

通过做这种方式的问题是,你正试图动画的对象已完成正在兴建之前,才可以添加到大型机,这还没有做出要在屏幕上可见必须完成。

我所做的后续更改您的code和我现在看到整个框架蓝色球移动。

 进口的javax.swing *。
进口java.awt中的*。类AnimationFrame继承JPanel {    INT ovalX = 50;
    长animDuration = 5000;
    长currentTime的= System.nanoTime()/ 1000000;
    长STARTTIME = currentTime的;
    长ela​​psedTime = currentTime的 - 的startTime;    公共AnimationFrame(){
        集preferredSize(新尺寸(500,500));
        //我删除调用runAnimation从这里    }    公共无效runAnimation(){
        而(elapsedTime< animDuration){
            currentTime的= System.nanoTime()/ 1000000;
            elapsedTime = currentTime的 - 的startTime;
            的System.out.println(elapsedTime);
            ovalX = ovalX + 1;
            尝试{
                视频下载(30);
            }
            赶上(例外五){
            }
            重绘();
        }
    }    @覆盖
    公共无效漆(图形G){
        矩形夹= 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);
    }    公共静态无效的主要(字串[] args){
        createAndShowGUI();
    }    公共静态无效createAndShowGUI(){
        JFrame中的mainFrame =新的JFrame();
        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        AnimationFrame animationPanel =新AnimationFrame();
        mainFrame.add(animationPanel);
        mainFrame.pack();
        mainFrame.setVisible(真);
        //我现在发出的呼吁,runAnimation这里
        //含帧后可见。
        animationPanel.runAnimation();
    }
}

I'm attempting to create a simple animation in Java that displays a blue ball moving horizontally part way across a 500 x 500 window. The ball is supposed to move at a rate of 1px/30ms. The problem is, is that the window is only being painted when the while loop exits and not during every iteration of the while loop as I intended. This results in the blue ball being painted in its final location. Could you please tell me what I'm doing wrong here? I've also tried executing this code on the EDT with the paintComponent() method and got the same result. In addition, as suggested by other posts, I got the same result when using paintImmediately(0, 0, getWidth(), getHeight()) instead of repaint() while using the EDT and paintComponent() method. I'm trying to do all of this without using timers.

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);
    }
}

解决方案

I looked at your code and noticed that you are calling the method that runs the animation from within the constructor of the "AnimationFrame" which you are adding to your "mainFrame".

The problems with doing it that way is that you are trying to animate before the object has finished being constructed, which must be completed before it can be added to the mainFrame, which is not yet made to be visible on the screen.

I made the follow changes to your code and I see a blue ball move across the frame now.

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();
    }
}

这篇关于重绘()不会被调用Java中的while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆