CrazyEngineers
  • How to add image on button?

    krishbcs

    Member

    Updated: Oct 26, 2024
    Views: 1.2K
    how to add image on button using awt in java without swing?
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Kaustubh Katdare

    AdministratorFeb 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? 😀
    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberFeb 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..............
    Are you sure? This action cannot be undone.
    Cancel
  • krishbcs

    MemberFeb 20, 2013

    i want to make a tool bar like following
    [​IMG]
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register