home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Late Night VRML 2.0 with Java CD-ROM
/
code.zip
/
Ch12
/
ui
/
ImageButton.java
< prev
next >
Wrap
Text File
|
1996-12-30
|
4KB
|
151 lines
/** */
/* Copyright (C) 1996 ADI Ltd, Systems Group */
/* Engineering & Development */
/* All rights reserved */
/* */
/* Written by : Cameron Gillies */
/* Last Modified : 21/10/96 */
/* */
package ui;
import java.awt.*;
import java.net.*;
import ui.ImageLabel;
/**
* An image button (a 3D rect around an image.) It greys itself out
* when disabled and inverts the 3D rect and moves its image when depressed.
*/
public class ImageButton extends ImageLabel
{
private int lineWidth = 2; // thickness of 3D line around button
private int pressMovement = 1; // distance image moves when button pressed
protected boolean depressed;
private String buttonTitle;
public ImageButton(String title, URL img, int x, int y)
{
super(img, x, y);
buttonTitle = new String(title);
depressed = false;
padWidth = lineWidth + 2;
}
public void setPadWidth(int w)
{
super.setPadWidth(w + lineWidth);
}
public int getPadWidth()
{
return(super.getPadWidth() - lineWidth);
}
public void setLineWidth(int w)
{
int oldPadWidth = getPadWidth();
lineWidth = w;
setPadWidth(oldPadWidth);
}
public int getLineWidth()
{
return(lineWidth);
}
public void setPressMovement(int p)
{
if (p != pressMovement)
{
pressMovement = p;
if (depressed)
{
Graphics g = getGraphics();
if (g != null)
{
Dimension d = size();
g.setColor(getBackground());
g.fillRect(0, 0, d.width, d.height);
repaint();
}
}
}
}
public int getPressMovement()
{
return(pressMovement);
}
public void paint(Graphics g)
{
if (img != null && (imgWidth == -1 || imgHeight == -1))
{
imgHeight = img.getHeight(this) ;
imgWidth = img.getWidth(this) ;
this.resize(imgWidth + padWidth * 2 , imgHeight + padWidth * 2 );
}
if (imgWidth >= 0 && imgHeight >= 0)
{
Dimension d = size();
int x = (d.width - imgWidth) / 2;
int y = (d.height - imgHeight) / 2;
int offset = (depressed ? pressMovement : 0);
if (pressMovement != 0)
{
// clear the area needed to accomodate press movement
g.setColor(getBackground());
int m = (pressMovement < 0 ? -1 : 1);
for (int i = 0; i < pressMovement * m; i++)
g.drawRect(x + i * m + (m < 0 ? -1 : 0),
y + i * m + (m < 0 ? -1 : 0), imgWidth, imgHeight);
}
// draw the image and the 3D border
g.drawImage(img, x + offset, y + offset, getBackground(), this);
g.setColor(getBackground());
for (int i = 0; i < lineWidth; i++)
g.draw3DRect(i, i,
d.width - i*2 - 1,
d.height - i*2 - 1,
!depressed);
}
}
public boolean mouseDown(Event e, int x, int y)
{
depressed = true;
repaint();
return true;
}
public boolean mouseDrag(Event e, int x, int y)
{
if (depressed != inside(x, y))
{
depressed = !depressed;
repaint();
}
return true;
}
public boolean mouseUp(Event evt, int x, int y)
{
if (depressed)
{
action();
depressed = false;
repaint();
}
return true;
}
public void action()
{
postEvent(new Event(this, Event.ACTION_EVENT, buttonTitle));
}
}