Draw Image button with link in java applet code.

import java.applet.Applet;
import java.awt.*;
import java.net.MalformedURLException;
import java.net.URL;

public class ImageButton extends Canvas
{

    public ImageButton()
    {
        staticImage = null;
        activeImage = null;
        linkURL = null;
        buttonUp = true;
        mouseOver = false;
    }

    public ImageButton(Applet app, String toolType, int border)
    {
        staticImage = null;
        activeImage = null;
        linkURL = null;
        buttonUp = true;
        mouseOver = false;
        this.app = app;
        this.border = border;
        this.toolType = toolType;
        if(toolType.equalsIgnoreCase("View"))
            setSize(25, 30);
        else
        if(toolType.equalsIgnoreCase("Draw"))
            setSize(25, 30);
        else
        if(toolType.equalsIgnoreCase("Text"))
            setSize(25, 30);
    }

    public void setLink(String link)
    {
        thisLink = link;
        try
        {
            linkURL = new URL(thisLink);
        }
        catch(MalformedURLException e)
        {
            linkURL = null;
        }
    }

    public void setStaticImage(Image image)
    {
        staticImage = image;
    }

    public void setActiveImage(Image image)
    {
        activeImage = image;
    }

    public void setTarget(String target)
    {
        thisTarget = target;
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public synchronized void paint(Graphics g)
    {
        int buttonWidth = size().width;
        int buttonHeight = size().height;
        int buttonWidth1 = staticImage.getWidth(null);
        int buttonHeight1 = staticImage.getHeight(null);
        try
        {
            shadowImage = createImage(buttonWidth, buttonHeight);
            shadow = shadowImage.getGraphics();
        }
        catch(Exception e)
        {
            shadow = null;
        }
        if(mouseOver)
        {
            shadow.setColor(getForeground());
            shadow.fill3DRect(0, 0, buttonWidth, buttonHeight, buttonUp);
            if(activeImage != null)
                shadow.drawImage(activeImage, border, border, buttonWidth - border * 2, buttonHeight - border * 2, this);
        } else
        {
            shadow.setColor(getBackground());
            shadow.fill3DRect(0, 0, buttonWidth, buttonHeight, buttonUp);
            if(staticImage != null)
                if(toolType.equalsIgnoreCase("Text"))
                    shadow.drawImage(staticImage, border, border, buttonWidth - border * 2, buttonHeight - border * 2, this);
                else
                if(toolType.equalsIgnoreCase("Draw"))
                    shadow.drawImage(staticImage, buttonWidth / 4 + 6, buttonHeight / 4 - 2, buttonWidth1 - border * 2, buttonHeight1 - border * 2, this);
                else
                if(toolType.equalsIgnoreCase("View"))
                    shadow.drawImage(staticImage, buttonWidth / 4 + 6, buttonHeight / 4 - 2, buttonWidth1 - border * 2, buttonHeight1 - border * 2, this);
        }
        g.drawImage(shadowImage, 0, 0, this);
    }

    public boolean mouseEnter(Event evt, int x, int y)
    {
        setBackground(new Color(12, 97, 195));
        return true;
    }

    public boolean mouseExit(Event evt, int x, int y)
    {
        setBackground(new Color(208, 228, 123));
        return true;
    }

    public int border;
    public String thisLink;
    public String thisTarget;
    public Applet app;
    public Image shadowImage;
    public Graphics shadow;
    public Image staticImage;
    public Image activeImage;
    public URL linkURL;
    public boolean buttonUp;
    public boolean mouseOver;
    public String toolType;
    public int backRed;
    public int backGreen;
    public int backBlue;
    public int foreRed;
    public int foreGreen;
    public int foreBlue;
}

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

How to capture finger prints in java

Shallow Copy Deep Copy Java example