|
|
Lesson 02 |
|
Load Image |
|
|
|
|
|
[ Load Image ]
In this tutorial you will learn how to :
- load an image file,
- handle simple mouse events,
- get parameters from html file.
[ Code ]
// Lesson02
// - Load Image
// - Handle mouse events
// This code is better viewed with tab size=4
import java.applet.Applet;
import java.awt.*;
import java.net.*;
public class Lesson02 extends Applet
{
Color txtColor; // Text color
Image image; // Image
URL link; // Link
public void init()
{
txtColor = Color.white;
// Load Image
image=getImage(getDocumentBase(),getParameter("image"));
MediaTracker mediatracker = new MediaTracker(this);
mediatracker.addImage(image, 0);
try { mediatracker.waitForID(0); }
catch(InterruptedException ex) {}
mediatracker=null;
// Init link
try { link=new URL(getParameter("link")); }
catch (MalformedURLException e) { System.err.println(e); }
}
public void start()
{
}
public void stop()
{
}
public void destroy()
{
}
public void paint(Graphics g)
{
g.drawImage(image, 0, 0, this);
g.setColor(txtColor);
g.drawString("Lesson02",10,20);
}
public void update(Graphics g)
{
}
public boolean mouseEnter(Event event, int i, int j) {
showStatus(link.toString()); // Show link in status bar
setCursor(new Cursor(Cursor.HAND_CURSOR)); // Change mouse cursor
return true;
}
public boolean mouseExit(Event event, int i, int j) {
setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); // Change mouse cursor
return true;
}
public boolean mouseDown(Event event, int i, int j) {
getAppletContext().showDocument(link,"_blank"); // Open link
return true;
}
}
|
[ HTML TAG ]
<applet code="Lesson02.class" codebase="lesson02" width="150" height="150">
<param name="image" value="lesson02/flare.jpg">
<param name="link" value="http://java.sun.com">
</applet>
|
[ Source ]
- Lesson.java
|
|
|
|
|
|
|