1

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.

などなど。また、Canvasに画像が表示されません(そもそも画像が描画されていないことを前提としています)。repaintメソッドを完全にスキップしているようです。デバッグステートメント「Updating...」および「Repainting...」は表示されません。ただし、再描画も実行されているようです。ループは問題なく繰り返されます。repaintメソッドがpaint()またはupdate()メソッドを呼び出さないのはなぜですか?

4

2 に答える 2

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()メソッドをオーバーライドする必要はありません。super.paintCompenent(g)代わりに、ペイント コンポーネント メソッド内のへの呼び出しを省略します。これにより、デフォルトで背景がそのまま残されます。

于 2010-11-09T17:26:36.897 に答える
1

BufferedDisplayオブジェクトがコンテナー (例: ) に追加されているFrameこと、およびコンテナー自体が表示されていることを確認してください。コンポーネントが表示されていない場合、 への呼び出しは呼び出されrepaint()ませんupdate()

これは単なる一般的なアドバイスです。コンパイルして実行できる自己完結型の例を投稿すると、おそらく何が問題なのかを簡単に見つけることができます。

于 2010-11-09T18:22:05.910 に答える