ArrayList

If more than one image is being manipulated in an applet, then the various images can be held in an ArrayList. This is shown in the example below.

ArrayList_Images_Demo Example: (Run Applet)

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

public class ArrayList_Images_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ListSelectionListener
    {
        private ArrayList<Sprite> imageList = new ArrayList<>();
        private ImageCanvas imageCanvas = new ImageCanvas();
        private JPanel controls = new JPanel();
        private JPanel controlsAndColours = new JPanel();
        private String names[] =
        {
            "koala", "dancers", "smiley", "champions_league"
        };
        private JList imageChoice = new JList(this.names);

        public View()
        {
            this.imageCanvas.setBackground(Color.white);

            // controls Panel
            this.controls.setLayout(new FlowLayout());
            this.controls.add(this.imageChoice);

            // this.controlsAndColours
            this.controlsAndColours.setLayout(new BorderLayout());
            this.controlsAndColours.add("Center", this.controls);

            // Applet's Panel
            setLayout(new BorderLayout());
            add("Center", this.imageCanvas);
            add("South", this.controlsAndColours);

            this.imageChoice.addListSelectionListener(this);
        }

        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            if (e.getValueIsAdjusting())
            {
                // Value is still changing
                // We should wait until it has changed
                return;
            }

            String selectedImage;
            selectedImage = this.names[this.imageChoice.getSelectedIndex()];
            Image img = new ImageIcon(getClass().getClassLoader().getResource("images/" + selectedImage + ".jpg")).getImage();
            MediaTracker tracker = new MediaTracker(this);
            tracker.addImage(img, 1);
            try
            {
                tracker.waitForAll();
                this.imageList.add(new Sprite(img)); // the identity of the added image. This information is used by deleteImages
            }
            catch (InterruptedException interruptedException)
            {
                // do not show the image
            }

            this.imageCanvas.paintList(this.imageList);
        }
    }

    // ImageCanvas
    public class ImageCanvas extends JPanel implements MouseListener, MouseMotionListener
    {
        private ArrayList<Sprite> imageList = null;
        private int selectedImage = -1;  // the currently selected image

        public ImageCanvas()
        {
            super();
            this.addMouseListener(this);
            this.addMouseMotionListener(this);
        }

        @Override
        public void mousePressed(MouseEvent e)
        {
            for (int i = this.imageList.size() - 1; i >= 0; i--)
            {
                if ((this.imageList.get(i)).contains(e.getX(), e.getY()))
                {
                    this.selectedImage = i;
                    if (e.getButton() == MouseEvent.BUTTON3)
                    {
                        this.imageList.add(((Sprite) this.imageList.get(i)));  // add a copy of the current Sprite to the front of the list
                        this.imageList.remove(i);                          // delete the Sprite from its original position
                    }
                    this.imageList.get(i).setOffset(e.getX(), e.getY());
                    break;
                }

            }
            this.repaint();
        }

        @Override
        public void mouseClicked(MouseEvent e)
        {
        }

        @Override
        public void mouseReleased(MouseEvent e)
        {
        }

        @Override
        public void mouseEntered(MouseEvent e)
        {
        }

        @Override
        public void mouseExited(MouseEvent e)
        {
        }

        @Override
        public void mouseMoved(MouseEvent e)
        {
        }

        @Override
        public void mouseDragged(MouseEvent e)
        {
            if ((this.imageList.get(this.selectedImage)).contains(e.getX(), e.getY()))
            {
                (this.imageList.get(this.selectedImage)).move(e.getX(), e.getY());
                this.repaint();
            }
        }

        public void paintList(ArrayList imageList)
        {
            this.imageList = imageList;
            this.repaint();
        }

        @Override
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            if (this.imageList != null)
            {
                // draw the sprites
                for (int i = 0; i < this.imageList.size(); i++)
                {
                    g.drawImage(((this.imageList.get(i))).getImage(),
                                ((this.imageList.get(i))).getX(),
                                ((this.imageList.get(i))).getY(), this);
                }
            }
        }
    }

    //Sprite
    public class Sprite
    {
        public final static int NEW = 1;
        public final static int NORMAL = 2;
        public final static int DELETED = 3;
        private final Image img;
        private int state = Sprite.NORMAL; // can be NORMAL, NEW or DELETED used to trigger animation
        private int x = 0;  // initialise at position 0, 0
        private int y = 0;
        private int offsetX = 0;
        private int offsetY = 0;

        public Sprite(Image img)
        {
            super();

            this.img = img;
        }

        public Image getImage()
        {
            return this.img;
        }

        public int getX()
        {
            return (int) this.x;
        }

        public int getY()
        {
            return (int) this.y;
        }

        public int getWidth()
        {
            return this.img.getWidth(rootPane);
        }

        public int getHeight()
        {
            return this.img.getHeight(rootPane);
        }

        public int getState()
        {
            return this.state;
        }

        public void setState(int state)
        {
            this.state = state;
        }

        public boolean isNormal()
        {
            return (this.state == NORMAL);
        }

        public boolean isNew()
        {
            return (this.state == NEW);
        }

        public boolean isDeleted()
        {
            return (this.state == DELETED);
        }

        public void move(int mouseX, int mouseY)  // move top left corner to position x,y
        {
            this.x = mouseX - this.offsetX;
            this.y = mouseY - this.offsetY;
        }

        public void setOffset(int mouseX, int mouseY)
        {
            this.offsetX = mouseX - this.x;
            this.offsetY = mouseY - this.y;
        }

        public boolean contains(int x, int y)
        {
            return ((x >= (int) this.x) &&
                    (x <= (int) this.x + this.img.getWidth(rootPane)) &&
                    (y >= (int) this.y) &&
                    (y <= (int) this.y + this.img.getHeight(rootPane)));
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>