(Java) 2008 1 2008.8.14 15 1 E mail : sakkun@yokohama-cu.ac.jp
3 I 11 1 Java 15 1.1................ 15 1.1.1......................... 15 1.1.2............................ 16 1.2 Java............................. 19 1.2.1 Windows.................. 19 1.2.2.......................... 19 1.2.3.......................... 20 2 Java 23 2.1........................ 23 2.1.1 Sun SDK - JDK........................... 23 2.1.2.................... 23 2.1.3 Java...................... 24 2.2 Java............. 27 2.2.1.......................... 27 2.2.2 Java.......... 28 2.2.3................................... 29 2.3 Java....................... 30 2.3.1................................. 30 2.3.2................................. 30 2.3.3................................... 31 3 Java GUI 33 3.1 GUI................................... 33 3.1.1 GUI...................... 33 3.1.2 GUI..................... 33 3.2 GUI........................ 35 3.2.1 GUI......................... 35 3.2.2.................... 35
4 3.2.3 sample04.java..................... 37 3.3.................. 39 3.3.1 sample05.java..................... 39 3.3.2............................... 39 4 Java 41 4.1........................... 41 4.1.1....................... 41 4.2 Java.................... 42 4.2.1 Java................. 42 4.2.2 sample06.java sample07.java........ 43 4.3....................... 44 4.3.1............................. 44 4.3.2 sample07.java......................... 44 5 47 5.1.............................. 47 5.1.1 Java................. 47 5.1.2................... 47 5.1.3................................. 48 5.1.4.......................... 48 5.2 Java.................. 50 5.2.1 sample08.java......................... 50 5.3................................ 51 5.3.1........ 51 II 55 6 57 6.1.......................... 57 6.1.1 sample01.java................... 57 6.1.2 sample02.java................... 59 6.1.3 sample03.java................... 60 6.1.4 sample04.java................... 64 6.1.5 sample05.java................... 65 6.1.6 sample06.java................... 67 6.2.......................... 68 6.2.1 sample07.java................... 68 6.2.2 sample08.java................... 69
7 71 7.1............................. 71 7.1.1 Java...................... 71 7.1.2.......................... 71 5 III Java 73 8 75 8.1 Java............................. 75 8.1.1.................................. 75 8.1.2.............................. 75 8.1.3................................. 75 8.2................................. 76 8.2.1................................... 76 8.2.2.................................. 76 8.2.3................................. 76 8.3....................................... 77 8.3.1................................. 77 8.3.2................................. 77 8.3.3................................. 77 8.3.4.......................... 78 8.4....................... 78 8.4.1............................. 78 8.4.2................................. 79 8.4.3.................................. 79 8.4.4.......................... 80 8.5........................................ 80 8.5.1.................................. 80 8.5.2.................................. 80 8.5.3.................................. 81 8.5.4 switch case.............................. 81 8.5.5 for................................... 82 8.5.6 while.................................. 83 8.5.7 do while.............................. 83 8.5.8 break continue........................... 84 8.5.9................................... 85 8.5.10................................. 86
6 9 89 9.1.............................. 89 9.2.................................. 89 9.2.1.............................. 89 9.2.2................................. 91 10 GUI 93 10.1 GUI.................................... 93 10.2................................... 99 10.2.1 BorderLayout............................. 99 10.2.2 GridLayout............................... 99 A 101 A.1 Java............................ 101 A.2........................... 103
7 1............................. 13 1.1............................ 16 1.2.................... 17 1.3 LOGO..................... 18 2.1 Java...... 26 2.2.... 27 3.1 GUI sample03.java........... 34 4.1............................. 41 4.2 Java........... 42 7.1................ 72 8.1 if......................... 82 8.2 switch case.................. 83 8.3 for()....................... 84 8.4 while....................... 85 8.5 do while................... 86
9 2.1........................ 24 3.1 GUI............... 36 3.2................ 37 8.1 Java............................. 77 8.2................................ 77 8.3................................ 78 8.4................................ 78 8.5 Java............................. 79 8.6.......................... 80 8.7.......................... 80 8.8 Math............... 87 9.1 Color........................ 92
I
13 Java Java Java 1: 2003
14 Java Java Java
15 1 Java 1.1 Java 1.1.1 1.1 1 1 API(Application Program Interface)
16 1 Java 1.1: 1.1.2 Java Visual BASIC Windows
1.1. 17 Java Java 1980 xxx 1.2:
18 1 Java Smalltalk LOGO LOGO (forward) (right,left) 1.3: LOGO GUI(=Graphical User Interface) GUI
1.2. Java 19 1.2 Java Java Web 1.2.1 Windows Ctrl+Alt+Del 2 Ctrl Alt Delete * OK Windows 1.2.2 Web Java Web Java 1. (Internet Explorer) 2. http://sakkun.cc.yokohama-cu.ac.jp/ Enter 3. 2008 (Java) 2 Windows
20 1 Java 4. Java 5. Web Web 6. II 7. II 71 1.2.3 Java 1. Web (Java) 2. 3. 4. ( ) 5. z: OK
1.2. Java 21 6. z: jsamples
23 2 Java 2.1 2.1.1 Sun SDK - JDK Sun Microsystems Software Development Kit= SDK Java Java Development Kit= JDK (javac) 1 (java) (appletviewer) SDK(Java2 Development Kit, Standard Edition = J2SE) Sun Web Windows98 Pentium II 100MB SDK CD-ROM SDK JDK 2.1.2 SDK 1. 2. cmd 1
24 2 Java 3. z: cd jsamples Enter 1. javac sample01.java Enter 2. dir Enter sample01.class 3. start sample01.html Enter sample01.java HTML sample01.html Windows CPad for J2SDK z: Java 2.1: notepad "filename.java" notepad " javac filename.java java filename <applet> HTML appletviewer webpage.html Windows start webpage.html Web 2.1.3 Java Java
2.1. 25 Java Windows Windows MacOS MacOS Java Windows MacOS Linux Java 2.1 OS OS Java OS Virtual Machine= VM OS VM OS Java
26 2 Java Windows Compiler Linux Compiler MacOS Compiler Windows Windows Linux Linux MacOS MacOS OS Java Windows Compiler Linux Compiler MacOS Compiler (class ) Windows Java VM (class ) Linux Java VM (class ) MacOS Java VM OS OS Java Virtual Machine Windows Linux MacOS 2.1: Java
2.2. Java 27 2.2 Java Java sample01.java 2.2.1 Java 0 width-1 0 height width height-1 0 1 2 3 4 5 6 0 1 2 3 4 5 6 RGB R (0 255) G B 1/72-1/75 2.2: 2.2
28 2 Java Red, Blue, Green Java 0 255 256 2 8 = 256 0 255 Java 2.2 width height HTML 1024 768 2.2.2 Java Java III C sample01.java g. (,... ) ; Java z: jsamples> javac sample01.java z: jsamples> start sample01.html sample01.java notepad "sample01.java"
2.2. Java 29 Web Ctrl 2.2.3 g.setcolor( Color. ) ; g.setcolor( new Color( ) ) ; 0 255.java HTML
30 2 Java 2.3 Java Java C 2.3.1 Java,... ; = GUI [ ] 0 2.3.2
2.3. Java 31 if else C == 2 switch case... for while do C 2.3.3 sample02.java Web sample02.java
32 2 Java 2 2
33 3 Java GUI 3.1 GUI GUI 3.1.1 GUI Windows Graphical User Interface GUI Java GUI GUI Java GUI 1 AWT Abstract Window Toolkit Swing Toolkit 3.1.2 GUI GUI new init() add() add() 1 GUI
34 3 Java GUI Label TextField Choice Button Checkbox Checkbox (grouped by CheckboxGroup) Scrollbar TextArea Canvas 3.1: GUI sample03.java paint() GUI GUI
3.2. GUI 35 3.2 GUI GUI sample03.java sample04.java 3.2.1 GUI AWT GUI sample03.java import java.applet.applet; import java.awt.*; import java.awt.event.*; // public void extends Applet implements { // // GUI GUI ; public void init() { // GUI GUI = new GUI ( ) ; add(gui ) ; // add() GUI. ( ) ; // this public void ( ) { // GUI if(.getsource() == GUI ) { 3.1 GUI 3.2.2 GUI
36 3 Java GUI 3.1: GUI Button ActionListener actionperformed TextField addactionlistener ActionEvent TextArea Choice ItemListener itemstatechanged Checkbox additemlistener ItemEvent Scrollbar AdjustmentListener adjustmentvaluechanged addadjustmentlistener AdjustmentEvent import java.applet.applet; import java.awt.*; import java.awt.event.*; // public void extends Applet implements MouseListener, MouseMotionListener { public void init() { addmouselistener( ) ; addmousemotionlistener( ) ; // this // MouseListener public void mousepressed(mouseevent e) { public void mousereleased(mouseevent e) { public void mouseclicked(mouseevent e) { public void mouseentered(mouseevent e) { public void mouseexited(mouseevent e) { // MouseMotionListener public void mousemoved(mouseevent e){ public void mousedragged(mouseevent e) {
3.2. GUI 37 3.2 3.2: MouseListener mousepressed addmouselistner mousereleased mouseclicked mouseentered mouseexited MouseMotionListener mousemoved addmousemotionlistener mousedragged MouseEvent GUI 3.2.3 sample04.java sample04.java paint() fillrect() paint() repaint() repaint Color Color bgcolor = Color.lightGray ; Color repaint() ;
38 3 Java GUI paint setcolor fillrect getsize().width getsize().height fillrect BMI BMI BMI 20 24 22
3.3. 39 3.3 sample05.java 3.3.1 sample05.java GUI 3.3.2 Windows Windows Windows
40 3 Java GUI
41 4 Java 4.1 GUI 4.1.1 1 4.1 4.1: 1 24 0.05
42 4 Java 4.2 Java Java 4.2.1 Java Java (run) (paint) run paint 4.2 start run stop repaint repaint paint paint sleep() 4.2: Java Thread sleep
4.2. Java 43 2 4.2.2 sample06.java sample07.java saple06.java sample07.java 2
44 4 Java 4.3 sample06.java sample07.java implements Runnable Thread sleep() 4.3.1 paint() repaint() update() sample05.java update() paint() 4.3.2 sample07.java sample07.java sample07a.java (sample07a.java) 1 //
4.3. 45 2 import java.applet.* ; 3 import java.awt.* ; 4 5 public class sample07a extends Applet implements Runnable { 6 int t = 0 ; // (deg.) 7 double hankei = 150 ; // 8 Thread th = null ; 9 Image backimg = null ; 10 Graphics backg = null ; 11 12 public void start() { 13 backimg = createimage(400,400) ; 14 backg = backimg.getgraphics() ; 15 paintback() ; 16 repaint() ; 17 th = new Thread(this) ; 18 th.start() ; 19 20 21 public void run() { 22 while( th!= null ) { 23 paintback() ; 24 repaint() ; 25 t = (t+5)%360 ; 26 try { 27 th.sleep(10) ; // 10ms = 0.01 28 29 catch (InterruptedException e) { 30 31 32 33 34 public void paintback() { 35 double a ; 36 int x, y ; 37 if( backg!= null ) { 38 backg.setcolor(color.white) ; 39 backg.fillrect(10,10,380,380) ; 40 a = (double)t*math.pi/180.0 ; 41 x = (int)( hankei * Math.cos(a) ) ; 42 y = (int)( hankei * Math.sin(a) ) ; 43 backg.setcolor(color.blue) ; 44 backg.filloval( x+200-10, -y+200-10, 21, 21 ) ; 45 46 47 48 public void update(graphics g) { 49 paint(g) ; 50 51 public void paint(graphics g) {
46 4 Java 52 if( backimg!= null ) { 53 g.drawimage(backimg,0,0,this) ; 54 55 56 GUI GUI boolean isrunning isrunning true false run() isrunning true bt.setlabel(" ") ; bt.repaint() ;
47 5 5.1 GUI 5.1.1 Java Java 1 5.1.2 InputStream InputStream f = new FileInputStream( ) ; URL url = new URL( URL ) ; URL url = new URL( getdocumentbase(), ) ; InputStream f = url.openstream() ; // url BufferedReader infile = new BufferedReader(new InputStreamReader(f)) ; String record ; record = infile.readline() ; // infile.close() ; 1
48 5 5.1.3 try {...... catch( e ) {... e // catch(... ) {... FileNotFoundException IOException 5.1.4 Image Image mypict ; Applet init() mypict = getimage( getdocumentbase(), ) ; Applet paint() g.drawimage( mypict, X Y, this ) ; drawimage Graphics sample07a JPEG GIF PNG Windows BMP MediaTracker
5.1. 49 MediaTracker MediaTracker mt = new MediaTracker(this) ; // init() mypict1 = getimage( getdocumentbase(), "pict1.bmp" ) ; mypict2 = getimage( getdocumentbase(), "pict2.bmp" ) ; mt.addimage( mypict1, 0 ) ; // mt.addimage( mypict2, 1 ) ; try { // try catch mt.waitforall() ; // catch( InterruptedException e ) {
50 5 5.2 Java 5.2.1 sample08.java sample08.java
5.3. 51 5.3 Java 5.3.1 Web Java GUI Java main (GUI ) public class myapp1 { static public void main(string[] args) { System.out.println("*** This is a smallest Java application ***") ; public myapp1.java main static public javac java.class Z: jsamples> notepad "myapp1.java" (A ) Z: jsamples> javac myapp1.java Z: jsamples> java myapp1 *** This is a smallest Java application *** Z: jsamples> GUI Java
52 5 Window Frame JFrame import java.awt.* ; import java.awt.event.* ; public class myapp2 { static public void main(string[] args) { // ( ) Frame myframe = new Frame("** My Application using GUI **") ; myframe.setsize(400,400) ; // myframe.setvisible(true) ; // myframe.addwindowlistener( // new WindowAdapter() { public void windowclosing(windowevent e) { System.exit(0) ; // ) ; myapp2.java myapp1 sample01.java run01.java import java.awt.* ; import java.awt.event.* ; public class run01 { static public void main(string[] args) { // ( ) Frame myframe = new Frame("** Running an applet as an application **") ; // sample01 myapplet = new sample01() ; myframe.setsize(520,450) ; // myframe.add(myapplet) ; // myapplet.init() ; // myframe.setvisible(true) ; // myapplet.start() ; //
5.3. 53 // myframe.addwindowlistener( new WindowAdapter() { public void windowclosing(windowevent e) { System.exit(0) ; // ) ; sample01 sample01.java run01.java java run01 sample01
II
57 6 6.1 6.1.1 sample01.java 1 // Java Applet 2 import java.applet.* ; // 3 import java.awt.* ; // 4 5 // sample01 ( ), 6 // ( ) 7 public class sample01 extends Applet { 8 9 // paint 10 public void paint (Graphics g) { 11 // 12 setforeground(color.black) ; 13 setbackground(color.green) ; 14 15 // 16 g.drawline( 0,0, 50, 50 ) ; 17 g.drawline( 0,0, 3, 10 ) ; 18 g.drawline( 0,0, 10, 3 ) ; 19 g.drawstring("(0,0)",50,50) ; 20 21 // 22 g.drawstring( "Rectangles", 50, 110 ) ; 23 g.drawrect( 50, 120, 100, 100 ) ; 24 g.fillrect( 50, 230, 100, 100 ) ; 25 g.drawstring( "Round-Rectangles", 210, 110 ) ; 26 g.drawroundrect( 210, 120, 100, 100, 20, 20 ) ; 27 g.fillroundrect( 210, 230, 100, 100, 20, 20 ) ; 28 g.drawstring( "Ovals", 370, 110 ) ; 29 g.drawoval( 370, 120, 100, 100) ; 30 g.filloval( 370, 230, 100, 100) ; 31 g.drawstring("line", 50, 350 ) ;
58 6 32 g.drawline( 50, 360, 470, 360 ) ; 33 34 // 35 g.drawline( 519, 449, 519-50, 449-50 ) ; 36 g.drawline( 519, 449, 519-3, 449-10 ) ; 37 g.drawline( 519, 449, 519-10, 449-3 ) ; 38 g.drawstring("(519,449)",519-50,449-50) ; 39 40 HTML sample01.html sample01.java HTML 1 <html> 2 <head> 3 <title>java Sample Program (1)</title> 4 </head> 5 <body> 6 <h2 align="center">java Sample Program (1)</h2> 7 <p align="center"> 8 <!-- APPLET HTML --> 9 <applet code="sample01.class" width="520" height="450"> 10 </applet> 11 </p> 12 </body> 13 </html> HTML <applet code=".class" width=" ( )" height=" ( )"> </applet> sample02.html
6.1. 59 6.1.2 sample02.java 1 // Java 2 import java.applet.* ; 3 import java.awt.* ; 4 5 public class sample02 extends Applet { 6 7 //, 8 // int[] b_x = new int[5] ; 9 int[] b_x = { 50, 100, 150, 200, 250 ; 10 int[] b_y = { 100, 50, 30, 100, 70 ; 11 12 public void paint (Graphics g) { 13 // 14 int i, j ; 15 int[] y = new int[5] ; 16 // 17 Color[] cols = { 18 Color.red,Color.green,Color.blue, // 19 new Color(255,255,0) ; // RGB 20 for( i=0 ; i<4 ; i++ ) { 21 g.setcolor( cols[i] ) ; // 22 for( j=0 ; j<5 ; j++ ) y[j] = b_y[j]+i*50 ; // 23 g.drawpolyline(b_x,y,5) ; 24 25 26 27
60 6 6.1.3 sample03.java 1 import java.awt.* ; 2 import java.awt.event.* ; 3 import java.applet.* ; 4 5 public class sample03 extends Applet 6 implements ActionListener, 7 ItemListener, 8 AdjustmentListener, 9 MouseListener { 10 11 Button bt = new Button("Reset") ; 12 TextField tf = new TextField(10) ; 13 TextArea ta = new TextArea(10,40) ; 14 Choice ch = new Choice() ; 15 int colorindex = 0 ; 16 String[] colornames = { 17 "Red", "Green", "Blue", "Yellow", "Magenta", "Cyan", "White", "Black" 18 ; 19 Color[] colorobj = { 20 Color.red, Color.green, Color.blue, Color.yellow, Color.magenta, 21 Color.cyan, Color.white, Color.black 22 ; 23 Checkbox cb1 = new Checkbox("Fortran") ; 24 Checkbox cb2 = new Checkbox("PL/I") ; 25 Checkbox cb3 = new Checkbox("Basic") ; 26 Checkbox cb4 = new Checkbox("C or C++") ; 27 Checkbox cb5 = new Checkbox("Java") ; 28 CheckboxGroup cg = new CheckboxGroup() ; 29 Checkbox cg1 = new Checkbox("Never", true, cg) ; 30 Checkbox cg2 = new Checkbox("Sometimes",false,cg) ; 31 Checkbox cg3 = new Checkbox("Often", false,cg) ; 32 Scrollbar sc = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 100+10 ) ; 33 DrawingCanvas mycanvas = new DrawingCanvas(300,225) ; 34 35 public void init() { 36 int i ; 37 for( i=0 ; i<colornames.length ; i++ ) 38 ch.additem(colornames[i]) ; 39 ch.select(colornames[colorindex]) ; 40 add(new Label("TextField:",Label.RIGHT)) ; add(tf) ; 41 add(new Label("BackGround:",Label.RIGHT)) ; add(ch) ; 42 add(bt) ; 43 add(cb1) ; add(cb2) ; add(cb3) ; add(cb4) ; add(cb5) ; 44 add(cg1) ; add(cg2) ; add(cg3) ; 45 add(sc) ; 46 add(ta) ; 47 add(mycanvas) ; 48
6.1. 61 49 tf.addactionlistener(this) ; 50 bt.addactionlistener(this) ; 51 ch.additemlistener(this) ; 52 cb1.additemlistener(this) ; 53 cb2.additemlistener(this) ; 54 cb3.additemlistener(this) ; 55 cb4.additemlistener(this) ; 56 cb5.additemlistener(this) ; 57 sc.addadjustmentlistener(this) ; 58 addmouselistener(this) ; 59 60 61 public void paint(graphics g) { 62 int w, h, c ; 63 w = getsize().width ; 64 h = getsize().height ; 65 g.setcolor(colorobj[colorindex]) ; 66 g.fillrect(0,0,w,h) ; 67 68 69 // ActionListener 70 public void actionperformed(actionevent e) { 71 if( e.getsource() == tf ) { 72 ta.append("caught: TextField\n") ; 73 74 else if( e.getsource() == bt ) { 75 tf.settext("") ; 76 colorindex=0 ; 77 ch.select(colorindex) ; 78 cb1.setstate(false) ; cb2.setstate(false) ; cb3.setstate(false) ; 79 cb4.setstate(false) ; cb5.setstate(false) ; 80 cg1.setstate(true) ; 81 sc.setvalue(50) ; 82 ta.append("caught: Button\n") ; 83 repaint() ; 84 85 86 87 // ItemListener 88 public void itemstatechanged(itemevent e) { 89 if( e.getsource() == ch ) { 90 ta.append("caught: Choice\n") ; 91 colorindex = ch.getselectedindex() ; 92 repaint() ; 93 94 else if( e.getsource() instanceof Checkbox ) { 95 ta.append("caught: Checkbox ["+ 96 (cb1.getstate()? "ON ":"OFF ")+ 97 (cb2.getstate()? "ON ":"OFF ")+ 98 (cb3.getstate()? "ON ":"OFF ")+
62 6 99 (cb4.getstate()? "ON ":"OFF ")+ 100 (cb5.getstate()? "ON ":"OFF ")+"]\n") ; 101 102 103 104 // AdjustmentListener 105 public void adjustmentvaluechanged(adjustmentevent e) { 106 if( e.getsource() == sc ) { 107 ta.append("caught: Scrollbar v="+sc.getvalue()+"\n") ; 108 109 110 111 // MouseListener 112 public void mouseentered(mouseevent e) { 113 public void mouseexited(mouseevent e) { 114 public void mousepressed(mouseevent e) { 115 public void mousereleased(mouseevent e) { 116 public void mouseclicked(mouseevent e) { 117 ta.append("clicked at ("+e.getx()+","+e.gety()+")\n") ; 118 119 120 121 // Canvas public 122 // -- public 123 class DrawingCanvas extends Canvas 124 implements MouseListener, MouseMotionListener { 125 public int initx=-1, inity=-1, lastx=-1, lasty=-1 ; 126 127 public DrawingCanvas(int w, int h) { 128 setsize(w,h) ; 129 addmouselistener(this) ; 130 addmousemotionlistener(this) ; 131 132 133 public void update(graphics g) { 134 paint(g) ; 135 136 public void paint(graphics g) { 137 g.drawline( initx, inity, lastx, lasty ) ; 138 initx = lastx ; inity = lasty ; 139 140 141 // MouseListener 142 public void mousepressed(mouseevent e) { 143 initx = e.getx() ; inity = e.gety() ; 144 145 public void mousereleased(mouseevent e) { 146 public void mouseclicked(mouseevent e) { 147 public void mouseentered(mouseevent e) { 148 public void mouseexited(mouseevent e) {
6.1. 63 149 // MouseMotionListener 150 public void mousemoved(mouseevent e) { 151 public void mousedragged(mouseevent e) { 152 lastx = e.getx() ; lasty = e.gety() ; 153 repaint() ; 154 155
64 6 6.1.4 sample04.java 1 // GUI 2 import java.applet.* ; 3 import java.awt.* ; 4 import java.awt.event.* ; 5 6 public class sample04 extends Applet 7 implements ActionListener { 8 // 9 Button b1 = new Button(" ") ; 10 TextField t1 = new TextField(5) ; 11 TextField t2 = new TextField(5) ; 12 TextArea t3 = new TextArea(2,50) ; 13 14 public void init() { 15 add(new Label(" [kg]:",label.right)) ; add(t1) ; 16 add(new Label(" [cm]:",label.right)) ; add(t2) ; 17 add(b1) ; add(t3) ; 18 b1.addactionlistener(this) ; 19 20 21 public void actionperformed(actionevent e) { 22 if( e.getsource() == b1 ) { 23 float taijuu, sincho, himando ; 24 String s ; 25 s = t1.gettext() ; taijuu = Float.valueOf(s).floatValue() ; 26 s = t2.gettext() ; sincho = Float.valueOf(s).floatValue() ; 27 if( sincho > 0 && taijuu > 0 ) { 28 himando = taijuu*1.0e7f/(sincho*sincho*sincho) ; 29 s = " ( ) "+himando+" \n" ; 30 if( himando < 100 ) { 31 t3.settext(s+" ") ; 32 33 else if( himando > 140 ) { 34 t3.settext(s+" ") ; 35 36 else { 37 t3.settext(s+" ") ; 38 39 40 else { 41 t3.settext( "" ) ; 42 43 44 45 46
6.1. 65 6.1.5 sample05.java 1 import java.applet.* ; 2 import java.awt.* ; 3 import java.awt.event.* ; 4 5 public class sample05 extends Applet 6 implements ActionListener, 7 ItemListener, 8 MouseListener { 9 10 int shapeid = 0 ; // 0:Circle 1:Square 2:Cross 11 int colorid = 0 ; 12 Color[] colortable = {Color.black,Color.red,Color.green,Color.blue ; 13 Button clearbutton = new Button("Clear") ; 14 Choice shapechoice = new Choice() ; 15 Choice colorchoice = new Choice() ; 16 String[] itemshape = {"Circle","Square","Cross" ; 17 String[] itemcolor = {"Black", "Red", "Green", "Blue" ; 18 int lastx=-1, lasty=-1 ; 19 boolean clearflag = true ; 20 21 public void init() { 22 int i ; 23 for( i=0 ; i<3 ; i++ ) { 24 shapechoice.additem(itemshape[i]) ; 25 26 for( i=0 ; i<4 ; i++ ) { 27 colorchoice.additem(itemcolor[i]) ; 28 29 add(clearbutton) ; add(shapechoice) ; add(colorchoice) ; 30 clearbutton.addactionlistener(this) ; 31 shapechoice.additemlistener(this) ; 32 colorchoice.additemlistener(this) ; 33 addmouselistener(this) ; 34 setbackground(color.white) ; 35 36 37 public void update(graphics g) { 38 paint(g) ; 39 40 public void paint(graphics g) { 41 if( clearflag ) { 42 g.setcolor(color.white) ; 43 g.fillrect(0,0,getsize().width,getsize().height) ; 44 g.setcolor(color.black) ; 45 g.drawrect(0,0,getsize().width-1,getsize().height-1) ; 46 clearflag = false ; 47 48 else {
66 6 49 g.setcolor(colortable[colorid]) ; 50 switch( shapeid ) { 51 case 0: g.filloval(lastx-3,lasty-3,7,7) ; break ; 52 case 1: g.fillrect(lastx-3,lasty-3,7,7) ; break ; 53 case 2: g.drawline(lastx-3,lasty,lastx+3,lasty) ; 54 g.drawline(lastx,lasty-3,lastx,lasty+3) ; break ; 55 56 57 58 59 public void actionperformed(actionevent e) { 60 if( e.getsource() == clearbutton ) { 61 clearflag = true ; repaint() ; 62 63 64 public void itemstatechanged(itemevent e) { 65 if( e.getsource() == shapechoice ) { 66 shapeid = shapechoice.getselectedindex() ; 67 68 else if( e.getsource() == colorchoice ) { 69 colorid = colorchoice.getselectedindex() ; 70 71 72 public void mousepressed(mouseevent e) { 73 public void mousereleased(mouseevent e) { 74 public void mouseentered(mouseevent e) { 75 public void mouseexited(mouseevent e) { 76 public void mouseclicked(mouseevent e) { 77 lastx = e.getx() ; lasty = e.gety() ; 78 repaint() ; 79 80
6.1. 67 6.1.6 sample06.java 1 // 2 import java.applet.* ; 3 import java.awt.* ; 4 5 public class sample06 extends Applet { 6 7 public void paint(graphics g) { 8 int t ; // (deg.) 9 double hankei = 150 ; // 10 for( t=0 ; t<360 ; t+=10 ) { 11 double a ; 12 int x, y ; 13 a = (double)t*math.pi/180.0 ; 14 x = (int)( hankei * Math.cos(a) ) ; 15 y = (int)( hankei * Math.sin(a) ) ; 16 g.setcolor(color.blue) ; 17 g.filloval( x+200-10, -y+200-10, 21, 21 ) ; 18 19 20
68 6 6.2 6.2.1 sample07.java 1 // 2 import java.applet.* ; 3 import java.awt.* ; 4 5 public class sample07 extends Applet implements Runnable { 6 int t = 0 ; // (deg.) 7 double hankei = 150 ; // 8 Thread th = null ; 9 10 public void start() { 11 th = new Thread(this) ; 12 th.start() ; 13 14 15 public void run() { 16 while( th!= null ) { 17 repaint() ; 18 t = (t+5)%360 ; 19 try { 20 th.sleep(10) ; // 10ms = 0.01 21 22 catch (InterruptedException e) { 23 24 25 26 27 public void paint(graphics g) { 28 double a ; 29 int x, y ; 30 g.setcolor(color.white) ; 31 g.fillrect(10,10,380,380) ; 32 a = (double)t*math.pi/180.0 ; 33 x = (int)( hankei * Math.cos(a) ) ; 34 y = (int)( hankei * Math.sin(a) ) ; 35 g.setcolor(color.blue) ; 36 g.filloval( x+200-10, -y+200-10, 21, 21 ) ; 37 38
6.2. 69 6.2.2 sample08.java 1 // 2 import java.applet.* ; 3 import java.awt.* ; 4 import java.io.* ; 5 import java.net.* ; 6 7 public class sample08 extends Applet { 8 public void paint(graphics g) { 9 BufferedReader infile ; 10 String datatitle ; 11 String itemlabel ; 12 String s ; 13 int x, h, k ; 14 Color[] ctbl = { 15 Color.blue,Color.red,Color.green,Color.magenta,Color.pink 16 ; 17 try { 18 URL url = new URL(getDocumentBase(), "hist.txt") ; 19 InputStream fis = url.openstream() ; 20 // 21 // InputStream fis = new FileInputStream("hist.txt") ; 22 infile = new BufferedReader(new InputStreamReader(fis)) ; 23 datatitle = infile.readline() ; // 1 24 g.drawstring(datatitle,0,15) ; 25 h = 0 ; 26 while( (s = infile.readline())!= null ) { 27 s.trim() ; // 28 k = s.indexof(" ") ; 29 if( k >= 0 ) { 30 h++ ; 31 g.setcolor( ctbl[h%5] ) ; 32 itemlabel = s.substring(0,k) ; 33 g.drawstring(itemlabel,140-k*12,15+h*20) ; 34 x = Integer.parseInt(s.substring(k+1)) ; 35 g.fillrect(150,5+h*20,x*5,10) ; 36 37 38 infile.close() ; 39 40 catch( IOException e ) { 41 42 43
71 7 7.1 7.1.1 Java Java Sun Microsystems Web Java 7.1.2 paint init public class extends Applet { ( ) {... void paint(graphics g) {... ( ) ;... 7.1
72 7 void drawseitakakukei(graphics g,int n,int cx,int cy,int r) g: Graphics n: cx, cy: r: n = 3 r (cx,cy) 7.1:
III Java
75 8 8.1 Java 8.1.1 extends inheritance 8.1.2 = new ( ) ; ; = new ( ) ; constructor new 8.1.3 Fortran Pascal, PL/I C ( ) { ; return ; // void
76 8 3 a 10 a 3 reference by value reference by address public private 8.2 8.2.1 1 2 8.2.2 abstract boolean break byte case cast catch char class const continue default do double else extends final finally float for future generic goto if implements import inner instanceof int interface long native new null operator outer package private protected public rest return short static super switch synchronized this throw throws transient try var void volatile while 8.2.3 Java 8.1 1 a z A Z $ 2 0 9
8.3. 77 8.1: Java 1 boolean true false true 2 char Unicode (ASCII ) a 1 byte -127 127 (byte)12 2 short -32767 32767 (short)7456 4 int 18358 8 long 19 75379638L 4 float 6 4.5F 8 double 15 1.236 8.3 8.3.1 8.2 8.2: 1+2 3 5-1 4 2*3 6 5/3 1 5%3 2-2 -2 8.3.2 true false 8.3 8.3.3 NOT AND OR 8.4 true false
78 8 8.3: == 5==3 false!= 5!=3 true < 5<3 false > 5>3 true <= 5<=3 false >= 5>=3 true 8.4:!! 1==2 true && 5<3 && 1<2 false 1<2 2<3 true 8.3.4 () 1+2 *3 4+6 /7 /8 C 8.4 8.4.1
8.4. 79 float takasa ; short a, b, c ; ; 8.4.2 8.5 8.5: Java = += -= *= /= %= C ++ -- 8.4.3 (int), (float) byte < short < int < long < float < double int k ; k = (int)(5/2.0) // (5/2.0) 2.5 // 2 k (int)
80 8 8.4.4 Java String 8.6 8.6: int i = Integer.parseInt( ); long l = Long.parseLong( ); float f = Float.valueOf( ).floatvalue(); double d = Double.valueOf( ).doublevalue(); tostring() 8.7: int s = Integer.toString( i ); long s = Long.toString( l ); float s = Float.toString( f ); double s = Double.toString( d ); 8.5 8.5.1 ; 8.5.2 {
8.5. 81 8.5.3 if if ( ) { ; if ( x < 0 ) { x = -x ; if ( ) { 1; else { 2; if ( x < 0 ) { y = -x ; else { y = x ; 8.1 8.5.4 switch case switch ( ) { case 1 : 1 break; case 2 : 2 break; case n : n break; default: break 8.2
82 8 if ( ) if ( ) False True False True ( ) 1( ) 2( ) 8.1: if 8.5.5 for for ( 1 ; 2 ; 3 ) { ; for ( ) ; 1 2 3 1 10 int x ; for ( x=1 ; x<=10 ; x+=1 ) { System.out.println("X="+x) ;
8.5. 83 == 1 True 1( ) break False break == 2 True 2( ) break False break (default: ) ( ) 8.2: switch case 8.5.6 while DO WHILE while ( ) { ; for while 8.4 while( th!= null ) { repaint() ; t += 5 ; 8.5.7 do while REPEAT UNTIL
84 8 False True ( ) 8.3: for() do { ; while ( ) ; for do while 8.5 do { while( x>0 ) ; 8.5.8 break continue break for while do while 3 3 break
8.5. 85 False True ( ) 8.4: while continue break soto: // for( i=0 ; i<10 ; i++ ) { naka: // for( j=0 ; j<10 ; j++ ) { if( ) { break soto ; // i if( ) { break ; // j break naka ; 8.5.9 int a[] ; // int[] a; ( )
86 8 ( ) True False 8.5: do while double b[], c[], d[][]; int n = 90 d = new double [n][n]; // d = new double[90][90]; double b[] = new double[50]; // a[0] a[49] 50 float c[] = {10.0f, 15.0f, 10.0f; // C int x[5]; 2 int a[][] = { { 1, 2, { 3, 4 ; a[0][0] = 1, a[0][1] = 2, 0 a a.length 8.5.10 sin, cos Math 8.8 π e Math.PI Math.E
8.5. 87 8.8: Math a Math.abs(a) int,long,float,double a sin Math.sin(a) double double cos Math.cos(a) double double tan Math.tan(a) double double sin 1 Math.asin(a) double double cos 1 Math.acos(a) double double tan 1 Math.atan(a) double double Math.sqrt(a) double double a b Math.pow(a,b) double double Math.exp(a) double double Math.log(a) double double Math.max(a,b) int,long,float,double a,b Math.min(a,b) int,long,float,double a,b Math.floor(a) double double Math.rint(a) double double Math.ceil(a) double double Math.round(a) double,float long,int Math.random() double
89 9 9.1 import java.applet.* ; import java.awt.* ; public class extends Applet{ public void init() { public void paint( Graphics g ) { 9.2 Graphics Graphics Graphics. ( ) ; 9.2.1 drawline(int x1, int y1, int x2, int y2) (x1, y1) (x2, y2) drawpolyline(int[] xpoints, int[] ypoints, int npoints) x y
90 9 drawrect(int x, int y, int width, int height) fillrect(int x, int y, int width, int height) draw3drect(int x, int y, int width, int height, boolean raised) 3D fill3drect(int x, int y, int width, int height, boolean raised) 3 drawroundrect(int x, int y, int width, int height, int arcwidth, int archeight) fillroundrect(int x, int y, int width, int height, int arcwidth, int archeight) drawoval(int x, int y, int width, int height) filloval(int x, int y, int width, int height) drawarc(int x, int y, int width, int height, int startangle, int arcangle) fillarc(int x, int y, int width, int height, int startangle, int arcangle) drawpolygon(int[] xpoints, int[] ypoints, int npoints) x y fillpolygon(int[] xpoints, int[] ypoints, int npoints) x y
9.2. 91 drawstring(string str, int x, int y) copyarea(int x, int y, int width, int height, int dx, int dy) dx dy Sun Microsystems http://java.sun.com/j2se/1.5.0/ja/docs/ja/api/index.html Graphics 9.2.2 Java 1 Color Color RGB Color Color c ; c = new Color(255,0,0) ; // (R=255,G=0,B=0) Color c = new Color(255,0,0) ; // (R=255,G=0,B=0) new Color( ) 0 255 ( 8 ) setcolor c g.setcolor( c ) ; g.filloval(10,10,100,100) ; // Color 9.1 1 Java
92 9 9.1: Color Color.black Color.blue Color.cyan Color.darkGray Color.gray Color.green Color.lightGray Color.magenta Color.orange Color.pink Color.red Color.white Color.yellow
93 10 GUI 10.1 GUI TextField TextField tf1 ; // tf1 tf1 = new TextField(5) ; // 5 add(tf1) ; // object.add(tf1) init() String s ; s = tf1.gettext() ; int i ; i = Integer.parseInt(tf1.getText()) ; float f ; f = Float.valueOf(tf1.getText()).floatValue ; tf1.settext(s) ; tf1.settext(integer.tostring(i)) ; tf1.settext(float.tostring(f)) ; tf1.settext(" "+himando+" ") ; tostring()
94 10 GUI tf1.seteditable(flase) ; tf1.seteditable(true) ; TextArea TextArea ta1 ; // ta1 ta1 = new TextArea(5,20) ; // 5 20 add(ta1) ; // object.add(ta1) init() ta1.append(s) ; ta1.append(integer.tostring(i)) ; ta1.append(float.tostring(f)) ; ta1.append(" "+himando+" \n") ; tostring() \n C Label add(new Label(" ")) ; //
10.1. GUI 95 Button Button bt1 ; // bt1 bt1 = new Button("Start") ; // Start add(bt1) ; // object.add(bt1) ( ) bt1.addactionlistener(this) ; init() import java.awt.event.*; class implements ActionListener implements class public class myapplet extends Applet implements ActionListener {... public class myapplet extends Applet implements Runnable, ActionListener {... actionperformed public class myapplet extends Applet implements ActionListener {... public void actionperformed(actionevent ev) { if( ev.getsource() == bt1 ) { //......... Choice
96 10 GUI Choice ch1 ; // ch1 ch1 = new Choice() ; // ch1.additem("list 1") ; // ch1.additem("list 2") ; // add() add(ch1) ; // object.add(ch1) ( ) ch1.additemlistener(this) ; init() 0 ch1.select(1) ; // 2 0 int idx ; idx = ch1.getselectedindex() ; String selecteditem ; selecteditem = ch1.getselecteditem() ; class implements ItemListener implements class public class myapplet extends Applet implements ItemListener {... public class myapplet extends Applet implements Runnable, ItemListener {... itemstatechanged public class myapplet extends Applet implements ItemListener {... public void itemstatechanged(itemevent ev) { if( ev.getsource() == ch1 ) { //.........
10.1. GUI 97 Checkbox Checkbox cb1 ; // cb1 cb1 = new Checkbox("I am a Japanese.") ; // add(cb1) ; // object.add(cb1) ( ) cb1.additemlistener(this) ; cb1.setstate(true) ; // cb1.setstate(false) ; // if( cb1.getstate() ) {...... else {...... class implements ItemListener implements class public class myapplet extends Applet implements ItemListener {... public class myapplet extends Applet implements Runnable, ItemListener {... itemstatechanged public class myapplet extends Applet implements ItemListener {... public void itemstatechanged(itemevent ev) { if( ev.getsource() == cb1 ) { //.........
98 10 GUI Scrollbar Scrollbar sc1 ; // sc1 // 0, 100 (=110-10) 10 50 // Scrollbar.VERTIAL sc1 = new Scrollbar(Scrollbar.HORIZONTAL, 50, 10, 0, 110) ; add(sc1) ; // object.add(sc1) ( ) sc1.addadjustmentlistener(this) ; sc1.setvalue(75) ; // 75 int v ; v = sc1.getvalue() ; class implements AdjustmentListener implements class public class myapplet extends Applet implements AdjustmentListener {... public class myapplet extends Applet implements Runnable, AdjustmentListener {... adjustmentvaluechanged public class myapplet extends Applet implements AdjustmentListener {... public void adjustmentvaluechanged(adjustmentevent ev) { if( ev.getsource() == sc1 ) { //.........
10.2. 99 10.2 1 FlowLayout BorderLayout GridLayout 1 FlowLayout setlayout 10.2.1 BorderLayout setlayout BorderLayout add setlayout(new BorderLayout()) ; // add( GUI, BorderLayout.NORTH) ; // add( GUI, BorderLayout.SOUTH) ; // add( GUI, BorderLayout.WEST) ; // add( GUI, BorderLayout.EAST) ; // add( GUI, BorderLayout.CENTER) ; // 10.2.2 GridLayout setlayout GridLayout add setlayout(new GridLayout(3,2)) ; // 3 2 add( GUI ) ; // add( GUI ) ; // add( GUI ) ; // add( GUI ) ; // add( GUI ) ; // add( GUI ) ; // 1 GridBagLayout CardLayout
101 A Java Java 600 Java A.1 Java Java [8] [1] [9] Java [6] SDK Java [11] Java SDK CD-ROM [12] Java 2 CG 3 CG Web http://www.human.nagoya-u.ac.jp/~mine/simu.html Sun Microsystems Java [5] [10] JDK 2004
102 A
A.2. 103 A.2 [3] UNIX C Software Tools UNIX C Ratfor Fortran Fortran90 [4] Fortran PL/I Kernighan [2] [3] [4] Java C++ Java [7] [13] IDE(Integrated Development Environment) Eclipse Eclipse http://www.eclipse.org/
105 [1] Jerry R. Jackson, Alan L. McClellan. Java., 1997. [2] Brian W. Kernighan, Rob Pike. : The Practice of Programming., 2000( 1999). [3] Brian W. Kernighan, P. J. Plauger. ( ): SoftwareTools., 1981( 1976). [4] Brian W. Kernighan, P. J. Plauger. : The Elements of Programming Style., 1982( 1978). [5] Sun Microsystems. Java 2 Platform, Standard Edition (J2SE). Web (http://java.sun.com/j2se/1.5.0/ja/docs/ja/), 2004. [6]. Java., 2002. [7] Bjarne Stroustrup. C++ :The C++ Programming Language., 1993( 1991). [8]. Java., 1996. [9]. Java., 1998. [10]. Java, Java, 1., 2001. [11]. Java., 2003. [12]. Java., 2003. [13]. Eclipse 3., 2004.
106 <applet> </applet>, 58 AWT, 33 ActionEvent, 35 ActionListener, 35 AdjustmentEvent, 35 AdjustmentListener, 35 BorderLayout, 99 BufferedReader, 47 Button, 35, 95 C++, 103 Checkbox, 35, 97 Choice, 35, 95 96 Color, 38, 91, 91 Eclipse, 103 E, 86 Fortran, 103 Frame, 52 GridLayout, 99 Image, 48 InputStream, 47 ItemEvent, 35 ItemListener, 35 LOGO, 18 Label, 94 MouseListner, 37 MouseMotionListener, 37 PI, 86 PL/I, 103 RatFor, 103 Scrollbar, 35, 98 Smalltalk, 18 Swing, 33 TextArea, 35, 94 TextField, 35, 93 94 Thread, 43 acos, 86 actionperformed, 35 addactionlistener, 35 addadjustmentlistener, 35 additemlistener, 35 addmouselistener, 37 addmousemotionlistener, 37 add, 34 adjustmentvaluechanged, 35 appletviewer, 23 asin, 86 atan, 86 ceil, 86 copyarea, 91 cos, 86 do while, 31, 84 draw3drect, 90 drawarc, 90 drawimage, 48 drawline, 89 drawoval, 90 drawpolyline, 89 drawpolygon, 90 drawrect, 90 drawroundrect, 90 drawstring, 91 exp, 86 fill3drect, 90
107 fillarc, 90 filloval, 90 fillpolygon, 90 fillrect, 38, 90 fillroundrect, 90 floor, 86 for, 31, 82 getimage, 48 getsize, 38 if else, 31, 81 if, 81 init, 34 itemstatechanged, 35 javac, 23 java, 23 length, 86 log, 86 main, 51 max, 86 min, 86 mouseclicked, 37 mousedragged, 37 mouseenetered, 37 mouseexited, 37 mousemoved, 37 mousepressed, 37 mousereleased, 37 new, 34, 75 paint, 34, 38, 44 pow, 86 random, 86 repaint, 38 rint, 86 round, 86 sample01.html, 24, 28, 58 sample01.java, 24, 28, 57 sample02.java, 31, 59 sample03.java, 33, 35, 60 sample04.java, 37, 64 sample05.java, 39, 65 sample06.java, 67 sample07.java, 68 sample08.java, 69 setcolor, 38 setlabel, 46 setlayout, 99 sin, 86 sleep, 43 sqrt, 86 switch case, 81 tan, 86 try catch, 48 update, 44 while, 31, 83 API, 16 GUI, 18, 33, 93 GUI, 35 IDE, 103 JDK, 23 UNIX, 103 Virtual Machine, 25 VM, 25, 41, 23, 51, 23, 89, 23, 17 GUI, 35, 17, 37
108, 75, 34, 44, 76, 76, 18 GUI, 33 GUI, 33, 16 18, 75, 25, 76 boolean, 76 byte, 76 char, 76 double, 76 float, 76 int, 76 long, 76 short, 76, 77, 16, 53, 75, 75, 16, 75, 16, 16, 77, 27 28, 15, 31, 15, 30, 79, 44, 20, 33, 17, 31, 23, 23, 29, 47 48, 52, 23 24, 78 79, 30, 30, 85 86, 35 37, 28, 75 76, 89 91, 80, 76, 33, 33 34, 48, 77