java - SWT中的动态颜色应该如何分配和处理?

标签 java colors swt

有时我需要在 SWT 中动态生成颜色。以一个 View 为例,该 View 以红-绿的不同颜色显示 0-1 之间的分数。可能还有其他颜色不同的类似 View :

class ScoreView {
    private Canvas canvas;
    private List<Item> items = new ArrayList<Item>();

    public ScoreView(Composite parent) {
        canvas = new Canvas(parent, SWT.NONE);

        canvas.addPaintListener(new PaintListener() {
            public void paintControl(PaintEvent event) {
                GC gc = event.gc;

                int x = 0;
                for(Item item:items) {
                    int shade = (int)(item.getScore() * 255.0);

                    Color color = new Color(Display.getCurrent(), shade, 255-shade, 0);
                    gc.setBackground(color);
                    gc.fillRectangle(x, 0, 50, 50);
                    x += 50;
                }
            }
        });
    }

    public void setItems(List<Item> items) {
        this.items = items;
        canvas.redraw();
    }
}

在 SWT 中,您应该始终释放资源。来自 http://www.eclipse.org/articles/Article-SWT-Color-Model/swt-color-model.htm :

Colors contain OS resources that must be allocated and freed. For this reason, it is necessary to dispose every color that you have created.

那么处理颜色的最佳方法是什么?

来自 http://www.eclipse.org/articles/swt-design-2/swt-design-2.html :

If you are using graphics resources in a widget - for example, widget.setFont(font) - it is often best to clean these up when the widget they are used in is disposed, so you can hook a dispose listener on the widget...

这样我就可以跟踪我分配的每种颜色,并将它们放置在 Canvas 或外壳上的 DisposeListener 中。但是来自同一个链接:

The operating system frees all of a program's resources when the program exits. Why not just rely on this? Operating system resources are not infinite. If your program doesn't free up resources as they are no longer needed, it can run out of resources. It can also cause other programs to run out of resources. So waiting until the program exits to free up resources is generally a bad idea.

看起来我没有得到任何东西,因为即使颜色被处置,它们也会在退出时被处置( View 持续程序的生命周期)。

我认为我不能直接在 PaintListener 中处理颜色,因为它们在显示时仍被 GC 使用,所以我是否必须记住什么颜色正在使用并在下一次绘制调用中处理它们(如果它们在该调用中未再次使用)?

如果我做到了这一点,如果只有几种颜色可用,还有另一个问题。由于颜色分配的顺序,我可以渲染许多红色阴影,然后用完所有颜色,并且无法显示任何绿色阴影。因此,在那种情况下,在程序开始时预先分配我可能需要的一系列颜色(如调色板)会更好吗?我应该分配多少,按什么顺序分配?这意味着永远不要丢弃这些颜色。

还有,有什么办法可以随时知道有多少种颜色可供选择?其他正在运行的程序是否会带走可用颜色的数量?

最佳答案

Eclipse RCP 通过全局 ColorRegistry 处理这个问题.

通过使用它,您可以避免在应用程序启动时进行资源预分配。 当您需要资源时,您可以查询全局注册表。 如果它不可用,您将其分配并注册到全局注册表。

private static final String SHADE = "shadedcolor";

Color color = null;
if (!JFaceResources.getColorRegistry().hasValueFor(SHADE)) {
    color = new Color(Display.getCurrent(), shade, 255-shade, 0);
    JFaceResources.getColorRegistry().put(SHADE, color);
} else {
    color = JFaceResources.getColorRegistry().get(SHADE);
}

资源将在应用程序退出时释放。

关于java - SWT中的动态颜色应该如何分配和处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13409687/

相关文章:

java - AWS Lambda 中的相互身份验证(2 路 SSL)

java - 读取 JavaFX Canvas 像素的最佳方法是什么?

java - 如何在单击按钮时将焦点设置在 jface 中的特定选项卡上

java - 如何生成电话簿的可打印输出

java - 带有 ScrolledComposite 的 Jface 对话框

java - Restful api GET 请求出现 504 错误的原因是什么?

java - 在运行时编辑类

java - 根据图像修改背景颜色(主题)

java - Android:java TextView颜色

java - 在java中的HTTP请求中生成PDF作为输出流