public class GuiTest extends JPanel {
private class TimerAnimation extends JLabel implements ActionListener {
// public class TimerAnimation extends JTextField implements
// ActionListener {
int deltaX = 2, deltaY = 3, directionX = 1, directionY = 1;
public TimerAnimation(final int startX, final int startY,
final int deltaX, final int deltaY, int directionX,
int directionY, int delay) {
this.deltaX = deltaX;
this.deltaY = deltaY;
this.directionX = directionX;
this.directionY = directionY;
// setText("O");
try {
setIcon(new ImageIcon(new URL(
"http://www.java-forum.org/de/userfiles/"
+ "user3690/redbull.gif")));
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
setSize(getPreferredSize());
setLocation(startX, startY);
new javax.swing.Timer(delay, this).start();
}
public void actionPerformed(final ActionEvent e) {
Container parent = getParent();
// Determine next X position
int nextX = getLocation().x + (deltaX * directionX);
if (nextX < 0) {
nextX = 0;
directionX *= -1;
}
if (nextX + getSize().width > parent.getSize().width) {
nextX = parent.getSize().width - getSize().width;
directionX *= -1;
}
// Determine next Y position
int nextY = getLocation().y + (deltaY * directionY);
if (nextY < 0) {
nextY = 0;
directionY *= -1;
}
if (nextY + getSize().height > parent.getSize().height) {
nextY = parent.getSize().height - getSize().height;
directionY *= -1;
}
setLocation(nextX, nextY); // Move the label/textfield
}
}
private class Picture extends JLabel {
public Picture(final JFrame frame, final Image img) {
setIcon(new ImageIcon(img));
setOpaque(true);
setBounds(75, 75, img.getWidth(frame), img.getHeight(frame));
frame.setLayout(null);
frame.add(this);// Picture component to the Frame:
}
}
private class WordLabel extends JLabel {
private final FontMetrics metrics;
private final Font font = new Font("Sans", Font.BOLD, 24);
WordLabel() {
metrics = getFontMetrics(font);
setFont(font);
setForeground(Color.GREEN);
setBackground(Color.BLUE);
setBorder(BorderFactory.createLineBorder(Color.RED));
}
public int getWidth() {
return metrics.stringWidth(getText()) + 4;
}
public int getHeight() {
return metrics.getHeight() + 4;
}
}
private final WordLabel word = new WordLabel();
private final JFrame frame = new JFrame();
private final Picture pic;
private final Timer timer;
private int y;
GuiTest() {
timer = new Timer(100, new ActionListener() {
public void actionPerformed(ActionEvent e) {
y++;
word.setLocation(0, y);
}
});
timer.start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
URL url = null;
Image img = null;// background image
try {
url = new URL("http://www.java-forum.org/de/userfiles/user3690/"
+ "bottle-water-on-forehead.gif");
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
try {
img = ImageIO.read(url);
} catch (IOException e) {
e.printStackTrace();
}
pic = new Picture(frame, img);
pic.add(word);
pic.add(new TimerAnimation(10, 10, 2, 3, 1, 1, 10));
pic.add(new TimerAnimation(300, 100, 3, 2, -1, 1, 20),
BorderLayout.SOUTH);
frame.setVisible(true);
word.setText("hallo");
}
private static void createAndShowGui() {
new GuiTest();
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}