1 / 29

CSC 205 – Java Programming II

CSC 205 – Java Programming II. Applet. Types of Java Programs. Applets Applications Console applications Graphics applications Applications are stand-alone programs An application must have a main method. What Is Applet. An applet is a program that adheres to a set of conventions

Download Presentation

CSC 205 – Java Programming II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 205 – Java Programming II Applet

  2. Types of Java Programs • Applets • Applications • Console applications • Graphics applications • Applications are stand-alone programs • An application must have a main method

  3. What Is Applet • An applet is • a program that adheres to a set of conventions • run within a Java-compatible Web browser • downloaded from a Web server • An applet must extend the Applet class

  4. Download From Web Server

  5. Advantages & Disadvantages • Advantages • Can be accessed where Internet is available • When GUIs need to be upgraded, just change them at one location: on the Web server • Disadvantages • Time to download may be long • Security could be an issue

  6. Security Policies • Browsers impose the following restrictions on any applet that is loaded over the network: • An applet cannot load libraries or define native methods. • It cannot ordinarily read or write files, or start any program on the host that's executing it. • It cannot makenetwork connections except to the host that it came from. • It cannot read certain system properties. • Windows that an applet brings up look different than windows that an application brings up.

  7. Inheritance Hierarchy • java.appletClass Applet • java.lang.Object • | • +--java.awt.Component • | • +--java.awt.Container • | • +--java.awt.Panel • | • +--java.applet.Applet

  8. A Typical Applet import java.applet.Applet; import java.awt.Graphics; public class Simple extends Applet { //methods to be overridden public void init() {…} public void start() {…} public void stop() {…} public void destroy() {…} void addItem(String newWord) { …; repaint(); } public void paint(Graphics g) {…} //inherited from the Container class }

  9. Life Cycle of an Applet • Loading the Applet • An instance of the applet's controlling class (an Applet subclass) is created • The applet initializes itself • The applet starts running • Leaving and Returning to the Applet's Page • The applet stops running when leaving the page • the applet can start itself again when returning

  10. Methods for Milestones • An applet can override the following methods • init To initialize the applet each time it's loaded (or reloaded). • start To start the applet's execution, such as when the applet's loaded or when the user revisits a page that contains the applet. • stop To stop the applet's execution, such as when the user leaves the applet's page or quits the browser. • destroy To perform a final cleanup in preparation for unloading

  11. Methods for Drawing • An applet can override the following two display methods (of the Container class) : • paint The basic display method. Many applets implement the paint method to draw the applet's representation within a browser page. • update A method you can use along with paint to improve drawing performance

  12. Animation • To update applet changes • invoke the repaint method (inherited from the Component class) periodically • The paint method will be invoked when the applet is repainted

  13. Event Handling • Applets inherit a group of event-handling methods from the Component class • To react to an event, an applet must override • either the appropriate event-specific method, or • the handleEvent method (from the Component class)

  14. Event Handling – Example • Adding the following code to the Simple applet makes it respond to mouse clicks. import java.awt.Event; . . . public boolean mouseDown(Event event, int x, int y) { addItem("click!... "); return true; }

  15. Deprecation • Unfortunately, many of the samples available online or in textbooks use deprecated methods • You will be warned when you compile code with deprecated methods • Use the –deprecation option to see detailed info javac –deprecation Simple.java • Replace deprecated methods with newer methods as recommended by latest version of Java API

  16. Deprecation

  17. Using UI Components • Because the Applet class inherits from the AWT Container class, it's easy to add components to applets and to use layout managers to control the components' onscreen positions. • add Adds the specified Component • remove Removes the specified Component • setLayout Sets the layout manager

  18. Testing Applets • Two ways to run an applet • Use the applet viewer appletviewer simple.html • Embed applets into Web pages • Both need to use the applet tag in HTML files <APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt> </APPLET>

  19. HTML – An Overview • Markup language • use tags to represent the meaning and/or content of the enclosed data • Some features • Not case sensitive • Loose syntax • Predefined tags • Text is the only data type

  20. More Examples • The following two tags are equivalent <APPLET CODE=Simple.class WIDTH=100 HEIGHT=100> </APPLET> <applet code=“Simple.class” width=“100” height=“100”> </applet>

  21. A Simple HTML File <html> <body> <p>Click on the applet to start the animation.</p> <!– this line is comment --> <applet code="SelectionSortApplet.class" width="300" height="300"> </applet> </body> </html>

  22. Thread • A thread is a program unit that is executed independently of other parts of the program • The JVM executes each thread for a short time and then switches to another thread • A programmer can concentrate on what tasks each thread need to perform • and possibly, the communication between threads

  23. Write a Thread • Follow the steps • Write a class that extends the Thread class • Place the code for the task in the run method • Create an object of the your thread class • Call the start method to activate your thread

  24. Sample Thread Class public class GreetingThread extends Thread{ public GreetingThread(String aGreeting){ greeting = aGreeting; } public void run() { try { for (int i = 1; i <= REPETITIONS; i++) { Date now = new Date(); System.out.println(now + " " + greeting); sleep(DELAY); } } catch (InterruptedException exception){ } }

  25. Running A Thread – I public class GreetingThreadTest{ public static void main(String[] args) { GreetingThread t1 = new GreetingThread("Hello, World!"); GreetingThread t2 = new GreetingThread("Goodbye, World!"); t1.start(); t2.start(); } }

  26. Sample Output

  27. The Runnable Interface • Problems of writing a thread which extends another class, e.g. the Frame class • Since Java doesn’t allow multiple inheritance • Can implement the Runnable interface instead public class MyFrame extends Frame implements Runnable { public MyFrame() {…} public void run() {…} }

  28. Running A Thread – II • Follow the steps • Construct a Runnable object • Create a thread using the Runnable object • Invoke the start method of the thread MyFrame frame = new MyFrame(); Thread myThread = new Thread(frame); myThread.start();

  29. start interrupt

More Related