JAVA研究室へ戻る


JAVAアプレットのイメージファイル表示サンプルです。ソースコード及びサンプルHTMLは ダウンロード コーナー入っています。
 このアプレットはバックグラウンドイメージ処理で画面のずれに対応しています。
とくにスレッド処理する必要な無いのですが、バックグラウンドイメージ処理ようの テンプレートとして作成しました。

基本パラメータは
 timer   表示を呼び出すタイミングを指定
 wx      表示エリアX座標サイズ
 wy      表示エリアY座標サイズ
 image   イメージファイル




// JAVA applet sample 'DrawImage.java' for kitaro C3Lab JAVA lab
// copyright (c) by kitaro 1996
// このプログラムはイメージファイルを表示するアプレットサンプルです
// 特にスレッドにする必要性はないのですが、テンプレートとして使用
// するためにスレッドにしました
// wx     : エリアサイズX座標
// wy     : エリアサイズY座標
// image  : イメージファイル名

import java.applet.*;
import java.awt.*;
import java.net.*;

public class DrawImage extends java.applet.Applet implements Runnable {
  String temp;
  //
  Thread thread;
  int   timer,count;
  // background job
  MediaTracker mt;
  Graphics og;
  Image oimage;
  int wx,wy;

  // Wall paper
  String wall;
  Image wallImage;
  //
  public void init(){

	temp = getParameter("wx");     if(temp==null)temp="200"; wx    = Integer.valueOf( temp ).intValue(); 
	temp = getParameter("wy");     if(temp==null)temp="200"; wy    = Integer.valueOf( temp ).intValue(); 

	//-----------------------------------------------------------
	temp = getParameter("timer"); if(temp==null)temp="1000"; timer = Integer.valueOf( temp ).intValue();

	// wall paper setting
	wall = getParameter("image");
	if(wall==null) // debug code
		wall = "http://www.pionet.net/~kitaro/gif/title.gif";
	if(wall!=null)
		try{wallImage = getImage(new URL(wall)); }
		catch(Exception e){ wall=null; showStatus(e.toString());}
	else wallImage=null;

	// offscreen
	oimage = createImage(wx,wy);
	og = oimage.getGraphics();
	og.fillRect(0,0,wx,wy );
	mt = new  MediaTracker (this);
  }
  public void start(){
	thread = new Thread(this);
	thread.start();
  }
  public void run (){
	try{mt.waitForAll();}
	catch(InterruptedException e){showStatus(e.toString());};
	while(true)
	{
		 try{ thread.sleep(timer);}
		 catch(Exception e){}
		off_update();
		repaint();
	}  
}

 public void off_clear(){
	if(wall==null) og.fillRect(0,0,wx,wy );
	else{
		int sx,sy;
		try{
			sx = wallImage.getWidth(this);
			sy = wallImage.getHeight(this);
			if((sx>0)&&(sy>0)){
				og.drawImage(wallImage,0,0,new Color(0),this);
			}
		}catch(Exception e){}
	}
 }
 public void off_update(){
	off_clear();
 }
  public void paint(Graphics g){
	g.drawImage(oimage,0,0,new Color(0),this);  }
  public void update(Graphics g){paint(g); }
}

サンプルHTML
<html>
<head>
<title>DrawImage</title>
</head>
<body>
<hr>
<applet code=DrawImage width=200 height=200>
<param name=timer  value=1000>
<param name=image value=/~kitaro/gif/title.gif>
</applet>
</body>
</html>
<hr>