hi

I'm writing a small game and need to pass an image into an ArrayList. The Image and bounds are set in Attacker Class with an ArrayList partially constructed in my GameScrn Class posted below. Have spent 2 days on this one issue and failing badly...guessing as usual, im missing something really simple.

any help please...

package castledefender;

import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JComponent;

/**
 *
 * @author paul
 */
public class GameScrn extends JComponent implements Runnable
{


   public int lastx = 0;
   public int lasty = 0;
   //place images into icons
   private ImageIcon castle = new ImageIcon(getClass().getResource("background.jpg"));
   private ImageIcon turret = new ImageIcon(getClass().getResource("turret1.gif"));
   private Defender def = new Defender(this);
  // private Attacker att = new Attacker(this);
   private Missile miss = new Missile(this);
   private Thread animation = new Thread(this);
   public ArrayList<Attacker> atk = new ArrayList<Attacker>();

   GameScrn()
   {
   Object[] ol = atk.toArray();
   System.out.println(ol);
      animation.start();
   }

   public void run()
   {
      while (true)
      {
         repaint();
         try
         {
            Thread.sleep(30);
         }
         catch (InterruptedException ex)
         {
         }
      }
   }

   @Override
   public void paint(Graphics g)
   {
      g.drawImage(castle.getImage(), 0, 0, this);//draw background image
      def.draw(g);//draw defender
      miss.draw(g);//draw missile
      g.drawImage(turret.getImage(), 0, 11, this);//set position of gif image on top of defender and castle
      //att.add(new Attacker(this));
      System.out.println("contents of att = " + atk);
     // att.draw(g);//draw attacker
      atk.add(new Attacker(this));

   }

   public void moveDef(int hMove, int vMove)
   {
      def.moveDef(hMove, vMove);
      //miss.moveMiss(hMove, vMove);
   }

   public void moveAtk(int hMove, int vMove)
   {


   }

   public void moveMiss(int hMove, int vMove)
   {
     miss.moveMiss(hMove, vMove);
      System.out.println("Vmove" + vMove);
   }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

1/ for any animations in Swing (JComponent) is required use of java.swing.Timer, not plain thread (is possible, but with more stuff than ...)

2/ never, really never use in Swing code Thread.sleep(int), EDT issues

3/ look for JLabel plus ImageIcon (similair for JPanel)

4/ painting directly to the JComponent is hight level od programing, and required manually added/implements methods, that in Basic JComponents as JLabel ot JPanel are by default implemented in API

this is part of an assignment and have to use JComponent. My Attacker Class implements the image and bounds thus...

attIcon = new ImageIcon(getClass().getResource("horde1.gif"));
      atkBounds = new Rectangle(500,750,attIcon.getIconWidth(), attIcon.getIconHeight());

My problem is how to use the moveAtk method with an ArrayList of the Attacker class. As our tutor neglected to teach this, the whole class is having problems.

for example this way

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class AnimationJPanel extends JPanel {

    private static final long serialVersionUID = 1L;
    private int cx = 0; // circle's dimensions
    private int cy = 150;
    private int cw = 20;
    private int ch = 20;
    private int xinc = 1; // values to increment circle's position by
    private int yinc = 1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                AnimationJPanel panel = new AnimationJPanel();
                panel.setPreferredSize(new Dimension(400, 300));
                panel.animate();
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.getContentPane().add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public AnimationJPanel() {
        setLayout(new BorderLayout());
        JLabel label = new JLabel("This is an AnimationJPanel");// JLabel for testing purposes
        label.setForeground(Color.RED);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        add(label);
        setBackground(Color.BLACK);// background to be black, circle red
        setForeground(Color.RED);
        setOpaque(true);
    }

    public void animate() {
        new Timer(15, new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Rectangle oldCircle = new Rectangle(cx - 1, cy - 1, cw + 2, ch + 2);
                cx += xinc;
                cy += yinc;
                if (cx >= getWidth() - cw || cx <= 0) {
                    xinc *= -1;
                }
                if (cy >= getHeight() - ch || cy <= 0) {
                    yinc *= -1;
                }
                repaint(oldCircle);
                repaint(cx - 1, cy - 1, cw + 2, ch + 2);
            }
        }).start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawOval(cx, cy, cw, ch);
    }
}
This article has been dead for over six months. Start a new discussion instead.