SwingWorker gui repaint

你。 提交于 2019-12-11 13:52:52

问题


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

来源:https://stackoverflow.com/questions/7939743/swingworker-gui-repaint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!