SwingWorker 图形用户界面重绘

发布于 2024-12-12 18:36:53 字数 1253 浏览 2 评论 0原文

我尝试使用 SwingWorker 来更新我的 gui,而不调用 repaint()。

我希望 SwigWorker 更新每个 GridGraphic[i][j] 的状态,并且让 gui 响应更改而无需调用 repaint()。

网格图形.java

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

public class GridGraphic extends JComponent {

    private int intensity = 0;

    public GridGraphic() {
        //setBorder(BorderFactory.createLineBorder(Color.BLUE));
    }

    public void paintComponent(Graphics g) {

        //paints the GridGraphic black
        if (intensity == 0){
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
        }

        //paints the GridGraphic black with a yellow dot
        else if (intensity == 5){
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.YELLOW);
            g.fillOval(3, 3, getWidth()/2, getHeight()/2);
        }

    }


    public Dimension getPreferredSize() {
        return new Dimension(10, 10);
    }

    public void setAgent(){
        intensity = 5;
    }

    public void setDefault(){
        intensity = 0;
    }

    public int getIntensity(){
        return intensity;
    }
}

Im trying to use the SwingWorker to update my gui without calling repaint().

I want the SwigWorker to update the status of each GridGraphic[i][j] and the have the gui be repsonsive to the changes without calling repaint().

GridGraphic.java

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

public class GridGraphic extends JComponent {

    private int intensity = 0;

    public GridGraphic() {
        //setBorder(BorderFactory.createLineBorder(Color.BLUE));
    }

    public void paintComponent(Graphics g) {

        //paints the GridGraphic black
        if (intensity == 0){
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
        }

        //paints the GridGraphic black with a yellow dot
        else if (intensity == 5){
            super.paintComponent(g);
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
            g.setColor(Color.YELLOW);
            g.fillOval(3, 3, getWidth()/2, getHeight()/2);
        }

    }


    public Dimension getPreferredSize() {
        return new Dimension(10, 10);
    }

    public void setAgent(){
        intensity = 5;
    }

    public void setDefault(){
        intensity = 0;
    }

    public int getIntensity(){
        return intensity;
    }
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文