How to add image on button?
how to add image on button using awt in java without swing?
Replies
-
Kaustubh Katdare
Did you make an attempt at it? If yes, please share your code and tell us what's not working? 😀krishbcshow to add image on button using awt in java without swing? -
krishbcs
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) { super(); 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; repaint(); return true; } if (e.id == Event.MOUSE_EXIT) { selected = false; repaint(); 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.setColor(Color.yellow); g.drawRect(0, 0, image.getWidth(this)-1, image.getHeight(this)-1); } } public Dimension preferredSize() { return minimumSize(); } public void update(Graphics g) { paint(g); } }
please correct this error.............. -
krishbcsi want to make a tool bar like following
You are reading an archived discussion.
Related Posts
what is meant by vortex and vorticity?what is the difference between them?
With additional taxes (one more projected for disability aid - akin to the education surcharge- round the corner) we can look with green eyes at these Tax Free Eight before...
November 16, 1993 changed the future of Gaming when Sony decided to foray into the world of entertainment with the launch of the new gaming machine, called 'Playstation'. The playstation...
Sony is expected to unveil the next generation PlayStation 4 on February 20th. The company has already aired videos celebrating the success of earlier playstation platforms and almost everyone is...
According to The Times, a leaked official document suggests that the official price of Playstation 4 will be significantly lesser than PlayStation 3! The leaked internal document suggests that PS4...