home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 3.2 KB | 145 lines |
- // ImageButton.java
- // A button with an image on it. Can be set to indent on a mouse click, or
- // when the mouse pointer moves over it.
- import java.awt.*;
-
- public class ImageButton extends Canvas
- {
- Image im1, im2; // non-indented and indented images
- boolean indent; // indent on mouse-over?
- int border; // pixel size of 3d border
- Color col1, col2; // colours for border
- boolean down; // are we indented right now?
- int w,h; // max image size
-
- // Create a button using the same image for both indented and
- // non-indented modes.
- ImageButton(Image i)
- {
- this(i, i, false, 3);
- }
-
- // Create an imagebutton with the given iamge and border
- ImageButton(Image i, int b)
- {
- this(i, i, false, b);
- }
-
- // Create a button with non-indented and indented images
- ImageButton(Image i1, Image i2)
- {
- this(i1, i2, false, 3);
- }
-
- // Create a button with non-indented and indented images, and the
- // given indent mode and border size.
- ImageButton(Image i1, Image i2, boolean id, int b)
- {
- im1 = i1; im2 = i2;
- indent = id;
- border = b;
- // Wait for the image to properly load
- MediaTracker wt = new MediaTracker(this);
- wt.addImage(im1, 666);
- wt.addImage(im2, 666);
- try wt.waitForAll();
- catch(InterruptedException e);
- w = Math.max(im1.getWidth(this), im2.getWidth(this));
- h = Math.max(im1.getHeight(this), im2.getHeight(this));
-
- // alloc colours
- col1 = new Color(220, 220, 220);
- col2 = new Color(50, 50, 50);
- }
-
- // paint
- // Re-render this component
- public void paint(Graphics g)
- {
- if (g == null) return;
- // Paint border
- int w = size().width, h = size().height;
- g.setColor(down?col2:col1);
- for(int i=0; i<border; i++) {
- g.drawLine(i,i,w-i,i);
- g.drawLine(i,i,i,h-i);
- }
- g.setColor(down?col1:col2);
- for(int i=0; i<border; i++) {
- g.drawLine(w-i,h-i, w-i,i);
- g.drawLine(w-i,h-i, i,h-i);
- }
-
- // Paint image
- if (isEnabled())
- g.drawImage(down?im2:im1, border, border,
- w-border*2, h-border*2, this);
- else {
- g.setColor(Color.lightGray);
- g.fillRect(border, border, w-border*2, h-border*2);
- }
- }
-
- // mouseDown
- // If we are not indenting whenever the pointer moves over this
- // component, indent now.
- public boolean mouseDown(Event evt, int x, int y)
- {
- if (!isEnabled()) return false;
- if (!indent) {
- down = true;
- paint(getGraphics());
- }
- return true;
- }
-
- // mouseUp
- // Send a message to the parent component
- public boolean mouseUp(Event evt, int x, int y)
- {
- if (!isEnabled()) return false;
- if (!indent) {
- down = false;
- paint(getGraphics());
- }
- if (x >= 0 && y >= 0 && x < size().width && y < size().height)
- getParent().postEvent(new Event(this,Event.ACTION_EVENT,null));
- return true;
- }
-
- // mouseEnter
- // If in auto-indent mode, indent this button
- public boolean mouseEnter(Event evt, int x, int y)
- {
- if (!isEnabled()) return false;
- if (indent) {
- down = true;
- paint(getGraphics());
- }
- return true;
- }
-
- // mouseExit
- // If in auto-indent mode, un-indent this button
- public boolean mouseExit(Event evt, int x, int y)
- {
- if (!isEnabled()) return false;
- if (indent) {
- down = false;
- paint(getGraphics());
- }
- return true;
- }
-
- public Dimension minimumSize()
- {
- return new Dimension(w+border*2, h+border*2);
- }
-
- public Dimension preferredSize()
- {
- return minimumSize();
- }
- }
-
-