Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
What are Applets? execute • Applets are "small" Java programs to be integrated in web pages. • Downloaded from a web server together with their web page. • Execution within a web browser, using the browser's VM. • Execution platform unknown at programming time. (c)schmiedecke 05 Pr1-25 Applets communicate download www web server 2 Why Applets? – Design facilities beyond HTML – Interaction beyond HTML forms – Program interaction between web client and web server – GUI for distributed applications execute download www communicate web server (c)schmiedecke 05 Pr1-25 Applets 3 Applet Programming • Extend java.applet.Applet (orr javax.swing.JApplet) • class Applet extends Panel { public void init(); public void start(); public void stop(); } • An applet is showing a panel within a browser window. • init() is called when the applet is loaded. Design your panel, using init() to add components and listeners - instead of the constructor. • start() is called when the Applet is started – use it instead of main(). • Use the paint() method for drawing (c)schmiedecke 05 Pr1-25 Applets 4 Applet Design public class Foto extends Applet implements ActionListener{ private JButton button = new JButton("show photo"); private JLabel img = new JLabel(); private int imageIndex = 0; ImageIcon [] foto = new ImageIcon[2]; public void init() { // c.f. init() in applications setLayout(new BorderLayout()); foto[0] = new ImageIcon("images/P8040106.png"); foto[1] = new ImageIcon("images/P8040112.png"); img.setIcon(foto[0]); add(button, BorderLayout.NORTH); add(img); button.addActionListener(this); } public void start() { // erase image at new start imageIndex = 0; img.setIcon(foto[imageIndex]); } public void actionPerformed(ActionEvent e) { imageIndex = (imageIndex +1) % foto.length; img.setIcon(foto[imageIndex]); } } (c)schmiedecke 05 Pr1-25 Applets 5 Example: Clock import import import import java.applet.*; java.awt.*; java.util.*; java.text.*; public class Clock1 extends Applet implements Runnable { int width, height; Thread t = null; int hours=0, minutes=0, seconds=0; String timeString = ""; public void init() // from params, c.f. later slides { width = getSize().width; height = getSize().height; setBackground( Color.lightGrey ); } from: http://www.dgp.toronto.edu/~mjmcguff/learn/java/09-clocks/ (c)schmiedecke 05 Pr1-25 Applets 6 public void start() { if ( t == null ) { t = new Thread( this ); t.setPriority( Thread.MIN_PRIORITY ); t.start(); } public void run() { while (true) { // thread does some work Calendar cal = Calendar.getInstance(); hours = cal.get( Calendar.HOUR_OF_DAY ); if ( hours > 12 ) hours -= 12; minutes = cal.get( Calendar.MINUTE ); seconds = cal.get( Calendar.SECOND ); Date date = cal.getTime(); timeString = date.toString(); } (c)schmiedecke 05 Pr1-25 Applets 7 void drawHand( double angle, int radius, Graphics g ) { angle -= 0.5 * Math.PI; int x = (int)( radius*Math.cos(angle) ); int y = (int)( radius*Math.sin(angle) ); g.drawLine( width/2, height/2, width/2 + x, height/2 + y ); } void drawWedge( double angle, int radius, Graphics g ) { ... } public void paint( Graphics g ) { g.setColor( Color.blue ); drawWedge( 2*Math.PI * hours / 12, width/5, g ); drawWedge( 2*Math.PI * minutes / 60, width/3, g ); drawHand( 2*Math.PI * seconds / 60, width/2, g ); g.setColor( Color.black ); g.drawString( timeString, 10, height-10 ); } } // end of class (c)schmiedecke 05 Pr1-25 Applets 8 Applet Execution • Applets are integrated into HTML pages • The simplest Applet Tag is: • <Applet Code="Clock.class" Width=300 Height=300> </Applet> • Store this HTML file in the same directory as the compiled applet class and run it by double clicking. • Add a header to this HTML file and store it on the web server. Download and execute it via the www. (c)schmiedecke 05 Pr1-25 Applets 9 The complete Applet Tag <APPLET [CODEBASE = codebaseURL] CODE = appletFile [ALT = alternateText] [NAME = appletInstanceName] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment] [VSPACE = pixels] [HSPACE = pixels] > [< PARAM NAME = appletParameter1 VALUE = value >] [< PARAM NAME = appletParameter2 VALUE = value >] ...[alternateHTML] </APPLET> (c)schmiedecke 05 Pr1-25 Applets 10 Applet Positioning • Position attributes like in IMG tag: • Align: left, right, top, texttop, middle, absmiddle, baseline, bottom, absbottom. • VSpace, HSpace: Distance from neighbouring elements (pixels) (c)schmiedecke 05 Pr1-25 Applets 11 Parameter Parameter: • Free choice of paramNames • Applets can read parameters using getParameter(String paramName) • Parameters are HTML page configuration data (not user input) alternateHTML: • Any text – displayed if the browser does not support Java • i.e. has no java plugin. (c)schmiedecke 05 Pr1-25 Applets 12 Parameter Example <-- File AppletTest.html --> <Applet Code=" ConfiguredFoto.class" Width=200 Height=300> <Param Name="Language" Value="Englisch"> <Param Name="ImageSource" Value="images/foto3.png"> Your Browser is not Java-enabled.<br> You should install the Java plugin. <br> <Href "http://www.javaplugin.htm"> Install Instructions for Plugin</Href> </Applet> public class ConfiguredFoto extends Foto { public void init() { foto = new ImageIcon(getParameter("ImageSource")); if (getParameter("Language")!="English") button.setText("-->"); super.init(); } (c)schmiedecke 05 Pr1-25 Applets 13 } Downloading Applets from Anywhere • Use the Codebase attribute to hold the Download-URL • Any URL permitted. • Default: "DocumentBase" – i.e. download URL of the HTML page • <Applet Code="Bild.class" CodeBase="http://www.tfh-berlin.de/...../" Width=200 Height=300> </Applet> • read CodeBase and DocumentBase inside the applet: getCodeBase(), getDocumentBase() (c)schmiedecke 05 Pr1-25 Applets 14 Applets in Packages • Rules similar to javac: – applet name is a qulified class name – separator is dot "." (not slash!) – CodeBase denotes the root directory of the package structure • <Applet Code="fotos.Foto.class" Width=700 Height=600> </Applet> (c)schmiedecke 05 Pr1-25 Applets 15 Wecker.htm <html> <body> <applet code=wecker.WeckerApplet.class archive=cs101-lib.jar, Alarmimages.jar width="200" height="200" > </applet> </body> </html> (c)schmiedecke 05 Pr1-25 Applets 16 Applets in Jar Archives • Bundle applet and resources in a single jar • easy deployment and administration (e.g.moving) • saves download time. • Several jars possible – download the second while displaying content from the first. • <Applet Code="Bild.class" Archive=Applets1.jar, Applets2.jar ... </Applet> • Bild.class will be searched first in Applets1.jar, then in Applets2.jar, then in the codebase directory (c)schmiedecke 05 Pr1-25 Applets 17 Applet Security An applet plays in its "sandbox" An applet comes in handcuffs: It cannot read from the local file system It can only establish network connections to its codebase or document base. It cannot start or stop programs. It cannot execute "native" code. It can only read non-critical, java relevant system properties like version. • If executed locally from hard disk, using appletviewer.exe, it has less restrictions. So, if your applet works in the appletviewer, it might still not run in a browser! (c)schmiedecke 05 Pr1-25 Applets 18 Applet to Server Communication Network connections permitted to CodeBase or DocumentBase Never use absolute server URL. Always use relative URL: URL serverUrl = getCodeBase(); Socket netConnection = new Socket (serverUrl.getHost(),55555); OutputStream out = netConnection.getOutputStream(); (c)schmiedecke 05 Pr1-25 Applets 19 a2a Communication • An applet can directly access other applets which – run in the same browser window and – have the same CodeBase • Such applets can call each other: – use getAppletContext() to obtain the context object, which keeps track of all applets in a browser. – use getApplet("appname") to get the applet that was loaded with the name attribut "appname" – or use getApplets() to obtain an enumeration of all accessable applets (c)schmiedecke 05 Pr1-25 Applets 20 Example: • Applet Runner has two methods: • goLeft() makes a stick figure walk to the left edge and then, after a short pause, calls goRight • goRight() is the reverse. • Controlling a stick figure from another applet: Runner right = (Runner) (getAppletContext(). getApplet("rightRunner")); right.goLeft(); (c)schmiedecke 05 Pr1-25 Applets 21 Tutorials • Use google to find numerous tutorials! • http://www.realapplets.com/tutorial/index.html deals with applet basics • http://www.dgp.toronto.edu/~mjmcguff/learn/java tells you how to do all sorts of graphics and imaging in an applet (c)schmiedecke 05 Pr1-25 Applets 22