<:> MovingImages applet
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class RunnablePanel extends JPanel implements Runnable {
Image image;
int x = 20, y = 20;
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, x, y, null);
}
public void run() {
Dimension d = getSize();
while (true) try {
x += 1;
y += 1;
if (x > d.width) x = 0;
if (y > d.height) y = 0;
repaint();
Thread.currentThread().sleep(50);
} catch (Exception ex) { ex.printStackTrace(); }
}
}
public class MovingImages extends JApplet implements MouseListener {
RunnablePanel runnablePanel = new RunnablePanel();
public void init() {
runnablePanel.setSize(getSize());
runnablePanel.image = getImage(getDocumentBase(), "csLogo.png");
this.addMouseListener(this);
new Thread(runnablePanel).start();
getContentPane().add(runnablePanel);
}
public void mousePressed(MouseEvent event) {
runnablePanel.x = event.getX();
runnablePanel.y = event.getY();
runnablePanel.repaint();
}
public void mouseReleased(MouseEvent event) { }
public void mouseClicked(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
public void mouseExited(MouseEvent event) { }
}