Java应用程序:重绘未在While循环中执行?

发布于 2024-11-25 08:26:03 字数 1929 浏览 1 评论 0原文

我有这个Thread,它应该更新游戏的元素,即重新绘制,等待一段时间然后再做一次,问题是它不重新绘制。我已经测试了所有其他组件,它们都有效,但 Paint 方法仅被调用一次(因为组件已被绘制),即使我在循环中调用 repaint() 也是如此。这是我的循环代码:

Thread t = new Thread(){
public void run()
    {
        mouse.init();
        while(true)
        {                                       
         mouse.Refresh();//Adds Dirty Regions in the RepaintManager   
        SwingUtilities.invokeLater(new Runnable() {    
       @Override 
       public void run() {              
          //What here? 
       }
    });


        }            
    }
};

不需要看到线程或任何东西,它会循环。

这是绘画方法:

   @Override
public void paint(Graphics g)
{

            EndTime = System.currentTimeMillis();//For the FPS Counter
    Time = EndTime - StartTime;
    FPS = (byte) (1000/Time);   
    TotalFPS += FPS;
    TotalFrame++;   
            JPT.AverageFPs.setText( "" + TotalFPS/TotalFrame);
            JPT.CurrentFPS.setText( "" + FPS);
    StartTime = System.currentTimeMillis();

    g.clearRect(0,0,dim.width,dim.width); 


    for(int x = 0; x < Variables.Width; x++)
    {
        for(int y = 0; y < Variables.Height; y++)
        {
            if(Variables.Map[x][y] == 0)
            { 
                g.setColor(new Color(0x613F37));
                g.drawLine(x, y, x, y);
            }
            else if(Variables.Map[x][y] == 1)
            { 
                g.setColor(Color.black);
                g.drawLine(x, y, x, y);
            }
            else if(Variables.Map[x][y] == 2)
            { 
                g.setColor(new Color(0xDEDEDE));
                g.drawLine(x, y, x, y);
            }

        }
    }

        g.setColor( new Color(0.5f, 0.5f, 0.5f, 0.5f));
        g.fillRect(Variables.CurrentX, Variables.CurrentY, Variables.Zoom, Variables.Zoom);





}

提前非常感谢。

另外我想指出的是,我之前将这个游戏作为一个小程序制作,它的工作就像一个魅力,但现在我需要它作为应用程序。

I've got this Thread that is supposed to Update the game's elements, i.e. repaint, wait for a bit of time then do it again, the problem is that it doesn't repaint. I've tested every other component, they all works, but the paint method is only called once ( Because the components are painted ), even if I call repaint() in the loop. Here's my Code of the Loop:

Thread t = new Thread(){
public void run()
    {
        mouse.init();
        while(true)
        {                                       
         mouse.Refresh();//Adds Dirty Regions in the RepaintManager   
        SwingUtilities.invokeLater(new Runnable() {    
       @Override 
       public void run() {              
          //What here? 
       }
    });


        }            
    }
};

No need to see the thread or anything, it loops.

Here's the Paint Method :

   @Override
public void paint(Graphics g)
{

            EndTime = System.currentTimeMillis();//For the FPS Counter
    Time = EndTime - StartTime;
    FPS = (byte) (1000/Time);   
    TotalFPS += FPS;
    TotalFrame++;   
            JPT.AverageFPs.setText( "" + TotalFPS/TotalFrame);
            JPT.CurrentFPS.setText( "" + FPS);
    StartTime = System.currentTimeMillis();

    g.clearRect(0,0,dim.width,dim.width); 


    for(int x = 0; x < Variables.Width; x++)
    {
        for(int y = 0; y < Variables.Height; y++)
        {
            if(Variables.Map[x][y] == 0)
            { 
                g.setColor(new Color(0x613F37));
                g.drawLine(x, y, x, y);
            }
            else if(Variables.Map[x][y] == 1)
            { 
                g.setColor(Color.black);
                g.drawLine(x, y, x, y);
            }
            else if(Variables.Map[x][y] == 2)
            { 
                g.setColor(new Color(0xDEDEDE));
                g.drawLine(x, y, x, y);
            }

        }
    }

        g.setColor( new Color(0.5f, 0.5f, 0.5f, 0.5f));
        g.fillRect(Variables.CurrentX, Variables.CurrentY, Variables.Zoom, Variables.Zoom);





}

Thanks alot in advance.

Also I want to point out that I made this Game as an Applet before and it was working like a charm, but now I need it as Application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赏烟花じ飞满天 2024-12-02 08:26:03

尝试立即绘制(0, 0, getWidth(), getHeight());

Try paintImmediately(0, 0, getWidth(), getHeight());

一张白纸 2024-12-02 08:26:03

这是因为 RepaintManager 将多个请求折叠成对组件树成员的一次重绘。

This is because the RepaintManager collapses multiple requests into a single repaint for members of a component tree.

伤痕我心 2024-12-02 08:26:03

我认为您的数据在线程之间不同步(没有足够的代码来确定这种情况是否困难)。

repaint 在 Swing EDT 上调用,update.UpdateGravity() 在主线程中调用。我猜您的 update 更改了 Variable,但是您是否同步过此数据结构以便 Swing EDT 看到更新后的值?

编辑

这是您应该做的:

variables = update.updateGravity();  // runs in your main thread
SwingUtilities.invokeLater(new Runnable() {    
   @Override public void run() {
       yourCustomSwingComponent.update(variables);   // will run on the Swing EDT
   }
}

yourCustomSwingComponent 包含您拥有的 paint() 方法。 Swing 中有一条规则,任何 Swing 组件的状态只能从 Swing EDT 中修改(或读取)。

您应该阅读有关 Swing 和多线程的内容。实际上,您必须阅读有关多线程的内容才能使用 Swing。

I think your data is not synchronized between threads (there is not enough code to determine if this the case tough).

repaint is called on the Swing EDT and update.UpdateGravity() is called in your main thread. I guess your update changes Variable, but do you ever synchronize this data structure such that the Swing EDT sees the updated value?

EDIT:

here is what you should do:

variables = update.updateGravity();  // runs in your main thread
SwingUtilities.invokeLater(new Runnable() {    
   @Override public void run() {
       yourCustomSwingComponent.update(variables);   // will run on the Swing EDT
   }
}

and yourCustomSwingComponent contains the paint() method you have. There is a rule in Swing that the state of any Swing component can only be modified (or read) from the Swing EDT.

You should read about Swing and multi-threading. Actually, you must read about multi-threading to use Swing.

茶花眉 2024-12-02 08:26:03

不直接回答这个问题,但查看一些现有的 Java2D 动画库可能非常有用。例如,Trident 最初由 基里尔·格鲁奇尼科夫。如果有理由不使用它,至少检查来源并借鉴一些想法可能会有用。

另外, Swing Timer 对于实现基于时间的可能会有一些兴趣摇摆相关动作。

Not answering the question directly, but it might be very useful to have a look at some of the existing Java2D animation libraries. For example, Trident originally developed by Kirill Grouchnikov. If there is a reason not to use it, at least it could be useful to inspect the source and borrow some ideas.

Also, Swing Timer could be of some interest for implementing time based Swing related actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文