///////////////////////////////////////////////SKELETON OF H_APPLETS
// Here is the skeleton of all my applets. Refering to this document,
// you will quickly understand all my applet sources. If you are
// interested in this skeleton, by creating your own applet, you only
// need to add your codes at all the places I've commented. Two blocks
// "State of thread" and "Mouse activity" are not necessary to rewrite.
// Some methods, render(), for example, are also not necessary to clear
// if you don't use.
/////////////////////////////////////INTRODUCTION/////////////////////
// Created 01/1998
// Sample: h_myApplet.java
// For free use, copy, modify.
// By La Trong Hung, [email protected]
// Last modified, 01/2001
// SUMMARY: Summary of this applet is commented here
//////////////////////////////////////////////////////////////////////
//==================================================================//
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
public class h_myApplet extends Applet implements Runnable{
//Common variables for h_applets
private Thread th = null;
private MediaTracker mt;
private Image mainImg;
private Graphics mainG;
private String hung, link;
private boolean Init_Complete = false;
private boolean thSuspended = false;
private Image linkImg;
private String mesString;
private int mouseX, mouseY;
// Declare all other necessary variables for this applet here
//====================================================================//
//---------------------------------Initialization---------------------//
public h_myApplet(){}
public void init(){
// Get all parameters here
}
private void initSetting(){
LoadImg();
// All other necessary initializations of this applet here
mainImg = createImage(size().width, size().height);
mainG = mainImg.getGraphics();
mouseX = -1;
mouseY = -1;
linkImg = createMesImg(mesString);
Init_Complete = true;
}
private void LoadImg(){
// Load all images and use the following grabber to grap
// pixel here
}
private void grabPix(Image im, int[] pix, int w, int h){
PixelGrabber h_PixGrab = new PixelGrabber(im, 0, 0, w, h, pix, 0, w);
try {
h_PixGrab.grabPixels();
}
catch (InterruptedException e){
System.out.println("grabber error" + e);
return;
}
if ((h_PixGrab.status() & ImageObserver.ABORT) != 0){
System.out.println("grabber error");
return;
}
}
//---------------------------Render everywhen re-draw----------------//
private void render(){
handlePix();
}
private void handlePix(){
// Handle pixels every when re-draw here
}
//----------------------------Create main buffer image---------------//
private void createMainImg(){
if(Init_Complete == false){
repaint();
initSetting();
}
render();
// Draw all the necessary datas to mainImg (buffer image)
// object here
if(mouseX >= 0)
mainG.drawImage(linkImg, mouseX, mouseY-16, this);
}
//----------------------------State of thread---------------------------//
public void start(){
if(th == null){
th = new Thread(this);
th.start();
}
}
public void stop(){
if(th != null){
th.stop();
th = null;
}
}
public void destroy(){
//All destroy here
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(Init_Complete == true){
g.drawImage(mainImg, 0, 0, this);
}
else{
g.setColor(Color.red);
g.drawString("Initializing...", 10, 20);
}
}
public void run(){
while(Thread.currentThread() == th){
try {
createMainImg();
repaint();
// Set the new value for all variables everywhen
// re-draw here
Thread.sleep(//your_delay);
}
catch(InterruptedException E) {
E.printStackTrace();
}
}
}
//--------------------------Information-------------------------------//
public String getAppletInfo() {
// Return your information here
}
public String[][] getParameterInfo()
{
// Return the parameter informations here
}
//--------------------------Mouse activity-----------------------------//
public boolean mouseDown(Event evt, int x, int y){
if (link != null){
try{
URL url = new URL(getDocumentBase(), link);
getAppletContext().showDocument(url, hung);
if (hung.equals("_self")){
stop();
}
}
catch (MalformedURLException E){
E.printStackTrace();
}
}
else{
if (thSuspended) th.resume();
else th.suspend();
thSuspended = !thSuspended;
}
return(true);
}
public boolean mouseEnter(Event evt, int x, int y){
showStatus(mesString);
mouseX = x;
mouseY = y;
return(true);
}
public boolean mouseMove(Event evt, int x, int y){
mouseX = x;
mouseY = y;
return(true);
}
public boolean mouseExit(Event evt, int x, int y){
showStatus("");
mouseX = -1;
mouseY = -1;
return(true);
}
//------------------------------Message---------------------------//
public Image createMesImg(String mes){
Image linkImage;
Graphics g;
int mw;
int mh = 16;
mw = 6*mes.length();
linkImage = createImage(mw, mh);
g = linkImage.getGraphics();
g.setColor(new Color(255, 255, 190));
g.fillRect(0, 0, mw, mh);
g.setColor(Color.black);
g.drawRect(0, 0, mw-1, mh-1);
g.drawString(mes, 4, 12);
return linkImage;
}
}
|