Images on AWT Buttons.
2 posts in topic
Flat View  Flat View

Posted By:   Anthony_Stevenson
Posted On:   Monday, May 7, 2001 12:59 PM

How can I place an image on a AWT button? Please give a
working example. I am using AWT buttons because most
Browsers are using only jdk1.1.5 and the SWING components
do not work without a plug-in. Your help is greatly appreciated.

Re: Images on AWT Buttons.

Posted By:   Virgilio_Garcia  
Posted On:   Wednesday, August 29, 2001 01:46 AM

import java.awt.*;

public class ImageButton extends Canvas {
private Image image;
private String command;
private boolean selected = false;
private Dimension size;
public ImageButton(Image img, String command) {
this.image = img;
this.command = command;
if (image == null) {
size = new Dimension(0, 0);
} else {
size = new Dimension(image.getWidth(this), image.getHeight(this));
public boolean handleEvent(Event e) {
if (e.id == Event.MOUSE_UP) {
if (selected) {
e.id = Event.ACTION_EVENT;
e.arg = command;
if (e.id == Event.MOUSE_ENTER) {
selected = true;
return true;
if (e.id == Event.MOUSE_EXIT) {
selected = false;
return true;
return super.handleEvent(e);
public Dimension minimumSize() {
return size;
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
if (selected) {
g.drawRect(0, 0, image.getWidth(this)-1, image.getHeight(this)-1);
public Dimension preferredSize() {
return minimumSize();
public void update(Graphics g) {

Re: Images on AWT Buttons.

Posted By:   moonjeong_kim  
Posted On:   Monday, May 14, 2001 08:26 AM

I don't know how to draw Image on AWT Button.

But if it doesn't have to be AWT Button, how about making ImgButton using Canvas?
(drawing image on awt.Canvas is rather easy)

You can also give effects when mouse is entered(up) and pressed(down) -like icons on window toolbar - by overriding paint(Graphics g) method
About | Sitemap | Contact