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.
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| class UIButton { Applet parent; Rectangle bounds; boolean over; boolean pressed; void paint(Graphics g) { } void setOver(boolean b) { if (over == b) return; over = b; repaint(); } 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; void init() { addMouseListener(new MouseAdapter() { }); } 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) 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class UIAnimatedButton extends UIButton { int phase; Image phasesImage; synchronized void tick() { phase = (phase + 1) % MAX_PHASE; repaint(); } void paint(Graphics g) { 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(); } } |