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

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

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

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

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

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 (

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 VII II. III. IV. ( a d) V. VI : this==null, T == N A ActionListener A addactionlistener C class D actionperforme

: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

: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

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;

: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

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

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 4 p.2 4 GUI java.awt.event.* import /* 1 */ import mouseclicked MouseListener implement /* 2 */ init addmouselistener(this) this /* 3 */ this mousec

Thread

4 p.2 4 GUI return; public void mousepressed(mouseevent e) { /* 5 */ public void mousereleased(mouseevent e) { /* 5 */ public void mouseentered(mousee

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

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

次の演習課題(1),(2)のプログラムを完成させよ

Microsoft PowerPoint prog1_doc2x.pptx

r2.dvi

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

Microsoft PowerPoint prog1_doc2.pptx

アプレットの作成

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

r14.dvi

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.

目 次 Java GUI 3 1 概要 クラス構成 ソースコード例 課題...7 i

ÿþ˜u#u·0¹0Æ0à0

ガイダンス

r4.dvi

< F2D834F838C A815B A CC>

ガイダンス

2

< F2D B838A835882CC8CF68EAE2E6A7464>

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

text_13.dvi

< F2D F B834E2E6A7464>


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

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

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

< F2D82518CC282CC D2E6A7464>

text_12.dvi

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

< F2D82B682E182F182AF82F12E6A7464>

r3.dvi

新・明解Java入門

< F2D92DE82E8914B82CC977088D32E6A7464>

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

r3.dvi

public class Kadai _02 { public static void main(string[] args) { MyFrame frame = new MyFrame("Kadai _02"); (2) フレームのクラス名は MyFrame とし 以下

問1

Java言語 第1回

< F2D BCA82CC978E89BA82CC8EC08CB12E6A7464>

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

< F2D825282CC947B909482CC A815B83682E6A>

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

< F2D82518E9F8AD CC834F CC8CFC82AB82C68D4C>

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

10K pdf

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

< F2D8EA CE909482CC92EA82852E6A7464>

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

以下に java.awt.graphics クラスの主なメソッドを示す (Graphics クラスの ) メソッド drawline(int x1, int y1, int x2, int y2) drawrect(int x, int y, int width, int height) fillr

r2.dvi

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

untitled

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学習教材

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

シミュレーションの簡単な例 GUI 無しのシミュレーションを作る GUI を作る パラメタを設定するデモンストレーションをする 2 オブジェクト指向プログラミング特論

2

アジェンダ 1 グラフィカルなインタフェース GUI(Graphical User Interface) の基礎 2 Swing を利用する Swing の基礎知識 2

ガイダンス

r8.dvi

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

2

r14.dvi

< F2D82518E9F8AD CC95BD8D7388DA93AE2E6A7464>

IT プロジェクト

ガイダンス

ガイダンス

< F2D89BA8EE882C E6A7464>

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

public class MyFrame { private JPanel panel1; private JPanel panel2; private JPanel panel3; private JPanel panel4; private JLabel label1; private JTex

Microsoft PowerPoint - OOP.pptx

Microsoft PowerPoint - OOP.pptx

226


/ ( ) 8/7/ :21 p.2/64

< F2D E E6A7464>

PowerPoint Presentation

< F2D A839382CC906A2E6A7464>

GUIプログラムⅤ

解きながら学ぶJava入門編


Object MenuComponent MenuBar MenuItem Menu CheckboxMenuItem

Microsoft PowerPoint - prog10.ppt

< F2D A838B838D96402E6A7464>

Transcription:

2018 08 03 10:30 12:00 I. IV III II. III. IV. ( a d) V. VI. 70 III 30 100 60 : A ActionListener aa addactionlistener AE ActionEvent K KeyListener ak addkeylistener KE KeyEvent M MouseListener am addmouselistener ME MouseEvent pl System.out.println pf System.out.printf LeftRightButton3.java, LeftRightButton4.java, Guruguru.java, Point.java, ColorPoint.java 1

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 int width Rectangle public int height Rectangle public Rectangle(int x, int y, int width, int height) (x,y) width height Rectangle : x - X y - Y width - Rectangle height - Rectangle public Rectangle intersection(rectangle r) Rectangle Rectangle 2 Rectangle 2 : : r - Rectangle Rectangle Rectangle Rectangle public Rectangle union(rectangle r) 2

Rectangle Rectangle 2 Rectangle... : : r - Rectangle Rectangle Rectangle Rectangle java.lang Math Math... public static double random() 0.0 1.0 double... : 0.0 1.0 double : RectangleTest.java 1 import java.awt.color; 2 import java.awt.*; 3 import javax.swing.*; 4 5 public class RectangleTest extends JPanel { 6 private int width = 320, height = 320; 7 8 public RectangleTest() { 9 setpreferredsize(new Dimension(width, height)); 10 11 12 private Rectangle randomrect(int min, int w, int h) { 13 int rw = (int)( (i-1) ); 14 int rh = (int)( (i-2) ); 15 int rx = (int)( (i-3) ); 16 int ry = (int)( (i-4) ); 17 return new Rectangle(rx, ry, rw, rh); 18 19 20 @Override 21 protected void paintcomponent(graphics g) { 22 super.paintcomponent(g); 23 Rectangle rect1 = randomrect(50, width, height); 3

24 Rectangle rect2 = randomrect(50, width, height); 25 Rectangle union = (ii) ; 26 g.setcolor(color.blue); 27 g.fillrect(union.x, union.y, union.width, union.height); 28 g.setcolor(color.green); 29 g.fillrect(rect1.x, rect1.y, rect1.width, rect1.height); 30 g.setcolor(color.yellow); 31 g.fillrect(rect2.x, rect2.y, rect2.width, rect2.height); 32 Rectangle isect = (iii) ; 33 g.setcolor(color.red); 34 g.fillrect(isect.x, isect.y, isect.width, isect.height); 35 36 37 /* main */ 2 (green) (yellow) 2 (blue) 2 (i-1)(i-4) 4 (i-1) min w (i-2) min h (i-3) 0 (w - rw) (i-4) 0 (h - rh) Math random (i-1) (i-3) (ii) rect1 rect2 Rectangle (iii) rect1 rect2 Rectangle II. FontTester GUI 2 Hello, World! 16 Times New Roman Name: (fontlist) Size: Java : FontTester.java 1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class FontTester extends JPanel implements ActionListener { 4

6 String fontname = "Times New Roman"; 7 int fontsize = 16; 8 Font font = new Font(fontName, Font.PLAIN, fontsize); 9 String[] fontlist = { "Times New Roman", "Courier New", "Arial" ; 10 JTextField nametf, sizetf; 11 12 public FontTester() { 13 setpreferredsize(new Dimension(320, 80)); 14 nametf = new JTextField("Times New Roman", 16); 15 sizetf = new JTextField("16", 3); 16 add(new JLabel("Name:")); add(nametf); add(new JLabel("Size:")); add(sizetf); 17 (i-1) 18 (i-2) 19 20 21 private String selectfont(string init) { 22 for (String s: fontlist) { 23 if (s.tolowercase().startswith(init.tolowercase())) return s; 24 25 return Font.SERIF; 26 27 28 public void actionperformed(actionevent e) { 29 Object target = e.getsource(); 30 if (target == nametf) { 31 fontname = selectfont(nametf.gettext()); 32 else if (target == sizetf) { 33 fontsize = Integer.parseInt(sizeTF.getText()); 34 35 font = new Font(fontName, Font.PLAIN, fontsize); 36 repaint(); 37 38 39 @Override 40 protected void paintcomponent(graphics g) { 41 super.paintcomponent(g); 42 g.setfont(font); 43 g.drawstring("hello, World!", 16, 64); 44 45 46 public static void main(string[] args) { 47 SwingUtilities.invokeLater(() -> { 48 JFrame frame = new JFrame(""); 49 frame.add(new FontTester()); 50 frame.pack(); 51 frame.setvisible(true); 52 frame.setdefaultcloseoperation(jframe.exit_on_close); 53 ); 54 5

55 (i) (i-1)(i-2) 2 FontTester.java FontTester2.java : FontTester2.java 1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 5 public class FontTester2 (ii) { 6 String fontname = "Times New Roman"; 7 int fontsize = 16; 8 Font font = new Font(fontName, Font.PLAIN, fontsize); 9 String[] fontlist = { "Times New Roman", "Courier New", "Arial" ; 10 JTextField nametf, sizetf; 11 12 public FontTester2() { 13 setpreferredsize(new Dimension(320, 80)); 14 nametf = new JTextField("Times New Roman", 16); 15 sizetf = new JTextField("16", 3); 16 add(new JLabel("Name:")); add(nametf); add(new JLabel("Size:")); add(sizetf); 17 nametf.addactionlistener( 18 (iii) 19 ); 20 sizetf.addactionlistener( 21 (iv) 22 ); 23 24 /* selectfont, paintcomponent, main */ 25 (ii) (iii), (iv) FontTester2 (iii) (iv) /* FontTester.java */ III. (A). Java (B). Haskell (C). C (D). Prolog (i)? (ii)? 6

IV. Fish : Fish.java 1 public class Fish { 2 protected int age; 3 public void grow() { age++; 4 public void showname() { System.out.print(" "); 5 public void growshowname() { showname(); grow(); 6 3 Carp, Seriola, Mugil : Carp.java 1 public class Carp (i) { 2 public String color; 3 public Carp(String c) { color = c; 4 5 @Override 6 public void showname() { 7 if (age < 1000) { 8 System.out.print(color + " "); 9 else { 10 System.out.print(color + " "); 11 12 13 : Seriola.java 1 public class Seriola (i) { 2 @Override 3 public void showname() { 4 if (age == 0) { 5 System.out.print(" "); 6 else if (age <= 2) { 7 System.out.print(" "); 8 else { 9 System.out.print(" "); 10 11 12 : Mugil.java 1 public class Mugil (i) { 2 @Override 3 public void showname() { 4 if (age <= 1) { 5 System.out.print(" "); 6 else { 7

7 System.out.print(" "); 8 9 10 FishTest main : FishTest.java 1 public class FishTest { 2 public static void main(string[] args) { 3 Carp c = new Carp(""); 4 c.color = ""; 5 Fish[] fish = { c, new Seriola(), new Mugil() ; 6 7 int i, j; 8 for (i = 0; i < 3; i++) { 9 for (j = 0; j < 3; j++) { 10 fish[j].growshowname(); 11 12 System.out.println(); // 13 14 for (i = 0; i < 998; i++) { 15 for (j = 0; j < 3; j++) { 16 fish[j].grow(); 17 18 19 for (j = 0; j < 3; j++) { 20 fish[j].showname(); 21 22 System.out.println(); // 23 24 (i) 3 (ii) Carp color FishTest.java 4 Carp.java (iii) FishTest.java 4 main Fish age 0 8

V. Java GUI : TrafficLight.java 1 import java.awt.*; 2 import javax.swing.*; 3 4 public class TrafficLight extends JPanel (i) { 5 private int state = 2; 6 private volatile Thread thrd = null; 7 8 private Color changecolor(color c, int s) { 9 if (state == s) return c; 10 return Color.DARK_GRAY; 11 12 13 public TrafficLight() { 14 setpreferredsize(new Dimension(300, 150)); 15 JButton startbtn = new JButton("start"); 16 startbtn.addactionlistener( (ii) ); 17 JButton stopbtn = new JButton("stop"); 18 stopbtn.addactionlistener( (iii) ); 19 JButton nextbtn = new JButton("next"); 20 nextbtn.addactionlistener( (iv) ); 21 add(startbtn); add(stopbtn); add(nextbtn); 22 23 24 private void startanimation() { 25 if (thrd == null) { 26 thrd = (v) ; 27 thrd.start(); 28 29 30 31 private void stopanimation() { 32 thrd = null; 33 34 35 public void next() { 36 state++; 37 state %= 3; 38 repaint(); 39 40 41 @Override 42 protected void paintcomponent(graphics g) { 43 super.paintcomponent(g); 44 g.setcolor(changecolor(color.green, 0)); 45 g.filloval(10, 60, 80, 80); 46 g.setcolor(changecolor(color.yellow, 1)); 47 g.filloval(110, 60, 80, 80); 9

48 g.setcolor(changecolor(color.red, 2)); 49 g.filloval(210, 60, 80, 80); 50 51 52 public void run() { 53 Thread me = Thread.currentThread(); 54 while ( (vi) ) { 55 next(); 56 try { 57 Thread.sleep(1500); // 1.5 58 catch (InterruptedException e) { 59 60 61 62 public static void main(string[] args) { 63 SwingUtilities.invokeLater(() -> { 64 JFrame frame = new JFrame(""); 65 frame.add(new TrafficLight()); 66 frame.pack(); 67 frame.setvisible(true); 68 frame.setdefaultcloseoperation(jframe.exit_on_close); 69 ); 70 71 3 start 1.5 stop next (i)(vi) 10

LeftRightButton3.java, LeftRightButton4.java, Guruguru.java, Point.java, ColorPoint.java LeftRightButton3.java <import javax.swing.*; import java.awt.*; import java.awt.event.*; public class LeftRightButton3 extends JPanel { private int x = 20; public LeftRightButton3() { setpreferredsize(new Dimension(200, 70)); JButton lbtn = new JButton("Left"); JButton rbtn = new JButton("Right"); lbtn.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { x -= 10; repaint(); ); rbtn.addactionlistener(new ActionListener() { public void actionperformed(actionevent e) { x += 10; repaint(); ); setlayout(new FlowLayout()); add(lbtn); add(rbtn); @Override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawstring("hello WORLD!", x, 55); /* main */ LeftRightButton4.java import javax.swing.*; import java.awt.*; public class LeftRightButton4 extends JPanel { private int x = 20; public LeftRightButton4() { setpreferredsize(new Dimension(200, 70)); JButton lbtn = new JButton("Left"); JButton rbtn = new JButton("Right"); lbtn.addactionlistener(e -> { x -= 10; repaint(); ); rbtn.addactionlistener(e -> { x += 10; repaint(); ); setlayout(new FlowLayout()); add(lbtn); add(rbtn); @Override public void paintcomponent(graphics g) { 11

super.paintcomponent(g); g.drawstring("hello WORLD!", x, 55); /* main */ Guruguru.java import java.awt.*; import javax.swing.*; public class Guruguru extends JPanel implements Runnable { private int r = 50, x = 110, y = 70; private double theta = 0; // private volatile Thread thread = null; public Guruguru() { setpreferredsize(new Dimension(200, 180)); JButton startbtn = new JButton("start"); startbtn.addactionlistener(e -> startthread()); JButton stopbtn = new JButton("stop"); stopbtn.addactionlistener(e -> stopthread()); setlayout(new FlowLayout()); add(startbtn); add(stopbtn); startthread(); private void startthread() { if (thread == null) { thread = new Thread(this); thread.start(); private void stopthread() { thread = null; @Override public void paintcomponent(graphics g) { super.paintcomponent(g); // paintcomponent // g.drawstring("hello, World!", x, y); public void run() { Thread thisthread = Thread.currentThread(); for (; thread == thisthread; theta += 0.02) { x = 60 + (int)(r * Math.cos(theta)); y = 100 - (int)(r * Math.sin(theta)); repaint(); // paintcomponent try { Thread.sleep(30); // 30 catch (InterruptedException e) { /* main */ Point.java public class Point { public int x, y; public void move(int dx, int dy) { 12

x += dx; y += dy; public double distance() { return Math.sqrt(x * x + y * y); public void print() { System.out.printf("(%d, %d)", x, y); public void moveandprint(int dx, int dy) { print(); move(dx, dy); print(); public Point(int x0, int y0) { x = x0; y = y0; ColorPoint.java public class ColorPoint extends Point { private String[] cs = {"black", "red", "green",..., "white"; private String color; @Override public void print() { System.out.printf("<font color= %s >", getcolor()); super.print(); System.out.print("</font>"); // // public void setcolor(string c) { int i; for (i = 0; i < cs.length; i++) { if (c.equals(cs[i])) { color = c; return; // public ColorPoint(int x, int y, String c) { super(x, y); setcolor(c); if (color == null) color = "black"; public String getcolor() { return color; 13

RectangleTest.java FontTester.java TrafficLight.java 1.5 next 1.5 next 14

2018 08 03 I. 4, 4, 4 (i-1). (i-3). (ii). (iii). II. 4, 4, 5, 5 (i-1). (i-2) (ii). (iii). nametf.addactionlistener( ); (iv). sizetf.addactionlistener( );

III. 3 2 (i). (ii). IV. (i). 4, 6, 6 (ii). (iii). V. 3 6 (i). (ii). (iii). (iv). (v). (vi).