[quote]i want to implement a custom button class for my applet. main idea is, that the button is able to provide my kind of animation-strips, so that there’s some animation, not only on/off graphics.
[/quote]
Using the Java Plug-in, you’ve Swing and Swing can display any Image on a JButton, even animated gifs. That would make your problem go away.
But if you want to make things more difficult for yourself, you need to create your own barebone mini UI from scratch. You applet is an AWT component (the only one left if you don’t want to use AWT or Swing) and you need to add your mouse listener here and overwrite its paint() method.
You’ve to create your own objects representing buttons and synthetize your own events. See below for a code sketch:
class UIButton {
Applet parent;
Rectangle bounds;
boolean over;
boolean pressed;
void paint(Graphics g) {
// paint stuff inside bounds, depending over and pressed state
}
void setOver(boolean b) {
if (over == b) return;
over = b; repaint();
}
// same for pressed
// also add getters
void repaint() {
repaint(bounds);
}
void repaint(Rectangle r) {
if (parent != null) parent.repaint(r.x, r.y, r.width, r.height);
}
void boolean contains(int x, int y) {
return bounds.contains(x, y);
}
void onMouseEnter() { setOver(true); }
void onMouseExit() { setOver(false); }
void onMousePressed() { setPressed(true); }
void onMouseReleased() { setPressed(false); }
void onMouseClicked() {}
}
class YourApplet extends Applet {
List<UIButton> buttons; //notice, I'm using JDK 1.5 style generics
void init() {
addMouseListener(new MouseAdapter() {
// delegate calls to methods shown below
});
// do the same for mouseMoveListener
}
void addButton(UIButton b) {
buttons.add(b);
b.parent = this;
b.repaint();
}
void removeButton(UIButton b) {
b.repaint();
b.parent = null;
buttons.remove(b);
}
UIButton buttonAt(int x, int y) {
for (UIButton b : buttons) // again 1.5 style, it's so much shorter
if (b.contains(x, y)) return b;
return null;
}
void paint(Graphics g) {
for (UIButton b : buttons) b.paint(g);
}
UIButton over, pressed;
private void onMouseMove(int x, int y) {
UIButton b = buttonAt(x, y);
if (b != over) {
if (over != null) over.onMouseExit();
over = b;
if (over != null) over.onMouseEnter();
}
}
private void onMousePressed(int x, int y) {
if (over != null) {
pressed = over;
pressed.onMousePressed(x, y);
}
}
private void onMouseReleased(int x, int y) {
if (pressed != null) pressed.onMousePressed(x, y);
if (pressed == over) pressed.onMouseClicked();
pressed = false;
}
}
This code implements generic buttons you can subclass and use similar to you’d use AWT buttons (there’s no listener concept though). To add animations, I could imagine this.
class UIAnimatedButton extends UIButton {
int phase;
Image phasesImage;
synchronized void tick() {
phase = (phase + 1) % MAX_PHASE; repaint();
}
void paint(Graphics g) {
// draw some part of the image
g.drawImage(phasesImage, bounds.x, bounds.y, ..., phase*WIDTH, 0, (phase+1)*WIDTH, HEIGHT, ..);
}
}
class YourApplet extends Applet {
Thread animator = new Thread() {
public void run() {
while (!interrupted()) {
sleep(1000);
for (UIButton b : buttons) b.tick();
}
}
void start() { animator.start(); }
void stop() { animator.interrupt(); }
}