画像を表示する
<html> <body> <applet code= "sample.class" width="100" height="200"> </applet> </body> </html>
<applet code= "sample.class" width="100" height="200">
import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; import java.applet.*; import java.awt.*; public class sample extends Applet { Image img[] = new Image[1]; public void init() // 画像の読み込み { img[0] = getImage(getDocumentBase(), "sample.gif"); } public void paint(Graphics g) // 画像表示 { g.drawImage( img[0],1,1, this ); } }
img[0] = getImage(getDocumentBase(), "sample.gif");
g.drawImage( img[0],1,1, this );
img[0] = getImage(getDocumentBase(), "sample.gif"); ↓ ↓ img = getImage(getDocumentBase(), "sample.gif");
g.drawImage( img[0],1,1, this ); ↓ ↓ g.drawImage( img,1,1, this );
import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; import java.applet.*; import java.awt.*; public class puzzle extends Applet { Image img[] = new Image[4]; //sampleでは[1]でしたが,画像が4枚なので,[4]。 //↑ここは書き直し忘れやすいので注意! public void init() // 画像の読み込み { img[0] = getImage(getDocumentBase(), "1.gif"); img[1] = getImage(getDocumentBase(), "2.gif"); img[2] = getImage(getDocumentBase(), "3.gif"); img[3] = getImage(getDocumentBase(), "4.gif"); } public void paint(Graphics g) // 画像表示 { g.drawImage( img[0],0,0, this ); g.drawImage( img[1],50,0, this ); //x軸を変えて画像が g.drawImage( img[2],100,0, this ); //重ならないようにする。 g.drawImage( img[3],150,0, this ); } }