Specialeffekter och spelutveckling
i Java(TM) -Appleten Button
import java.applet.*;
import java.awt.*;
import java.net.*;
public class button extends Applet {
Image currentimage,normalimage,mouseoverimage;
public void init()
{
MediaTracker tracker=new MediaTracker(this);
//Observera att vi nu har
//getParameter("normalimage")
//och getParameter("mouseoverimage")
normalimage=getImage(getDocumentBase(),getParameter("normalimage"));
tracker.addImage(normalimage,0);
mouseoverimage=getImage(getDocumentBase(),getParameter("mouseoverimage"));
tracker.addImage(mouseoverimage,1);
currentimage=normalimage;
try {tracker.waitForAll();}
catch(InterruptedException e)
{
System.out.println("Någonting stoppade inladdningen...");
}
}
public void paint(Graphics g)
{
g.drawImage(currentimage,0,0,this);
}
public boolean mouseEnter(Event e, int x, int y)
{
currentimage=mouseoverimage;
update(getGraphics());
return true;
}
public boolean mouseExit(Event e, int x, int y)
{
currentimage=normalimage;
update(getGraphics());
return true;
}
public boolean mouseDown(Event e, int x, int y)
{
String url=getParameter("url");
String target=getParameter("target");
gotoURL(url,target);
return true;
}
public void gotoURL(String address, String target)
{
URL link=null;
AppletContext appletcontrol=getAppletContext();
try {link=new URL(address);}
catch (MalformedURLException ex)
{
showStatus("FELSKRIVEN ADRESS."+address);
};
if (link!=null)
{
if (target!=null && target.length()>0) appletcontrol.showDocument(link,target);
else appletcontrol.showDocument(link);
}
}
}
|
 |  |
|