Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Start by trying to call <code>repaint</code> to request that the panel should be repainted...</p> <pre><code>public void spawnEnemy(int type, int amount) { aliens = new ArrayList(); for (int i = 1; i &lt; amount + 1; i++) { aliens.add(new Enemy(0 - i * 40, 400)); System.out.println("Attack Message Recieved " + i); } repaint() // &lt;&lt;-- } </code></pre> <p>Also, it is recommended that should be overriding <code>paintComponent</code> instead of <code>paint</code></p> <p><strong>Updated</strong></p> <p>Don't dispose of any resource you didn't create...</p> <pre><code>public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; // g2d.drawImage(background, 0, 0, null); if (player.isVisible()) { g2d.drawImage(player.getImage(), player.getX(), player.getY(), this); } ArrayList ms = player.getMissiles(); g2d.setColor(Color.BLACK); g2d.drawString("BattleView: ", 5, 15); Toolkit.getDefaultToolkit().sync(); // This is a bad idea.. //g.dispose(); } </code></pre> <p><code>Graphics</code> is a shared resource, meaning that other components in your application may need to use them. Also disposing may prevent Swing from begin able to paint anything at all...</p> <p><strong>Updated</strong></p> <p>You define a <code>ArrayList</code> as an instance field, but you never initialise until the <code>spawnEnemy</code> method is called. <code>paint</code> is likely to called a long time before that.</p> <pre><code>public class BattleView extends JPanel implements ActionListener { //...// private ArrayList aliens; //..// public void spawnEnemy(int type, int amount) { aliens = new ArrayList(); </code></pre> <p>You have a number of options. You could check for to see if the <code>aliens</code> list is <code>null</code> before painting...</p> <pre><code>if (aliens != null) { ... } </code></pre> <p>But there is a problem here, what happens if you get more enemies? You could actually lose some (if <code>paint</code> isn't called before more updates arrive)</p> <p>Instead, you should create the <code>aliens</code> list in the constructor and simply update it as new enemies arrive...</p> <pre><code>public BattleView(Player player) { aliens = new ArrayList(); //...// } </code></pre> <p>Add new enemies to the list as they arrive...</p> <pre><code>public void spawnEnemy(int type, int amount) { for (int i=1; i&lt;amount+1; i++ ) { aliens.add(new Enemy(0-i*40,400)); System.out.println("Attack Message Recieved "+i); } repaint(); } </code></pre> <p>And then paint...</p> <pre><code>for (int i = 0; i &lt; aliens.size(); i++) { Enemy a = (Enemy)aliens.get(i); if (a.isVisible()) g2d.drawImage(a.getImage(), a.getX(), a.getY(), this); } </code></pre>
    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. This table or related slice is empty.
    1. 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