Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is the paint() method not executing update() or paint() methods?
    primarykey
    data
    text
    <p>I'm having this problem where the <em>paint()</em> or <em>update()</em> methods in a class isn't getting called when I execute <em>repaint()</em>. Here's the code:</p> <pre><code>public class BufferedDisplay extends Canvas implements Runnable { // Contains all the images in order, ordered from background to foreground private ArrayList&lt;ImageStruct&gt; images; // Tracks the last insert ID of the last image for a particular layer private TreeMap&lt;Integer, Integer&gt; insertIDs; // Image that holds the buffered Image private Image offscreen; public BufferedDisplay() { images = new ArrayList&lt;ImageStruct&gt;(); insertIDs = new TreeMap&lt;Integer, Integer&gt;(); } 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&gt;-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); } } } </code></pre> <p>This class is implemented in this:</p> <pre><code>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&lt;game.numRows()*tileSize; x+=tileSize) { for(int y = 0; y&lt;game.numCols()*tileSize; y+=tileSize) { System.out.println("Placing " +x +" " +y +"..."); display.addImageStruct(new ImageStruct(tile, tileLayer, x, y, tileSize, tileSize)); } } } } </code></pre> <p>...Which is then implemented in a JFrame.</p> <pre><code>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); } } </code></pre> <p>The output when I run it, is:</p> <pre><code>Slept. Sleeping... Slept. Sleeping... Slept. Sleeping... Slept. </code></pre> <p>And so forth. I'm also not able to see any images on the Canvas (I'm assuming they are never drawn in the first place). It seems to completely skip the repaint method; the debug statements "Updating... " and "Repainting... " never show up. However, repaint also seems to be executing; the loop repeats without problems. Why isn't the repaint method calling the paint() or update() methods?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload