import java awt import java applet public class
アプレットクラスの作成 1.アプレットクラスの作成 import java. awt. *; import java. applet. *; public class Hello extends Applet{ public static void main(Graphics g) { g. draw. String("Hello Java", 10); } }
アプレットクラスの作成 HTMLファイルの準備 ファイル名: Hello. html <HTML> <BODY> <APPLET CODE ="Hello. class" WIDTH = 200 HEIGHT = 100> </APPLET> </BODY> </HTML>
メソッド名 void draw. Arc(int x, int y, int width, int height, int start. Angle, int arc. Angle) Boolean drawimage(Image img int x int y image. Observer observer) void draw. Line(int x 1, int y 1, int x 2, int y 2) void draw. Oval(int x 1, int y 1, int width, int height) void draw. Polygon(int x. Points[], int y x. Points[] int n. Points) void draw. Rect(int x 1, int y 1, int width, int height) 図1. 機能 円弧を描く イメージを描く 線を描く 楕円を描く 多角形 四角形
メソッド名 機能 void fill. Arc(int x, int y, int width, int height, int start. Angle, int arc. Angle) void fill. Oval(int x 1, int y 1, int width, int height) void fill. Polygon(int x. Points[], int y x. Points[] int n. Points) void set. Color(Color c) 円弧(塗りつぶ し) 楕円(塗りつぶ し) 多角形(塗りつ ぶし) 色 void set. Font(Font font) フォント 図1.(続き)
例1.線描画 import java. applet. Applet; import java. awt. Graphics; public class Sample. Line 1 extends Applet{ public void paint(Graphics g){ g. draw. Line(10, 100, 100); } }
線描画 ファイルSample. Line 1. htmlとして <HTML> <BODY> <APPLET CODE ="Sample. Line 1. class" WIDTH = 200 HEIGHT = 100> </APPLET> </BODY> </HTML> を用意
例2.色の変更 import java. applet. Applet; import java. awt. Graphics; import java. awt. Color; public class Sample. Line 2 extends Applet{ public void paint(Graphics g){ g. set. Color(Color. red); g. draw. Line(10, 100, 100); } }
線描画 ファイルSample. Line 2. htmlとして <HTML> <BODY> <APPLET CODE ="Sample. Line 2. class" WIDTH = 200 HEIGHT = 100> </APPLET> </BODY> </HTML> を用意
例3.フォントの設定 import java. applet. Applet; import java. awt. Graphics; import java. awt. Color; import java. awt. Font; public class Sample. Line 3 extends Applet{ public void paint(Graphics g){ g. set. Color(Color. red); g. set. Font(new Font("Serif", Font. BOLD, 24)); g. draw. String("Hello", 20); } }
線描画 ファイルSample. Line 3. htmlとして <HTML> <BODY> <APPLET CODE ="Sample. Line 3. class" WIDTH = 200 HEIGHT = 100> </APPLET> </BODY> </HTML> を用意
色名 指定 白 ライトグレイ Color. white Color. light. Gray Color. gray ダークグレイ 黒 赤 ピンク オレンジ 黄 緑 マゼンダ シアン 青 Color. dark. Gray Color. black Color. red Color. pink Color. orange Color. yellow Color. green Color. magenda Color. cyan Color. blue
フォント名 フォントスタイル Dialog 並 Font. PLAN Dialog. Input 太字 Font. BOLD Monospaced イタリック Font. ITALIC Serif Sans. Serif Symbol
画像表示 import java. applet. Applet; import java. awt. Graphics; import java. awt. Image; public class Sample. Gazo 1 extends Applet{ Image img; public void init() { img=get. Image(get. Document. Base(), "small. jpg"); } public void paint(Graphics g) { g. draw. Image(img, 10, this); } }
画像表示 ファイルSample. Gazo 1. htmlとして <HTML> <BODY> <APPLET CODE ="Sample. Gazo 1. class" WIDTH = 200 HEIGHT = 100> </APPLET> </BODY> </HTML> を用意
マウスに反応させる import java. applet. Applet; import java. awt. Graphics; import java. awt. event. Mouse. Listener; import java. awt. event. Mouse. Event; public class Sample. Mouse 1 extends Applet implements Mouse. Listener{ int x=10; int y=10; public void init() { add. Mouse. Listener(this); }
public void mouse. Clicked(Mouse. Event e){} public void mouse. Entered(Mouse. Event e){} public void mouse. Exited(Mouse. Event e){} public void mouse. Pressed(Mouse. Event e){ x = e. get. X(); y = e. get. Y(); repaint(); } public void mouse. Released(Mouse. Event e){} public void paint(Graphics g){ g. fill. Oval(x, y, 10); } }
マウスに反応させる ファイルSample. Mouse 1. htmlとして <HTML> <BODY> <APPLET CODE =" Sample. Mouse 1. class" WIDTH = 200 HEIGHT = 100> </APPLET> </BODY> </HTML> を用意
import java. applet. Applet; import java. awt. Button; import java. awt. event. Action. Listener; import java. awt. event. Action. Event; public class Sample. AWT 1 extends Applet implements Action. Listener{ Button bt; public void init() { bt=new Button("Strat"); add(bt); bt. add. Action. Listener(this); } public void action. Performed(Action. Event ae){ bt. set. Label("Stop"); } }
AWTの部品とクラス ファイルSample. AWT 1. htmlとして <HTML> <BODY> <APPLET CODE =" Sample. AWT 1. class" WIDTH = 200 HEIGHT = 100> </APPLET> </BODY> </HTML> を用意
アニメーション import java. applet. Applet; import java. awt. Graphics; public class Sample. Anime 1 extends Applet implements Runnable{ int num; public void init() { Thread th; th= new Thread(this); th. start(); }
public void run(){ try{ for(int i=0; i<10; i++){ num=i; repaint(); Thread. sleep(1000); } } catch(Interrupted. Exception e){} } public void paint(Graphics g) { String str = num+"です"; g. draw. String(str, 10); } }
アニメーション ファイルSample. Anime 1. htmlとして <HTML> <BODY> <APPLET CODE =" Sample. Anime 1. class" WIDTH = 200 HEIGHT = 100> </APPLET> </BODY> </HTML> を用意
アプレットタグの埋め込み 例1.では import java. applet. Applet; import java. awt. Graphics; /* <APPLET CODE ="Sample. Line 1. class" WIDTH = 200 HEIGHT = 100> </APPLET> */ public class Sample. Line 1 extends Applet{ public void paint(Graphics g){ g. draw. Line(10, 100, 100); } }
- Slides: 47