180 likes | 340 Views
Package – Class (application programming interface). import javax.swing.JOptionPane;. method. argument. JOptionPane.showInputDialog("Cash: ");. String. javax.lang This package will be imported by default. Class method argument. Double.parseDouble(str);. double.
E N D
Package – Class (application programming interface) import javax.swing.JOptionPane; method argument JOptionPane.showInputDialog("Cash: "); String javax.lang This package will be imported by default Class method argument Double.parseDouble(str); double Cash = Double.parseDouble( JOptionPane.showInputDialog("Cash: ") ); http://java.sun.com/j2se/1.4.2/docs/api/index.html
String class, equals method String Answer;// Answer is an object of class String; boolean IsHappy; Answer = JOptionPane.showInputDialog( "Are you happy? (Y for yes, others for N) " ); // The following condition doesn’t work // if (Answer == ("Y" || "y" ) // // if (Answer == "Y" || Answer == "y" ) // IsHappy = true; if (Answer.equals("Y“) || Answer.equals("y")) IsHappy = true; else IsHappy = false;
Cosmetics & HTML Cosmetics : Tools or material to make up faces = Face + (????) HTML : HyperText Markup Language Tools or material to make up hypertext document HTML Document = Hypertext + HTML Tags
Text and HyperText Mathematicians and Philosophers by Chung-Chih Li Rene Descartes: "I am thinking, therefore I exist." Descartes was the most important figure in modern mathematics and philosophy, and this probably is the most famous say of Descartes; but what does it really mean? The existence is established necessarily via mankind's awareness, which seemed self-evidence to Descartes, but some obvious objections followed immediately. You may want to see the excellent debate in Meditations on First Philosophy edited by John Cottingham --, which is a collection of Descartes' Meditations, their objections, and Descartes' replies. "The brain is a machine." <html> <body> <h1> Mathematicians and Philosophers </h1> <div class="author">by Chung-Chih Li </div> <h3> Rene Descartes: </h3> <h4> "I am thinking, therefore I exist.“ </h4> Descartes was the most important figure in ....... <h4> "The brain is a machine. " </h4> </body> </html> http://hal.lamar.edu/~licc/math_phi_s2.html
Static HTML file1, file2, file3, file4, file5, file6, file7, file8, file9, file10, file11, file12, file13, file14, …………. ……………….. ……………….. ……………….. ……………….. ……………….. ……………….. I E file6, file9, file100, ……. File server click click click click click need file20 need file34 click click Internet Client Server
Dynamic HTML file1, file2, file3, .. quiz.html ……. Progrma1…. cgi-bin/generic-grader.pl ……………….. data1, data2, date3, 1022 ……………….. IE <form name=“x” method=“post” action="http://cs.colgate.edu/cgi-bin/generic-grader.pl" ….. </form> I.E 100 out of 100 are correct.. generic-grader.pl result.html answers.txt file server ? quiz.html answers.txt Form x ……. ? answers.txt Internet Client Server
HTML Web browser (IE, Netscape) Running a Java Application in your Web browser --- which means, an HTML document can run a program -- Big deal?? YES, Big deal. • What kinds of machines? (100 kinds!!) • Java uses Virture Machine • (bytecodes) HelloWorld.class • Security and safety. • Java provides strong security environment
Pigglet:Pigglet is a little pig who is Pooh’s best friend. Applet:An applet is a little application who is Web’s best friend. Running a Java Application in your Web browser --- which means, an HTML document can have a running program Tic-Tac-Toe Appletexample Many more on C:/Program Files/j2sdk_nb/j2sdk1.4.2/demo/applets
An html document with Java applet <html> <head> <title> TicTacToe v1.1 </title> </head> <body> <h1> TicTacToe v1.1 </h1> <hr> <applet code=TicTacToe.class width=120 height=120> </applet> <a href="TicTacToe.java">The source.</a> </body></html>
Your next assignment <HTML> <body> <h>COSC 1373-01/Java, Sprint 2004</h> This is an applet. <APPLET code="Asg3.class" width=350 height=200> </APPLET> <a href="Asg3.java">The source.</a> <h3> by Your name here </h3> </body> </HTML> Asg3.html
Netscape 7 • I.E. withJava Plug-in Java 2 supported Web browsers 1. 2. 3. install Java 2 Running time environment. (J2RE) j2re-1_4_0-win.exe or go to http://www.java.com/en/download/windows_automatic.jsp for automated Java Plug-in installation under window command mode: appletviewer Asg3.html
A class is a concept of something Vehicle color, wheels, seat, engine……..… .(Attributes) can run, turn, stop, carry things ……(Methods) Classes: Truck big wheels, diesel engine, cargo deck...(Attributes) …lift cargo deck…………………………(Methods) Truck extends the concept of Vehicle Applet (some people knows what an Applet should be) Asg3 (We want them all but modify or add a little.) Asg3 extends the concept ofApplet
Vehicle SUV instantiation Class Objects Honda Pilot not these Drive this instantiation I J 000-AAA 011-JAV Object Integer I,J; I = new Integer(3); J = new Integer(9); int i,j; i = Integer.parseInt(“1”); j = I.parseInt(“1”); Number Integer Double
An applet import java.awt.Graphics; // import class Graphics in // java.awt package // (Abstract Windows Toolkit) import javax.swing.JApplet; // import class JApplet public class TestApplet extends JApplet { public void paint( Graphics g ) { super.paint( g ); g.drawString( "Welcome to Java Programming!", 40, 40 ); } } // end class TestApplet
Typical applet components import java.awt.Graphics; // import class Graphics in // java.awt package // (Abstract Windows Toolkit) import javax.swing.JApplet; // import class JApplet public class App extends JApplet { public void init() { } // This will be executed 1st public void start() { } // then this, 2nd public void paint( Graphics g ) { } // then this. 3rd public void stop() { } public void destroy() { } } // end class App
Some Terminologies Drawing: putting anything on the screen of the applet. images, text (String), buttons, graphics (line, dots, circle..) primitives. Event handling:detecting and processing anything caused by the user (or other programs) Applets inherit the methods in some classes of java.awt package to handle drawing and event handling.
What Applets Can't Do (for security reasons) • Cannot read or write files. • Cannot make network connections except to where it came from. • Cannot start other program. • Cannot detect certain system properties.
Graphics class import java.awt.Graphics; // import class Graphics import java.awt.Color; // import class Color Some useful methods of Graphics and a field of Color. g.drawString("I can draw!!", 50, 150); g.drawRect(30,30, 200, 100); g.setColor(Color.blue); g.drawArc(100, 20, 100, 100, 0, 120); g.drawArc(0, 0, 100, 100, 0, 275); g.setColor(Color.green); g.fillRect(60,60, 10,20); g.setColor(Color.red); g.drawOval(150,100, 200, 100); g.setColor(Color.yellow); g.fillOval(200, 130, 90, 50); Test Applet