How to add image on button?

krishbcs

krishbcs

@krishbcs-5tG26c Oct 26, 2024
how to add image on button using awt in java without swing?

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Kaustubh Katdare

    Kaustubh Katdare

    @thebigk Feb 18, 2013

    krishbcs
    how to add image on button using awt in java without swing?
    Did you make an attempt at it? If yes, please share your code and tell us what's not working? 😀
  • krishbcs

    krishbcs

    @krishbcs-5tG26c Feb 20, 2013

    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..............
  • krishbcs

    krishbcs

    @krishbcs-5tG26c Feb 20, 2013

    i want to make a tool bar like following
    [​IMG]