为什么paint()方法不执行update()或paint()方法?

时间:2010-11-09 17:21:43

标签: java awt doublebuffered

我遇到这个问题,当我执行 repaint()时,没有调用类中的 paint() update()方法。这是代码:

public class BufferedDisplay extends Canvas implements Runnable {

// Contains all the images in order, ordered from background to foreground
private ArrayList<ImageStruct> images;
// Tracks the last insert ID of the last image for a particular layer
private TreeMap<Integer, Integer> insertIDs;
// Image that holds the buffered Image
private Image offscreen;

public BufferedDisplay() {
    images = new ArrayList<ImageStruct>();
    insertIDs = new TreeMap<Integer, Integer>();
}

public void addImageStruct(ImageStruct is) {
    int layer = is.getLayer();
    // Index to insert the image at
    int index = -1;
    if(insertIDs.containsKey(layer)) {
        index = insertIDs.get(layer)+1;
        insertIDs.put(layer, index);
    }
    else {
        index = images.size();
        insertIDs.put(layer, index);
    }
    if(index>-1) {
        images.add(index, is);
    }
}

public void run() {
    try
    {
        while(true)
        {
            System.out.print("\nSleeping... ");
            System.out.print("ArraySize:"+images.size()+" ");
            Thread.sleep(1000);
            System.out.print("Slept. ");
            repaint();
        }
    }
    catch(Exception e)
    {
        System.out.println("Display Error: ");
        e.printStackTrace();
        System.exit(-1);
    }
}

// Overrides method so the background isn't automatically cleared
public void update( Graphics g )
{
    System.out.print("Updating... ");
    paint(g);
}

public void paint( Graphics g )
{
    System.out.print("Painting... ");
    if(offscreen == null)
        offscreen = createImage(getSize().width, getSize().height);
    Graphics buffer = offscreen.getGraphics();
    buffer.setClip(0,0,getSize().width, getSize().height);
    g.setColor(Color.white);
    paintImages(buffer);
    g.drawImage(offscreen, 0, 0, null);
    buffer.dispose();
}

public void paintImages( Graphics window )
{
    for(ImageStruct i : images) {
        i.draw(window);
    }
}
}

此课程在以下实施:

public class Game extends JPanel{
// A reference to the C4Game class
private C4Game game;
// A reference to the BufferedDisplay class
private BufferedDisplay display;
// The Image used to initialize the game board
private Image tile;
private int tileSize = 75;
private int tileLayer = 5;
// Thread that controls animation for the BufferedDisplay
Thread animation;

public Game(C4Game game) {
    this.game = game;
    display = new BufferedDisplay();
    try {
        tile = ImageIO.read(new File("img\\tile.png"));
    } catch (IOException e) {
        System.out.println("ERROR: ");
        e.printStackTrace();
    }
    ((Component)display).setFocusable(true);
    add(display);
    animation = new Thread(display);
    animation.start();
    initBoard();
}

public void initBoard() {
    for(int x = 0; x<game.numRows()*tileSize; x+=tileSize) {
        for(int y = 0; y<game.numCols()*tileSize; y+=tileSize)  {
            System.out.println("Placing " +x +" " +y +"...");
            display.addImageStruct(new ImageStruct(tile, tileLayer, x, y, tileSize, tileSize));
        }
    }
}
}

...然后在JFrame中实现。

public class TetraConnect extends JFrame{

    public TetraConnect() {
    super("TetraConnect", 800, 600);
    try {
        setIconImage(Toolkit.getDefaultToolkit().createImage("img/icon.png"));
        ms = new MainScreen(this);
        add(ms);
        ms.updateUI();
        C4Game c4g = new C4Game(5,6);
        Game g = new Game(c4g);
        add(g);
        g.updateUI();
    }
    catch(Exception e) {
        System.out.println("Init. Error: ");
        e.printStackTrace();
        System.exit(-1);
    }
}

运行时的输出是:

Slept.
Sleeping... Slept.
Sleeping... Slept.
Sleeping... Slept.

等等。我也无法在画布上看到任何图像(我假设它们从未被绘制过)。它似乎完全跳过了重绘方法;调试语句“正在更新...”和“重绘......”永远不会出现。然而,重绘似乎也在执行;循环重复没有问题。为什么重绘方法不调用paint()或update()方法?

2 个答案:

答案 0 :(得分:2)

正如@camickr在评论中指出的那样,你正在使用重量级的AWT画布。你应该真正使用轻量级Swing组件。而不是:

public class BufferedDisplay extends Canvas implements Runnable {

我建议:

 public class BufferedDisplay extends JPanel implements Runnable {

鉴于这一小变化,我会做以下事情:

当覆盖组件的默认痛苦时,您应该覆盖paintComponent()方法。

所以,而不是:

public void paint( Graphics g )
{

应该是:

protected void paintComponent( Graphics g )
{

这可能会解决您的问题。

此外,您不必覆盖update()方法。相反,只需在paint组件方法中忽略对super.paintCompenent(g)的调用。这应该会导致背景默认保持不变。

答案 1 :(得分:1)

确保已将BufferedDisplay对象添加到容器(例如Frame),并且容器本身可见。如果组件未显示,则调用repaint()不会导致update()被调用。

这只是一般性建议。如果您发布可以编译并运行的self-contained example,则可能更容易找出错误。