更改 Java 图形对象的颜色 [英] Changing Color of a Java graphic object

查看:116
本文介绍了更改 Java 图形对象的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过更改颜色和调用 repaint() 方法来刷新 Java 图形对象.颜色仅随最终更改颜色调用而更新.这是我的代码:

I'm trying to flash a Java graphic object by changing the color and calling the repaint() method. The color is only updating with the final change color call. Here is my code:

public void start() {
    try {
        Color origColor = node.getColor();
        for (int i=0; i<noOfFlashes; i++) {
            Manager.gui.getDrawGraph().changeNodeColor(node, Color.WHITE);
            Thread.sleep(500);
            Manager.gui.getDrawGraph().changeNodeColor(node, origColor);
            Thread.sleep(500);
        }
        Manager.gui.getDrawGraph().changeNodeColor(node, Graph.VISITED_NODE);       
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

而改变节点颜色的方法是:

and the change node color method is:

public void changeNodeColor(Node node, Color c) {
    node.setColor(c);
    repaint();
}

更改节点颜色与绘制组件在同一类中.

The change node color is in the same class as the paint component.

任何帮助将不胜感激.

推荐答案

您需要使用单独的线程来管理您的 GUI 事件.您可以使用 SwingWorker 执行此操作,按照阿明的建议,或者实现 Runnable 接口,或扩展 Thread类,开发 run() 方法,这是您线程的任务.

You need to use separate thread to manage your GUI event. You can do this, using a SwingWorker, as suggested by Amine, or implement the Runnable interface, or extend the Thread class, developing the run() method, that is the task of your thread.

您可以阅读 SO 的这个老问题:我如何使用 SwingWorker爪哇?

You can read this old question of SO : How do I use SwingWorker in Java?

SwingWorker 教程:http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

A tutorial for SwingWorker : http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

制作线程的教程:http://docs.oracle.com/javase/tutorial/essential/concurrency/

颜色仅随最终更改颜色调用而更新.

The color is only updating with the final change color call.

如果您不使用单独的线程,您的 gui 将冻结,直到该方法完全执行为止,并且您将看不到由 Thread.sleep(500); 分隔的颜色变化.

If you don't use a separate thread, your gui will freezing until the method is completely executed, and you won't see the color change separated by Thread.sleep(500);.

更新

这个链接中,在为什么 Swing GUI 会冻结或锁定?,您可以理解为什么 Java Swing GUI 会冻结,使用单线程.

In this link, in the paragraph Why does a Swing GUI freeze or lock up?, you can understand why a Java Swing GUI freezes, with the use of a single thread.

另请查看官方链接,在段落创建线程,和这个 页面,返回:

Check also this official link, in the paragraph Creating Threads, and this page, that returns:

Swing 的单线程规则说 Swing 组件只能由单个线程访问.此规则适用于 get 和 set,单线程称为事件调度线程.

Swing's single-thread rule says that Swing components can only be accessed by a single thread. This rule applies to both gets and sets, and the single thread is known as the event-dispatch thread.

单线程规则非常适合 UI 组件,因为它们无论如何都倾向于以单线程方式使用,大多数操作由用户发起.此外,构建线程安全组件既困难又乏味:不做是件好事如果可以避免的话.但对于它的所有好处,单线程规则具有深远的影响.

The single-thread rule is a good match for UI components because they tend to be used in a single-threaded way anyway, with most actions being initiated by the user. Furthermore, building thread safe components is difficult and tedious: it's a good thing not to be doing if it can be avoided. But for all its benefits, the single-thread rule has far-reaching implications.

Swing 组件一般不会遵守单线程规则除非他们的所有事件都在事件调度中发送和接收线.例如,属性更改事件应该在事件调度线程和模型更改事件应该在事件调度线程.

Swing components will generally not comply with the single-thread rule unless all their events are sent and received on the event-dispatch thread. For example, property-change events should be sent on the event-dispatch thread, and model-change events should be received on the event-dispatch thread.

对于基于模型的组件,例如 JTable 和 JTree,单线程规则意味着模型本身只能由事件调度线程.为此,模型的方法必须快速执行,不应阻塞,或整个用户界面将无响应.

For model-based components such as JTable and JTree, the single-thread rule implies that the model itself can only be accessed by the event-dispatch thread. For this reason, the model's methods must execute quickly and should never block, or the entire user interface will be unresponsive.

我认为上面的句子对于更好地理解 Swing 包非常有用.

I think that the sentences above are very useful to understand better the Swing package.

我报告了垃圾神的建议.

I report the suggestion of trashgod.

您可以使用 javax.swing.Timer 包中的 Timer 类.这也是一个不错的选择.

You can use the Timer class, from the javax.swing.Timer package. That is also a good alternative.

这个问题,trashgod报告了一些Timer的例子.

In this question, trashgod reports some examples of Timer.

此处查看有关的教程定时器.

这篇关于更改 Java 图形对象的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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