Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your application hangs due to the <code>repaint()</code> call in the <code>paint()</code> method. It is a recursive call, as <code>repaint()</code> calls <code>paint()</code>.</p> <p>You need to remove it, and your application won't hang anymore.</p> <p><hr> If you simply remove the <code>repaint()</code> call from the <code>paint()</code> method, your graphics won't be drawn, because you are overriding the <code>paint()</code> method without calling the superclass one. It should be like this:</p> <pre><code>@Override public void paint(Graphics g){ super.paint(g); g.drawImage(sprite, 100, 100, null); } </code></pre> <p>However, it will not display the image, because you call the <code>init()</code> method after setting it visible. Call it before setting the frame visible and it will work.</p> <p>However, this is not the correct way to put a background image in your <code>JFrame</code>, you should use a <code>JPanel</code> instead:</p> <pre><code>class ImagePanel extends JPanel { private Image image; int x, y; public ImagePanel(Image image, int x, int y) { this.image = image; this.x = x; this.y = y; } @Override protected void paintComponent(Graphics g) { g.drawImage(image, x, y, null); } } </code></pre> <p>To call it:</p> <pre><code>SpriteSheet ss = new SpriteSheet(spriteSheet); sprite = ss.grabSprite(0, 0, 20, 32); ImagePanel backgroundImagePanel = new ImagePanel(sprite, 100, 100); setContentPane(backgroundImagePanel); </code></pre> <p>Also, Remove the <code>paint()</code> override to the <code>JFrame</code> and call the <code>init()</code> method <strong>before</strong> setting the frame visible</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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