260 likes | 859 Views
Applets and HTML MSTU 4031: Programming I Applets Applets are programs that run inside a web browser. The code for an applet is stored on a web server and downloaded into the browser whenever you access a web page with an applet. Applets are programmed as a class. Sample Applets
E N D
Applets and HTML MSTU 4031: Programming I
Applets • Applets are programs that run inside a web browser. • The code for an applet is stored on a web server and downloaded into the browser whenever you access a web page with an applet. • Applets are programmed as a class.
Sample Applets • Heart Simulator • http://www.columbia.edu/ccnmtl/projects/heart/sim.html • Rice Virtual Statistics Lab • http://www.ruf.rice.edu/~lane/stat_sim/descriptive/index.html • Final Projects (Fall 2003) • http://www.columbia.edu/~lwk2103/mstu4031/Seasons/WinterOuter.html • http://www.columbia.edu/~yw2009/HealthEducation/ • http://www.columbia.edu/~jh2284/mstu4031/finalproject/curve.html
The Process of Creating and Displaying an Applet Creates Run & Display .java Compiler .class .html Browser Embed Compile
Applets & Security • Applets run in a sandbox, where it can display information and get user input, but can’t read or touch anything on the user’s computer. • You can give an applet more privileges (reading and writing local files), if you notify your browser that the applet is trusted. • Applets can carry a certificate or signatures from an authentication firm (VeriSign) to tell you where the applet came from.
Console Runs from command line Text based Terminal window public static void main(String [] args) Applets Runs from browser Web based Graphical No main() method Reserved methods for Applets. init() like a no parameter constructor paint() method is called whenever the surface of the applet needs to be filled in. Difference between Applets and Console Applications
java.applet Package import java.applet.Applet; public void init(){} public void paint(Graphics g){}
java.awt Package • java.awt package (abstract windowing toolkit) • Defines many classes for graphics programming, mechanism for event handling, and a set of user interface components. • java.awt.Graphics • java.awt.Graphics2D
Console (java.io.PrintStream) method println() Applets (java.awt.Graphics2D) method drawString() Writing Text to the Screen
import java.applet.Applet; import java.awt.Graphics; public class MyApplet extends Applet { } import java.awt.Graphics2D; public void paint (Graphicsg) { } Graphics2D g2 = (Graphics2D) g ; g2.drawString ("Floor plan of a diner", 145, 20);
Graphics2D Class • Use the drawString () method to draw a string, starting at its basepoint. • Use draw() method of the Graphics2D class to draw shapes such as rectangles, ellipses, line segments, polygons, and arcs.
Applet Display Properties • Applet canvas is measured width and height (defined in <applet> tag in HTML) <applet code = “MyClass.class” width=“300” height=“300”> </applet> • Screen coordinates (x, y) and canvas size are measured in pixels. • A pixel (picture element) is a dot on the screen. • (10, 20) or (x, y ) is 10 pixels to the right and 20 pixels down from the top left corner of the panel.
y 0 50 100 250 300 350 400 450 500 550 600 Origin 0 50 100 150 200 250 300 350 400 450 500 0,0 x, y (100, 350) x, y, w, h (50, 400, 350, 100) x 0 50 100 250 300 350 400 450 500 550 600 My Floor plan Basepoint
Graphical Shapes • Rectangles • Ellipses • Lines • Arcs Example Floor Plan
import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; public class MyApplet extends Applet { } import java.awt.Rectangle; public void paint (Graphicsg) { } Graphics2D g2 = (Graphics2D) g ; Rectangleouterborder = new Rectangle (5, 30, 400, 300); g2.draw(outerborder);
import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; public class MyApplet extends Applet { } import java.awt.geom.Ellipse2D; public void paint (Graphicsg) { } Graphics2D g2 = (Graphics2D) g ; Ellipse2D.Doubleseat1 = new Ellipse2D.Double (50, 125, 20, 20); g2.draw(seat1); g2.drawString("S1",55,138);
import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; public class MyApplet extends Applet { } import java.geom.Line2D; public void paint (Graphicsg) { } Graphics2D g2 = (Graphics2D) g ; Line2D.Doublemyline = new Line2D.Double(5,50,400,50); g2.draw(myline);
import java.applet.Applet; import java.awt.Graphics; import java.awt.Graphics2D; public class MyApplet extends Applet { } public void paint (Graphicsg) { } Graphics2D g2 = (Graphics2D) g ; // x, y, w, h, start angle, arc angle g2.drawArc(300, 100, 50, 50, 90, 90);
About Web Pages • Web pages are written in Hypertext Markup Language (HTML) • HTML files are made up of tags to format text (and include images, applets, audio and video files) that tell the browser how to render the text.
HTML • Most HTML tags come in pairs with an opening and closing tag. • Each pair applies to the text between the two tags. • The closing tag is just like the opening tag, but it is prefixed by a slash (/).
Sample HTML Page <html> <title>Title Here</title> <h3>Text Here</h3> <body> </body> </html>
Running an Applet • To run an applet you need an HTML page with the applet tag. <applet code = “ .class” width=“” height=“”> </applet> • The applet tag include the applet in a web page • Tell the browser where to find the applet code
Sample HTML Page With <applet> <html> <title>My First Applet! </title> <applet code = “My.class” width=“400” height=“400”> </applet> <h3>Look at my applet!</h3> <body> </body> </html>
Browser Compatibility • For applets to work on the latest versions of IE and Netscape you can not use the <applet></applet> tags. • Instead, insert this code: http://java.sun.com/products/plugin/1.3/docs/tags.html#Any
Using Parameters • HTML • <PARAM NAME=“myText” VALUE”Hello!”> • JAVA • init() • This method is called when the applet is first loaded into the browser. It is used to perform applet initialization, in preference to a constructor method. You can either use a constructor or the init(). Create all GUI components in the either the constructor or init. • If you use a constructor, it must be a no-argument constructor. An applet gets its arguments from <PARAM> tags in the HTML file. • getParameterInfo() • this method is called to obtain information about the parameters to which the applet responds. Returns a String [][] that describes the parameters. • getParameter() • This method looks up and returns the value of the named parameter, specified with a <PARAM> tag in the HTML file that contains the applet.
Sample Floor Plans • Floor Plans • http://www.columbia.edu/~rh506/mstu4031/hw7/FloorPlan.html • http://www.columbia.edu/~sc472/mstu4031/hw7/FloorPlan.html • http://www.columbia.edu/~mk2379/mstu4031/hw07/FloorPlan.html • http://www.columbia.edu/~lwk2103/mstu4031/hw7/FloorPlan.html