SwingWorker gui重画

时间:2011-10-29 15:18:19

标签: swing generics user-interface swingworker

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

我希望SwigWorker更新每个GridGraphic [i] [j]的状态,并让gui对更改进行反复调用而不调用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;
    }
}

0 个答案:

没有答案