1 / 27

Mastering Java Applets: Design, Deploy, Interact

Discover Java applets, learn to create interactive web programs, differentiate applets from applications, and explore event handling, graphics, fonts, and colors. Dive into applet life cycles, parameter passing, and GUI design. Improve your skills in applet development.

jlim
Download Presentation

Mastering Java Applets: Design, Deploy, Interact

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. Applets Session 4

  2. Objectives • Define an applet • Differentiate between Java applications and Java applets • Explain how to create a program to work as both applet and an application • Identify how parameters are passed to applets • Discuss Event handling with applets • Describe the graphics class • Discuss the Font class • Examine the FontMetrics class • Discuss the Color class Applets / 2 of 27

  3. Introduction • There are a number of ways to create interactive programs on the Web but one of the most remarkable ways is using a Java applet • An applet is a Java program designed to work on the Internet through a web browser • An applet can be used for a variety of purposes right from web-based communications to designing graphical user interfaces for backend applications • HTML pages by themselves are very passive Applets / 3 of 27

  4. Applets • Created by subclassing the ‘java.applet.Applet’ class • Examples of Java enabled web browsers are Internet Explorer • An ‘Applet’ is a Java program that can be embedded in an HTML page and executed on a Java enabled browser and Netscape Communicator Applets / 4 of 27

  5. Difference between Applets and Applications (1) • Applets are created by extending the java.applet.Applet class. There is no such constraint for an application • Applets run on any browserwhileapplications run using Java interpreter • An applet is basically designed for deploying on the web whereas anapplication is designed to work as a standalone program Applets / 5 of 27

  6. Difference between Applets and Applications (2) • Applet must contain at least one public class failing which the compiler reports an error. There it is not mandatory to declare main( ) for an appletwhere as In case of application, ‘main( )’ has to be included in a public class • Execution of applets begin with the init() methodwhileExecution of applications begin with main() method Applets / 6 of 27

  7. Life cycle of an Applet (1) • An applet defines its structure from four events that take place during execution • For each event, a method is automatically called • Life cycle of an object specifies stages the object has to pass right from its creation until it is destroyed Applets / 7 of 27

  8. Life cycle of an Applet (2) • The methods are as follows: • init() – called during initialization • start() – starts the applet once it is initialized • stop() – used to pause the execution of an applet • destroy() – used to destroy the applet • The method paint() is used to display a line, text or an image on the screen • Whenever an applet has to be painted again after it has been drawn once, the repaint() method is used. Applets / 8 of 27

  9. Life cycle of an Applet (2) Redraw Applet Destroy Applet Start state init( ) Applet Born paint( ) start( ) stop( ) destroy( ) Applet Working Initialization state Applet Displayed If start( ) called again Idle State Applet Destroyed Applets / 9 of 27

  10. A simple applet Output Applets / 10 of 27

  11. Compiling and running an applet • An applet is compiled using the Java compiler: javac • javac Firstapplet.java • Create a HTML page to display the applet <html> <appletcode=Firstapplet width=200 height=200> </applet> </html> • Then type the following at command prompt: • appletviewer abc.html //‘abc.html’- name of the html file Applets / 11 of 27

  12. Displaying images using applets (1) Output Applets / 12 of 27

  13. Displaying images using applets (2) • getCodeBase() method gets the base URL • getImage() method returns an Image object which can be drawn on the screen • drawImage() takes four parameters – Image object, location in terms of x and y coordinates and an object of type ImageObserver • To display images, we need to make use of the Image and Graphics classes Applets / 13 of 27

  14. Passing parameters • Parameters are passed to the applet using the <param> tag in the HTML file • Parameter value is retrieved in the applet using the getParameter() method which returns a string • By the help of parameters we can allow the user to control certain factors Applets / 14 of 27

  15. Example Applets / 15 of 27

  16. Applets and GUI • Default layout of an applet is FlowLayout • The figure below depicts the various controls that can be created • Graphical User Interface is used to create a pictorial interface that is easy to work with Applets / 16 of 27

  17. Handling events with applets • Clicking or pressing the Enter key on GUI components generates event • While designing applets we need to trap these events and provide suitable actions to be performed in response to each of those events • Mouse events in an applet can be handled by overriding mouseDown(), mouseUp(), mouseDrag() methods • Besides the methods, we can also use Listener interfaces Applets / 17 of 27

  18. Graphics class (1) • Graphics class is a part of the java.awt package • It has to be imported into the program • Apart from text, it is possible to draw images, rectangles, lines, polygons and various other graphical representations Applets / 18 of 27

  19. Graphics class (2) Applets / 19 of 27

  20. Graphics class (3) Applets / 20 of 27

  21. Example (1) Applets / 21 of 27

  22. Example (2) Output Applets / 22 of 27

  23. Font class • One constructor of the Font is: • public Font(String name, int style, int pointsize) • name can be “Times New Roman”, “Arial”, etc. • style can be Font.PLAIN, Font.BOLD, Font.ITALIC • pointsize for fonts can be 11,12,14,16,etc. • java.awt.Font class is used to set or retrieve fonts Applets / 23 of 27

  24. Example Output Applets / 24 of 27

  25. FontMetrics class • In such a case, the FontMetrics class proves useful • Commonly used methods of FontMetrics class • int stringWidth(String s) – returns full width of string • int charWidth(char c) – returns width of that character • int getHeight() – returns total height of the font • At times, it is necessary to know the attributes of fonts used within a program Applets / 25 of 27

  26. Example Output Applets / 26 of 27

  27. Color class • Colors can be constructed as shown : • Color a=new Color(255,255,0); • Color b=new Color(0.907F,2F,0F); • To change or set colors for a component : • void setColor(Color)of Graphics class • void setForeground(Color)of Component class,inherited by various components • void setBackground(Color)of Component class,inherited by various components • java.awt.Color class is used to add color to applications and applets Applets / 27 of 27

More Related