首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么update()方法不执行update()或update()方法?

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

Stack Overflow用户
提问于 2010-11-09 17:21:42
回答 2查看 2.9K关注 0票数 1

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

代码语言:javascript
复制
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);
    }
}
}

这个类在以下内容中实现:

代码语言:javascript
复制
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));
        }
    }
}
}

然后,...Which在JFrame中实现。

代码语言:javascript
复制
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);
    }
}

当我运行它时,输出是:

代码语言:javascript
复制
Slept.
Sleeping... Slept.
Sleeping... Slept.
Sleeping... Slept.

以此类推。我也无法在画布上看到任何图像(我假设它们从一开始就没有绘制过)。它似乎完全跳过了重新绘制方法;调试语句“更新.”和“重新油漆.”永远不要出现。然而,重绘似乎也在执行;循环没有问题地重复。为什么repaint方法不调用update()或update()方法?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-09 17:26:36

正如@camickr在评论中所指出的,您使用的是重量级AWT画布。您确实应该使用轻量级的Swing组件。而不是:

代码语言:javascript
复制
public class BufferedDisplay extends Canvas implements Runnable {

我建议:

代码语言:javascript
复制
 public class BufferedDisplay extends JPanel implements Runnable {

在这小小的改变之后,我会做以下几件事:

在重写组件的默认绘图时,应该重写paintComponent()方法。

因此,与其:

代码语言:javascript
复制
public void paint( Graphics g )
{

它应该是:

代码语言:javascript
复制
protected void paintComponent( Graphics g )
{

这可能会解决你的问题。

此外,您不应该必须重写update()方法。相反,只需在paint组件方法中省略对super.paintCompenent(g)的调用。默认情况下,这应该会导致背景被单独保留。

票数 2
EN

Stack Overflow用户

发布于 2010-11-09 18:22:05

确保BufferedDisplay对象已经添加到容器中(例如,Frame),并且容器本身是可见的。如果组件没有显示,那么对repaint()的调用将不会导致调用update()

这只是一般性的建议。如果您发布了一个可以编译和运行的自给示例,那么可能更容易找出问题所在。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4136535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档