r4.dvi

Similar documents
r3.dvi

r8.dvi

r5.dvi

r14.dvi

r2.dvi

r14.dvi

r3.dvi

6 p.1 6 Java GUI GUI paintcomponent GUI mouseclicked, keypressed, actionperformed mouseclicked paintcomponent thread, 1 GUI 6.0.2, mutlithread C

Java演習(9) -- クラスとメソッド --

r6.dvi

Local variable x y i paint public class Sample extends Applet { public void paint( Graphics gc ) { int x, y;... int i=10 ; while ( i < 100 ) {... i +=

r2.dvi


Thread

Java言語 第1回

< F2D F B834E2E6A7464>

r4.dvi

2


JAVA入門

< F2D834F838C A815B A CC>

< F2D B838A835882CC8CF68EAE2E6A7464>

Microsoft PowerPoint - prog11.ppt

Microsoft PowerPoint - prog11.ppt

GUIプログラムⅤ

I. java.awt.rectangle java.lang.math random Java TM API java.awt Rectangle Rectangle (x,y)... public int x Rectangle X public int y Rectangle Y public

< F2D82518E9F8AD CC834F CC8CFC82AB82C68D4C>

Object MenuComponent MenuBar MenuItem Menu CheckboxMenuItem

Safari AppletViewer Web HTML Netscape Web Web 15-1 Applet Web Applet init Web paint Web start Web HTML stop destroy update init Web paint start Web up

アプレットの作成

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

< F2D E E6A7464>

I HTML HashMap (i) (ii) :.java import java.net.*; import java.io.*; import java.util.hashmap; public class SimpleStopWatch { public static voi

:30 12:00 I. I V II. III. IV. ( a d) V. VI : A ActionListener aa addactionlistener AE ActionEvent K KeyListener ak addkeyliste

GUIプログラムⅡ

< F2D89BA8EE882C E6A7464>

I. (i) Java? (A). 2Apples (B). Vitamin-C (C). Peach21 (D). Pine_Apple (ii) Java? (A). Java (B). Java (C). Java (D). JavaScript Java JavaScript Java (i

IE6 2 BMI chapter1 Java 6 chapter2 Java 7 chapter3 for if 8 chapter4 : BMI 9 chapter5 Java GUI 10 chapter6 11 chapter7 BMI 12 chap

Java 2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q 2.

< F2D8EA CE909482CC92EA82852E6A7464>

2

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

< F2D92DE82E8914B82CC977088D32E6A7464>

< F2D BCA82CC978E89BA82CC8EC08CB12E6A7464>

< F2D82518CC282CC D2E6A7464>

< F2D825282CC947B909482CC A815B83682E6A>

< F2D B825082CC96E291E82E6A7464>


2 1 Web Java Android Java 1.2 6) Java Java 7) 6) Java Java (Swing, JavaFX) (JDBC) 7) OS 1.3 Java Java

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperformed

< F2D82B682E182F182AF82F12E6A7464>

< F2D A839382CC906A2E6A7464>

表示の更新もそういた作業のひとつに当たる スレッドの使用アニメーション アニメーションやシミュレーションなどは画面の更新が一定のタイミングで行われていく この連続した画面の更新をスレッドを利用して行う しかし paint() メソッドを直接呼び出して表示を更新することはできない その理由

II Java :30 12:00 I. I IV II. III. IV. ( a d) V. : this==null, T == N A ActionListener C class D actionperformed G getsource I implements K

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperforme

Microsoft PowerPoint ppt

Microsoft PowerPoint - OOP.pptx

GUIプログラムⅣ

Microsoft PowerPoint - prog12.ppt

untitled

Microsoft PowerPoint prog1_doc2x.pptx

Java言語 第1回

5 p Point int Java p Point Point p; p = new Point(); Point instance, p Point int 2 Point Point p = new Point(); p.x = 1; p.y = 2;

Applet java.lang.object java.awt.component java.awt.container java.awt.panel java.applet.applet

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperforme

19 3!! (+) (>) (++) (+=) for while 3.1!! (20, 20) (1)(Blocks1.java) import javax.swing.japplet; import java.awt.graphics;

10K pdf

PowerPoint Presentation

Microsoft PowerPoint prog1_doc2.pptx

B 10 : N ip2003f10.tex B : 9/12/ :02 p.1/71

< F2D82518E9F8AD CC95BD8D7388DA93AE2E6A7464>

Java演習(2) -- 簡単なプログラム --

I. (i) Foo public (A). javac Foo.java java Foo.class (C). javac Foo java Foo (ii)? (B). javac Foo.java java Foo (D). javac Foo java Foo.class (A). Jav

I 4 p.2 4 GUI java.awt.event.* import /* 1 */ import mouseclicked MouseListener implement /* 2 */ init addmouselistener(this) this /* 3 */ this mousec

Java演習(4) -- 変数と型 --

< F2D A838B838D96402E6A7464>

vol.30.}...`.X...b.h

Java言語 第1回

55 7 Java C Java TCP/IP TCP/IP TCP TCP_RO.java import java.net.*; import java.io.*; public class TCP_RO { public static void main(string[] a

Java演習(6) -- 条件分岐 --


KeyListener init addkeylistener addactionlistener addkeylistener addkeylistener( this ); this.addkeylistener( this ); KeyListener public void keytyped

2 p.2 2 Java > javac Hello0.java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.j

Microsoft PowerPoint ppt

:30 12:00 I. I VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperformed

橡告改.PDF

tn_soturon_sjis.dvi

LMNtal LMNtal LMNtal JAVA JAVA JAVA LMNtal LMNtal LMNtal

text_13.dvi

入門Java解答.doc

Chapter 20. [ ] ; [ ] = new [ ] ; Color colors [ ] = new Color[ 20 ]; // 20 Button operations [ ] = new Button[ 10 ]; // 10 colors[ 3 ] = new Color( 1

PowerPoint プレゼンテーション

Java 3 p.2 3 Java : boolean Graphics draw3drect fill3drect C int C OK while (1) int boolean switch case C Calendar java.util.calendar A

Microsoft Word - Java3.DOC

問1

awt の主要なクラスを下記に示す クラス Component Container Button Label Panel Frame 説明画面にユーザインターフェイス要素として表示し, ユーザとのやり取りを行うコンポーネントを表すすべてのコンポーネントのスーパークラスになる ほかのコンポーネントを含

2 p.2 2 Java Hello0.class JVM Hello0 java > java Hello0.class Hello World! javac Java JVM java JVM : Java > javac 2> Q Foo.java Java : Q B

try catch Exception Java try catch try { } catch ( Exception e ) { } e 16-1 try catch 0 try { int x = 0; int y = 10 / x; } catch ( Exception e ) { Sys

Java言語 第1回

Transcription:

00 4 2000.6.24 0 GUI GUI GUI GUI 1 1.1 3 2 1 import java.applet.applet; import java.awt.*; public class r3ex2 extends Applet { Figure[] figs = new Figure[]{ new Circle(Color.blue, 100.0, 100.0, 30.0, 1.1, -3.0), new Circle(Color.red, 120.0, 80.0, 20.0, -0.8, 0.5), new Rect(new Color(200, 100, 250), 10.0, 10.0, 80.0, 50.0), new RegularPolygon(Color.yellow, 180.0, 120.0, 80.0, 3), new RegularPolygon(Color.green, 50.0, 120.0, 30.0, 7), new Hinomaru(220.0, 50.0, 20.0), new Hinomaru(200.0, 140.0, 15.0), new ShowText(Color.black, 100, 180, "Hello, World", 0.0, -3.0), 1

; public void paint(graphics g) { for(int i = 0; i < figs.length; ++i) { figs[i].addtime(10.0); figs[i].draw(g); Figure interface Figure { public void addtime(double time); public void draw(graphics g); Circle ( 3) class Circle implements Figure { Color col; double cx, cy, rad, vx, vy; public Circle(Color c, double x, double y, double r, double vx1, double vy1) { col = c; cx = x; cy = y; rad = r; vx = vx1; vy = vy1; cx = cx + vx*dt; cy = cy + vy*dt; g.setcolor(col); g.filloval((int)(cx-rad),(int)(cy-rad),(int)(rad*2),(int)(rad*2)); 2 class Rect implements Figure { Color col; double x0, y0, width, height; public Rect(Color c, double x, double y, double w, double h) { col = c; x0 = x; y0 = y; width = w; height = h; g.setcolor(col); g.fillrect((int)(x0), (int)(y0), (int)(width), (int)(height)); 2

class RegularPolygon implements Figure { Color col; double cx, cy, rad; int num; int[] px, py; public RegularPolygon(Color c, double x, double y, double r, int n) { col = c; cx = x; cy = y; num = n; rad = r; px = new int[n]; py = new int[n]; for(int i = 0; i < num; ++i) { double theta = (2.0 * Math.PI) * i / num; px[i] = (int)(cx + rad * Math.cos(theta)); py[i] = (int)(cy + rad * Math.sin(theta)); g.setcolor(col); g.fillpolygon(px, py, num); 1 class Hinomaru implements Figure { double cx, cy, rad; public Hinomaru(double x, double y, double r) { cx = x; cy = y; rad = r; g.setcolor(color.white); g.fillrect((int)(cx-rad*3.0), (int)(cy-rad*2.0), (int)(rad*6.0), (int)(rad*4.0)); g.setcolor(color.red); g.filloval((int)(cx-rad),(int)(cy-rad),(int)(rad*2.0),(int)(rad*2.0)); 3

class ShowText implements Figure { Font fn = new Font("Helvetica", Font.BOLD, 24); Color col; double px, py, vx, vy; String text; public ShowText(Color c, double x, double y, String t, double vx1, double vy1) { col = c; px = x; py = y; text = t; vx = vx1; vy = vy1; px = px + vx*dt; py = py + vy*dt; g.setfont(fn); g.setcolor(col); g.drawstring(text, (int)(px), (int)(py)); 1.2 5 import java.applet.applet; import java.awt.*; public class r3ex5 extends Applet { Figure[] figs = new Figure[]{ new RotPolygon(Color.blue, 100.0, 100.0, 50.0, 5, -0.5), new RotPolygon(Color.red, 150.0, 80.0, 70.0, 3, 0.8), new MovingSquare(Color.green, 120.0, 80.0, 20.0, 30.0, 0.5), new ChangeColorText("Hello, World", 130.0, 40.0, 0.2), ; boolean running; long basetime; public void paint(graphics g) { long time = System.currentTimeMillis(); double dt = 0.001*(time-basetime); basetime = time; for(int i = 0; i < figs.length; ++i) { figs[i].addtime(dt); figs[i].draw(g); public void start() { 4

running = true; basetime = System.currentTimeMillis(); (new Thread(new Timer())).start(); public void stop() { running = false; class Timer implements Runnable { public void run() { while(running) { try { Thread.sleep(100); catch(exception e) { repaint(); interface Figure { public void addtime(double time); public void draw(graphics g); class RotPolygon implements Figure { int[] px, py; Color col; int num; double cx, cy, rad, theta, vtheta; public RotPolygon(Color c, double x, double y, double r, int n, double v) { col = c; num = n; cx = x; cy = y; rad = r; theta = 0.0; vtheta = v; px = new int[num]; py = new int[num]; theta = theta + dt*vtheta; for(int i = 0; i < num; ++i) { double t = theta + (2.0 * Math.PI) * i / num; px[i] = (int)(cx + rad * Math.cos(t)); py[i] = (int)(cx + rad * Math.sin(t)); g.setcolor(col); g.fillpolygon(px, py, num); 5

5a public class MovingSquare implements Figure { Color col; double px, py, len, rad, vtheta; double theta = 0.0; public MovingSquare(Color c, double x, double y, double l, double r, double vt) { col = c; px = x; py = y; len = l; rad = r; vtheta = vt; theta = theta + vtheta*dt; int x = (int)(px + rad*math.cos(theta)); int y = (int)(py + rad*math.sin(theta)); g.setcolor(col); g.fillrect(x, y, (int)(len), (int)(len)); Color.getHSBColor() 3 class ChangeColorText implements Figure { Font fn = new Font("Helvetica", Font.BOLD, 24); String text; double px, py, vtheta; double theta = 0.0; public ChangeColorText(String t, double x, double y, double v) { text = t; px = x; py = y; vtheta = v; theta = theta + vtheta*dt; g.setfont(fn); g.setcolor(color.gethsbcolor((float)(theta % 1.0), 0.8f, 0.8f)); g.drawstring(text, (int)(px), (int)(py)); 5c ( ) 6

1.3 7 BounceCircle 7a 7c import java.applet.applet; import java.awt.*; public class r3ex7 extends Applet { Figure[] figs = new Figure[]{ new BounceCircle(Color.blue, 100.0, 100.0, 30.0, 30.0, 95.0), new BounceCircle(Color.red, 120.0, 80.0, 20.0, -85.0, 50.0), ; boolean running; long basetime; public void paint(graphics g) { long time = System.currentTimeMillis(); double dt = 0.001*(time-basetime); basetime = time; for(int i = 0; i < figs.length; ++i) { figs[i].addtime(dt); figs[i].draw(g); public void start() { running = true; basetime = System.currentTimeMillis(); (new Thread(new Timer())).start(); public void stop() { running = false; class Timer implements Runnable { public void run() { while(running) { try { Thread.sleep(100); catch(exception e) { repaint(); interface Figure { public void addtime(double time); public void draw(graphics g); 7

class BounceCircle implements Figure { double alpha = 100.0; double ratio = 0.85; double width = 300.0; double height = 200.0; Color col; double cx, cy, rad, vx, vy; public BounceCircle(Color c, double x, double y, double r, double vx1, double vy1) { col = c; cx = x; cy = y; rad = r; vx = vx1; vy = vy1; cx = cx + vx*dt; cy = cy + vy*dt; vy = vy + alpha*dt; if(cx-rad < 0 && vx < 0.0) { vx = -vx*ratio; if(cx+rad > width && vx > 0.0) { vx = -vx*ratio; if(cy-rad < 0 && vy < 0.0) { vy = -vy*ratio; if(cy+rad > height && vy > 0.0) { vy = -vy*ratio; g.setcolor(col); g.filloval((int)(cx-rad),(int)(cy-rad),(int)(rad*2),(int)(rad*2)); V y = V y + t (?) 2 implements extends interface Animal { public void forward(double speed); // public void tell(string mesg); // class Dog implements Animal { double speed; public void forward(double speed) { public void tell(string mesg) { 8

class Cat implements Animal { double speed; public void forward(double speed) { public void tell(string mesg) { Animal a =...... a.tell("come on!"); Dog Cat Animal Animal Dog Cat Animal class Animal { double speed; public void forward(double speed) { public void tell(string mesg) { class Dog extends Animal { public void tell(string mesg) { class Cat extends Animal { public void tell(string mesg) { Dog Cat forward() Animal speed Animal ( Animal) ( Dog Cat) tell() ( override) 3 GUI GUI GUI(Graphical User Interface) GUI (GUI ) 9

/ ( GUI ) Java GUI JDK 1.1 1.2 1.2 Swing 1.1 java.awt GUI ( Component ) setbounds(int, int, int, int) (x,y) setforeground(color) setbackground(color) setfont(font) 2 import java.applet.applet; import java.awt.*; public class R4Sample1 extends Applet { Font fn = new Font("Helvetica", Font.BOLD, 24); TextField t1 = new TextField("text..."); Button b1 = new Button("B1"); Button b2 = new Button("B2"); Label l1 = new Label("label..."); public void init() { setlayout(null); add(t1); t1.setbounds(20, 20, 200, 40); add(b1); b1.setfont(fn); b1.setbounds(20, 80, 60, 40); add(b2); b2.setfont(fn); b2.setbounds(120, 80, 60, 40); add(l1); l1.setforeground(color.red); l1.setbackground(color.yellow); l1.setbounds(20, 140, 200, 40); ( GUI ) init() add() setlayout(null) off ( ) ( ) ( ) ) Label Button Choice 10

Checkbox CheckboxGroup TextField TextArea List Frame * MenuBar * Menu * PopupMenu * 1 2 (API ) * 3 GUI ( ) GUI Java a. b. 2 c. ( ) d. ( ) e. 4 ( ) GUI GUI addactionlistener(actionlistner) ActionListener ActionListener actionperformed(actionevent) ActionListener implements actionperformed() GUI addactionlistener() GUI B1 ActionListener java.awt.event import import java.applet.applet; import java.awt.*; import java.awt.event.*; 11

public class R4Sample2 extends Applet { Font fn = new Font("Helvetica", Font.BOLD, 24); TextField t1 = new TextField("text..."); Button b1 = new Button("B1"); Button b2 = new Button("B2"); Label l1 = new Label("label..."); public void init() { setlayout(null); add(t1); t1.setbounds(20, 20, 200, 40); add(b1); b1.setfont(fn); b1.setbounds(20, 80, 60, 40); add(b2); b2.setfont(fn); b2.setbounds(120, 80, 60, 40); add(l1); l1.setforeground(color.red); l1.setbackground(color.yellow); l1.setbounds(20, 140, 200, 40); b1.addactionlistener(new MyAdapter()); class MyAdapter implements ActionListener { public void actionperformed(actionevent evt) { String str = t1.gettext(); String res = ""; for(int i = str.length()-1; i >= 0; --i) { res = res + str.charat(i); l1.settext(res); t1.settext(""); b1 MyAdapter actionperformed() t1 (String ) l1 5 MyAdapter 1 ( )! implements extends 1 12

class MyXXXClass extends/implements YYY { //... new MyXXXClass( )... new MyXXXClass ( )... new YYY( ) { //... new import java.applet.applet; import java.awt.*; import java.awt.event.*; public class R4Sample3 extends Applet { Font fn = new Font("Helvetica", Font.BOLD, 24); TextField t1 = new TextField("text..."); Button b1 = new Button("B1"); Button b2 = new Button("B2"); Label l1 = new Label("label..."); public void init() { setlayout(null); add(t1); t1.setbounds(20, 20, 200, 40); add(b1); b1.setfont(fn); b1.setbounds(20, 80, 60, 40); add(b2); b2.setfont(fn); b2.setbounds(120, 80, 60, 40); add(l1); l1.setforeground(color.red); l1.setbackground(color.yellow); l1.setbounds(20, 140, 200, 40); b1.addactionlistener(new ActionListener() { public void actionperformed(actionevent evt) { String str = t1.gettext(); String res = ""; for(int i = str.length()-1; i >= 0; --i) { res = res + str.charat(i); l1.settext(res); t1.settext(""); ); 3 R4Sample3 4 B2 (java.lang.string API ) 13

a. B2 b. B2 a * c. B2 a i u e o * : B2 ( ) b2.addactionlistener(new ActionListener() { public void actionperformed(actionevent evt) { ); 6 actionperformed()? ( ) try {...... catch(exception ex) {... main() ( ) GUI ( ex ) import java.applet.applet; import java.awt.*; import java.awt.event.*; public class R4Sample4 extends Applet { Font fn = new Font("Helvetica", Font.BOLD, 24); TextField t1 = new TextField("1"); Button b1 = new Button("+1"); Button b2 = new Button("-1"); Label l1 = new Label(""); public void init() { setlayout(null); add(t1); t1.setbounds(20, 20, 200, 40); add(b1); b1.setfont(fn); b1.setbounds(20, 80, 60, 40); 14

add(b2); b2.setfont(fn); b2.setbounds(120, 80, 60, 40); add(l1); l1.setforeground(color.red); l1.setbackground(color.yellow); l1.setbounds(20, 140, 300, 40); b1.addactionlistener(new ActionListener() { public void actionperformed(actionevent evt) { try { int i = (new Integer(t1.getText())).intValue(); t1.settext("" + (i+1)); catch(exception ex) { l1.settext("" + ex); ); b2.addactionlistener(new ActionListener() { public void actionperformed(actionevent evt) { try { int i = (new Integer(t1.getText())).intValue(); t1.settext("" + (i-1)); catch(exception ex) { l1.settext("" + ex); ); l1 5 3 7 : GUI GUI import java.applet.applet; import java.awt.*; import java.awt.event.*; public class R4Sample5 extends Applet { Circle c = new Circle(Color.red, 100.0, 150.0, 30.0); Label l1 = new Label(""); Button b1 = new Button("SET!"); TextField t_x = new TextField("100"); TextField t_y = new TextField("150"); TextField t_rad = new TextField("30"); TextField t_r = new TextField("255"); TextField t_g = new TextField("0"); TextField t_b = new TextField("0"); public void init() { 15

setlayout(null); l1.setbackground(color.white); add(t_x); t_x.setbounds(20, 20, 40, 30); add(t_y); t_y.setbounds(80, 20, 40, 30); add(t_rad); t_rad.setbounds(140, 20, 40, 30); add(t_r); t_r.setbounds(20, 60, 40, 30); add(t_g); t_g.setbounds(80, 60, 40, 30); add(t_b); t_b.setbounds(140, 60, 40, 30); add(b1); b1.setbounds(200, 60, 40, 30); add(l1); l1.setbounds(20, 100, 300, 30); b1.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { try { c.moveto(new Double(t_x.getText()).doubleValue(), new Double(t_y.getText()).doubleValue()); c.setradius(new Double(t_rad.getText()).doubleValue()); c.setcolor(new Color(new Integer(t_r.getText()).intValue(), new Integer(t_g.getText()).intValue(), new Integer(t_b.getText()).intValue())); repaint(); catch(exception ex) { l1.settext(ex.tostring()); ); public void paint(graphics g) { c.draw(g); class Circle { Color c0; double cx, cy, rad; public Circle(Color c, double x, double y, double r) { c0 = c; cx = x; cy = y; rad = r; g.setcolor(c0); g.filloval((int)(cx-rad), (int)(cy-rad), (int)(rad*2), (int)(rad*2)); double getx() { return cx; double gety() { return cy; double getradius() { return rad; Color getcolor() { return c0; void moveto(double x, double y) { cx = x; cy = y; void setradius(double r) { rad = r; void setcolor(color c) { c0 = c; X Y R G B TextField? 16