r8.dvi

Similar documents
r4.dvi

r2.dvi

r3.dvi

r14.dvi

r14.dvi

r6.dvi

r5.dvi

Programming-C-9.key

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

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

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

Thread

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

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

PowerPoint Presentation

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

untitled

r2.dvi

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

1 Java Java GUI , 2 2 jlabel1 jlabel2 jlabel3 jtextfield1 jtextfield2 jtextfield3 jbutton1 jtextfield1 jtextfield2 jtextfield3

アプレットの作成

text_13.dvi

: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

Java (9) 1 Lesson Java System.out.println() 1 Java API 1 Java Java 1

I. (i) Java? (A). Foo_Bar (B). G day (C). 999 (D). Golgo13 (ii)? (A). Java public (B). Java (C). Java JavaScript (D). Java C Java C (iii)? (A). Java (

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入門

Microsoft PowerPoint - prog12.ppt

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

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;

< F2D834F838C A815B A CC>

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

< F2D B838A835882CC8CF68EAE2E6A7464>

K227 Java 2


2

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

r1.dvi

r3.dvi

: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

< F2D F B834E2E6A7464>

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

: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

: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

Java言語 第1回

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

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 +=

問題1 以下に示すプログラムは、次の処理をするプログラムである

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

< F2D92DE82E8914B82CC977088D32E6A7464>

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

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

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

< F2D825282CC947B909482CC A815B83682E6A>


< F2D82518CC282CC D2E6A7464>

< F2D BCA82CC978E89BA82CC8EC08CB12E6A7464>

< F2D82B682E182F182AF82F12E6A7464>

Java プログラミング Ⅰ 3 回目変 数 今日の講義講義で学ぶ内容 変数とは 変数の使い方 キーボード入力の仕方 変 数 変 数 一時的に値を記憶させておく機能 変数は 型 ( データ型 ) と識別子をもちます 2 型 ( データ型 ) 変数に記憶する値の種類変数の型は 記憶できる値の種類と範囲

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

text_10.dvi

(Microsoft PowerPoint - \223\306\217KJAVA\221\346\202R\224\ ppt)

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

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

Microsoft PowerPoint - OOP.pptx

GUIプログラムⅤ

I java A

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

2 static final int DO NOTHING ON CLOSE static final int HIDE ON CLOSE static final int DISPOSE ON CLOSE static final int EXIT ON CLOSE void setvisible


上達Java解答.doc

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.

( ) p.1 x y y = ( x ) 1 γ γ = filtergamma.java import java.applet.*; public class filtergamma extends Applet{ Image img; Image new_img; publi

Java プログラミング Ⅰ 3 回目変数 変数 変 数 一時的に値を記憶させておく機能型 ( データ型 ) と識別子をもつ 2 型 ( データ型 ) 変数の種類型に応じて記憶できる値の種類や範囲が決まる 型 値の種類 値の範囲 boolean 真偽値 true / false char 2バイト文

3 p.1 3 Java Java Java try catch C Java if for while C 3.1 boolean Java if C if ( ) 1 if ( ) 1 else , 2 { } boolean true false 2 boolean Gr

JavaプログラミングⅠ

データ構造とアルゴリズム論

< F2D82518E9F8AD CC834F CC8CFC82AB82C68D4C>

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

オブジェクト指向プログラミング・同演習 5月21日演習課題

text_12.dvi

< F2D8EA CE909482CC92EA82852E6A7464>

ただし 無作為にスレッドを複数実行すると 結果不正やデッドロックが起きる可能性がある 複数のスレッド ( マルチスレッド ) を安全に実行する ( スレッドセーフにする ) ためには 同期処理を用いるこ とが必要になる 同期処理は 予約語 synchronized で行うことができる ここでは sy

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

やさしいJavaプログラミング -Great Ideas for Java Programming サンプルPDF

2

JavaプログラミングⅠ

Java プログラミング Ⅰ 7 回目 switch 文と論理演算子 条件判断文 3 switch 文 switch 文式が case の値と一致した場合 そこから直後の break; までを処理し どれにも一致しない場合 default; から直後の break; までを処理する 但し 式や値 1

データ構造とアルゴリズム論

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

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

Microsoft PowerPoint - prog13.ppt

untitled

Chapter JDK KeyListener keypressed(keyevent e ) keyreleased(keyevent e ) keytyped(keyevent e ) MouseListener mouseclicked(mouseeven

ガイダンス

2

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

< F2D89BA8EE882C E6A7464>

PowerPoint プレゼンテーション

Transcription:

I 2005 8 2005.12.9 GUI GUI ( ) GUI try... catch 1 1.1 2 1 2 paint() run() 1 Y 1 2 sin/cos 2 2 Color.getHSBColor() ( Circle setpaint() getpaint() ) import java.awt.*; import javax.swing.*; public class r7ex2 extends JApplet { Image buf; Circle c1 = new Circle(Color.blue, 0, 10, 20); Circle c2 = new Circle(Color.red, 150, 110, 15); public void update(graphics g) { if(buf == null) { buf = createimage(getwidth(), getheight()); Graphics2D g2 = (Graphics2D)buf.getGraphics(); g2.clearrect(0, 0, getwidth(), getheight()); paint(g2); g.drawimage(buf, 0, 0, this); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; c1.draw(g2); c2.draw(g2); public void start() { (new Thread(new MyRun())).start(); class MyRun implements Runnable { public void run() { 1

for(int i = 0; i < 100; ++i) { try { Thread.sleep(50); catch(exception e) { c1.moveto(c1.getx()+1, c1.gety()+1); double r = 50, t = Math.PI/50*i; c2.moveto((int)(150+r*math.cos(t)), (int)(100+r*math.sin(t))); repaint(); for(int i = 0; i < 200; ++i) { try { Thread.sleep(50); catch(exception e) { int dx = (int)(3*math.random()-1.5), dy = (int)(3*math.random()-1.5); c1.moveto(c1.getx()+dx, c1.gety()+dy); c2.moveto(c2.getx()+dx*2, c2.gety()+dy*2); c2.setpaint(color.gethsbcolor((0.01f*i)%1f, 1f, 1f)); repaint(); static class Circle { Paint col; double gx, gy, rad; public Circle(Paint c, double x, double y, double r) { col = c; gx = x; gy = y; rad = r; public void draw(graphics2d g) { g.setpaint(col); g.filloval((int)(gx-rad),(int)(gy-rad),(int)rad*2,(int)rad*2); public void moveto(double x, double y) { gx = x; gy = y; public double getx() { return gx; public double gety() { return gy; public void setpaint(paint c) { col = c; public Paint getpaint() { return col; 1.2 3 a R7Sample2 import java.awt.*; import javax.swing.*; public class r7ex3 extends JApplet { Image buf; boolean go; double time; Animation[] a = new Animation[20]; int count = 0; public void init() { 2

a[count++] = new FlyingCircle(Color.red, 100, 100, 40, 30, -40); a[count++] = new FlyingCircle(Color.blue, 100, 100, 30, 40, 50); a[count++] = new ResizingSquare(Color.green, 100, 100, 100); a[count++] = new GravityCircle(Color.pink, 100, 100, 30, 20, 50); a[count++] = new RoundingCircle(Color.black, 100, 100, 10, 50); a[count++] = new OscillatingOval(Color.yellow, 210, 40, 30); a[count++] = new RotatingNStar(Color.cyan, 150, 140, 5, 20, 40); public void update(graphics g) { if(buf == null) { buf = createimage(getwidth(), getheight()); Graphics2D g2 = (Graphics2D)buf.getGraphics(); g2.clearrect(0, 0, getwidth(), getheight()); paint(g2); g.drawimage(buf, 0, 0, this); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; for(int i = 0; i < count; ++i) a[i].draw(g2); public void start() { go = true; (new Thread(new MyRun())).start(); public void stop() { go = false; class MyRun implements Runnable { public void run() { time = 0.001 * System.currentTimeMillis(); while(go) { try { Thread.sleep(100); catch(exception e) { double dt = 0.001 * System.currentTimeMillis() - time; for(int i = 0; i < count; ++i) a[i].addtime(dt); time += dt; repaint(); interface Animation { public void addtime(double dt); public void draw(graphics2d g); static class FlyingCircle implements Animation { Paint col; double gx, gy, rad, vx, vy; public FlyingCircle(Paint c, double x, double y, double r, double vx1, double vy1) { col = c; gx = x; gy = y; rad = r; vx = vx1; vy = vy1; public void addtime(double dt) { gx += vx*dt; gy += vy*dt; if(gx < 0 && vx < 0 gx > 300 && vx > 0) vx = -vx; if(gy < 0 && vy < 0 gy > 200 && vy > 0) vy = -vy; public void draw(graphics2d g) { g.setpaint(col); 3

g.filloval((int)(gx-rad), (int)(gy-rad), (int)rad*2, (int)rad*2); static class ResizingSquare implements Animation { Paint col; double gx, gy, len, time; public ResizingSquare(Paint c, double x, double y, double l) { col = c; gx = x; gy = y; len = l; time = 0.0; public void addtime(double dt) { time += dt; public void draw(graphics2d g) { int l = (int)(2.0*len + len*math.sin(time)) / 4; g.setpaint(col); g.fillrect((int)(gx-l), (int)(gy-l), (int)(l*2), (int)(l*2)); vy = vy + *dt (*1 ) static class GravityCircle implements Animation { Paint col; double gx, gy, rad, vx, vy; public GravityCircle(Paint c, double x, double y, double r, double vx1, double vy1) { col = c; gx = x; gy = y; rad = r; vx = vx1; vy = vy1; public void addtime(double dt) { gx += vx*dt; gy += vy*dt; vy += 40*dt; // *1 if(gx < 0 && vx < 0 gx > 300 && vx > 0) vx = -vx; if(gy < 0 && vy < 0 gy > 200 && vy > 0) vy = -vy; public void draw(graphics2d g) { g.setpaint(col); g.filloval((int)(gx-rad), (int)(gy-rad), (int)rad*2, (int)rad*2); sin/cos (*2) gx gy static class RoundingCircle implements Animation { Paint col; double gx, gy, rad1, rad2, time = 0.0; public RoundingCircle(Paint c, double x, double y, double a, double b) { col = c; gx = x; gy = y; rad1 = a; rad2 = b; public void addtime(double dt) { time += dt; public void draw(graphics2d g) { int x = (int)(gx + rad2*math.cos(time) - rad1); // *2 4

int y = (int)(gy + rad2*math.sin(time) - rad1); // *2 g.setpaint(col); g.filloval(x, y, (int)rad1*2, (int)rad1*2); (*3) static class OscillatingOval implements Animation { Paint col; double gx, gy, rad, time = 0.0; public OscillatingOval(Paint c, double x, double y, double r) { col = c; gx = x; gy = y; rad = r; public void addtime(double dt) { time += dt; public void draw(graphics2d g) { int d = (int)(0.5 * rad * Math.cos(time)); int r1 = (int)rad + d, r2 = (int)rad - d; g.setpaint(col); g.filloval((int)(gx-r1), (int)(gy-r2), r1*2, r2*2); N fillpolygon() sin/cos (1 ) sin/cos time (*4) static class RotatingNStar implements Animation { Paint col; double gx, gy, rad1, rad2, time = 0.0; int num; int[] xpts, ypts; public RotatingNStar(Paint c, double x, double y, int n, double a, double b) { col = c; gx = x; gy = y; num = n; rad1 = a; rad2 = b; xpts = new int[2*n]; ypts = new int[2*n]; public void addtime(double dt) { time += dt; public void draw(graphics2d g) { double dt = Math.PI / num; for(int i = 0; i < 2*num; ++i) { double r = rad1; if(i%2 == 0) r = rad2; // *4 xpts[i] = (int)(gx + r * Math.cos(time+i*dt)); // *4 ypts[i] = (int)(gy + r * Math.sin(time+i*dt)); // *4 g.setpaint(col); g.fillpolygon(xpts, ypts, 2*num); 5

2 2.1 System.out.println(...) Java ( ) appletviewer appletviewer ( ) import java.awt.*; import javax.swing.*; public class R8Sample1 extends JApplet { int []x = new int[101]; int []y = new int[101]; public void init() { for(int i = 0; i <= 100; ++i) { double theta = 0.01 * 2 * Math.PI * i; x[i] = (int)(100*math.cos(theta)) + 100; y[i] = (int)(100*math.sin(2*theta)) + 100; public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; g2.setstroke(new BasicStroke(3f)); for(int i = 0; i <= 100; ++i) { g2.drawline(x[i-1], y[i-1], x[i], y[i]); Java 1 ArrayIndexOutOfBoundsException (array) (index) (bounds) (out) R8Sample1 paint R8Sample1.java 16 i 0 x[i-1] x -1 for(int i = 1; 2.2 Java Java try 6

1: Java try {... (1) catch( ) {... (2) (1) (2) try (1) (2) (2) 2.3 ( ) Throwable --- Error --- ClassFromatError ---.class NoClassDefFoundError ---.class...... Exception --- RuntimeException --- NumberFormatException --- NullPointerException --- null IndexOutOfBoundsException ---...... IOException --- IntrruptedException --- IllegalAccessException ---...... Exception try 7

try {... A try {... B... catch(numberformatexception e) { (1)... C catch(exception e) { (2) B NumberFormatException (1) (2) A C try (2) 1 2.4 System.err.println(...) System.err.println() System.out.println() printstacktrace() 3 printstacktrace() import java.io.*; public class R9Sample2 { public static void main(string[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while(true) { try { System.out.print("x> "); String s = in.readline(); if(s.equals("")) break; int x = (new Integer(s)).intValue(); System.out.print("y> "); int y = (new Integer(in.readLine())).intValue(); System.out.println("x + y = " + (x+y)); catch(exception e) { System.err.println("Oops! some error occured..."); e.printstacktrace(); System.err.println("Continue..."); 1 Exception? Exception 8

NumberFormatException catch % java R8Sample2 x> 3 y> 5 x + y = 8 x> 3 OK y> a Oops! some error occured... java.lang.numberformatexception: a Continue... at java.lang.integer.parseint(integer.java:426) at java.lang.integer.<init>(integer.java:567) at R8Sample2.main(R8Sample2.java:13) x> b Oops! some error occured... java.lang.numberformatexception: b Continue... x> 1 y> 2 x + y = 3 at java.lang.integer.parseint(integer.java:426) at java.lang.integer.<init>(integer.java:567) at R8Sample2.main(R8Sample2.java:11) OK x> [ret] % 2.5 throw throw ; (Throwable ) Exception ( ) throw new Exception(" "); catch 9

try {... catch(exception e) { System.err.println("..."); throw e; // 2.6 throws catch ( )... ( ) throws,... { throws throw catch 1 public static void main(string[] args) throws Exception {... ( in.readline() IOException ) Error RuntimeException throws Thread.sleep() try...catch InterruptedException( ) try...catch 3 GUI GUI GUI(Graphical User Interface) GUI (GUI ) / ( GUI ) Java GUI JDK 1.1 Java 2 Java 2 Swing javax.swing ( JApplet 1 ) GUI ( Component ) 10

setbounds(int, int, int, int) (x,y) setforeground(color) setbackground(color) setfont(font) Color( ) 2 import java.awt.*; import javax.swing.*; public class R8Sample3 extends JApplet { Font fn = new Font("Helvetica", Font.BOLD, 20); JTextField t1 = new JTextField("text..."); JButton b1 = new JButton("B1"); JButton b2 = new JButton("B2"); public void init() { Container c = getcontentpane(); c.setlayout(null); c.add(t1); t1.setforeground(color.blue); t1.setbounds(20, 20, 200, 40); c.add(b1); b1.setfont(fn); b1.setbounds(20, 80, 60, 40); c.add(b2); b2.setfont(fn); b2.setbounds(120, 80, 60, 40); ( GUI ) init() getcontentpane() Container add() setlayout(null) off ( ) ( ) ( ) JLabel JButton JCheckbox JToggleButton JRadioButton ButtonGroup (JRadioButton ) JComboBox JSlider JSpinner SpinnerNumberModel (JSpinner ) SpinnerListModel (JSpinner ) JTextField JTextArea JList 1 2 (API ) 3 GUI ( ) Java a. 11

b. c. 3 (0 255) RGB d. ( ) e. 2 ( ) f. g. h. 4 ( ) GUI implements ActionListener public void actionperformed(actionevent evt) { run() mousepressed() GUI addactionlistener( ) * import java.awt.*; import java.awt.event.*; import javax.swing.*; public class R8Sample4 extends JApplet { Font fn = new Font("Helvetica", Font.BOLD, 16); JLabel l1 = new JLabel("*"); JButton b1 = new JButton("Press Me!"); public void init() { Container c = getcontentpane(); c.setlayout(null); c.add(l1); l1.setfont(fn); l1.setbounds(20, 20, 160, 40); c.add(b1); b1.setfont(fn); b1.setbounds(20, 80, 100, 40); b1.addactionlistener(new MyAdapter()); class MyAdapter implements ActionListener { public void actionperformed(actionevent e) { l1.settext(l1.gettext() + "*"); MyAdapter implements ActionListener actionperformed() static ( ) l1 1 1 actionperformed() 12

5 1. 1 ( MyAdapter 1 ) 2. extends XXX implements XXX 1 ( ) :... new MyAdapter()... class MyAdapter extends/implements XXX { // new MyAdapter()... new XXX() { //... ( ) import java.applet.applet; import java.awt.*; import java.awt.event.*; public class R8Sample5 extends Applet { Font fn = new Font("Helvetica", Font.BOLD, 16); JLabel l1 = new JLabel("*"); JButton b1 = new JButton("Press Me!"); public void init() { setlayout(null); l1.setbackground(color.white); add(l1); l1.setfont(fn); l1.setbounds(20, 20, 160, 40); add(b1); b1.setfont(fn); b1.setbounds(20, 80, 100, 40); b1.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { l1.settext(l1.gettext() + "*"); ); 6 actionperformed()? try...catch 13

import java.applet.applet; import java.awt.*; import java.awt.event.*; public class R8Sample6 extends Applet { Font fn = new Font("Helvetica", Font.BOLD, 16); JTextField t1 = new JTextField("1"); JLabel l1 = new JLabel(""); JButton b1 = new JButton("+1"); JButton b2 = new JButton("-1"); public void init() { setlayout(null); l1.setbackground(color.white); add(t1); t1.setfont(fn); t1.setbounds(20, 20, 160, 40); add(b1); b1.setfont(fn); b1.setbounds(20, 80, 60, 40); add(b2); b2.setfont(fn); b2.setbounds(100, 80, 60, 40); add(l1); l1.setbounds(20, 140, 300, 40); b1.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { ); try { t1.settext("" + (new Integer(t1.getText()).intValue() + 1)); catch(exception ex) { l1.settext(ex.tostring()); b2.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { ); try { t1.settext("" + (new Integer(t1.getText()).intValue() - 1)); catch(exception ex) { l1.settext(ex.tostring()); tostring() l1 settext() 4 R8Sample4 6 1 5 3 6 GUI A 8A 2 3 ( 1 ) WWW HTML cp1 report8a.html 1. Subject: Report 8A 2. 3. 1 14

4. 5. Q1. GUI? Q2. GUI? Q3. B 8B 5 6 (GUI ) 1 HTML cp1 report8b.html 1. Subject: Report 8B 2. 3. 4. 5. Q1. GUI?? Q2. GUI? Q3. C : 9B GUI 4 2 2 A4 1. Report9B 2. GUI 4 2 3. 4. Java 5. 6. / 3 7. report9a.html 1 ( 1/20 TA ) 2 GUI 3 gcopy 15