Chapter 14 Applets and More. 2 Contents I. Introduction to Applets II. A Brief Introduction to HTML III. Creating Applets with Swing IV. Using AWT for.

Slides:



Advertisements
Similar presentations
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Advertisements

1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Chapter 14 Applets. 2 Knowledge Goals Understand the differing roles of applications and applets Understand how a browser operates Understand the role.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Chapter 9: Applets Jim Burns Fall Outline Learn about applets Learn about applets Write an HTML doc to host an applet Write an HTML doc to host.
Applets Java API.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
Chapter 13 Advanced GUIs and Graphics. Chapter Objectives Learn about applets Explore the class Graphics Learn about the class Font Explore the class.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
JAPPLET.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 14: Applets and More Starting Out with Java: From Control Structures.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Chapter 8 Introduction to HTML and Applets Fundamentals of Java.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Chapter 13Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 13 l HTML l Applets Applets and HTML.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Timer Objects  regularly generates action events at programmer-specified time intervals  Timer class is in javax.swing package  Timer (int delay, ActionListener.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
MSc Workshop - © S. Kamin, U. ReddyLect 3 - GUI -1 Lecture 3 - Graphical User Interfaces r GUI toolkits in Java API r JFrame r GUI components.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
Applets Yong Choi School of Business CSU, Bakersfield.
Java Programming Applets. Topics Write an HTML document to host an applet Understand simple applets Use Labels with simple AWT applets Write a simple.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
Creating Applets. What is an applet? What is an applet? A Java program that runs in a web browser. A Java program that runs in a web browser. An applet.
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
Field Trip #23 Hangman By Keith Lynn. JApplet A JApplet is a top-level container An applet is a small Java program that is executed by another program.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Chapter 131 Applets and HTML Chapter Objectives learn how to write applets learn to write a simple HTML document learn how to embed an applet in.
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Introduction to Applets Chapter 21. Applets An applet is a Java application that is intended to be invoked and executed through a Web browser. Click Here.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
CHAPTER 14 Applets and More Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Introduction to Java Applets Will not cover Section 3.7 Thinking About Objects: Identifying.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
CHAPTER 14 Applets and More Copyright © 2016 Pearson Education, Ltd.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Java Applets.
Applets.
A First Look at GUI Applications
Lecture 09 Applets.
Java Programming: Guided Learning with Early Objects
Chapter 13: Advanced GUIs and Graphics
UNIT-5.
Chapter 14: Applets and More
Java Applets.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Advanced GUIs and Graphics
Presentation transcript:

Chapter 14 Applets and More

2 Contents I. Introduction to Applets II. A Brief Introduction to HTML III. Creating Applets with Swing IV. Using AWT for Portability V. Drawing Shapes VI. Handling Mouse Events VII. Timer Objects VIII. Playing Audio

3 I. Introduction to Applets Applets are Java programs that are usually part of a Web site. If a user opens the Web site with a Java-enable browser, the applet is executed inside the browser window. When a user accesses a Web page on a server with the browser, any applets associated with the Web page are transmitted over the Internet from the server to the user's system. Once the applets are transmitted, the user's system executes them.

4 I. Introduction to Applets Applets are important. To extend the capabilites of a Web page. Web pages are normally written in Hypertext Markup Language (HTML). HTML is limited: It merely describes the content and layout of a Web page, and creates links to other files and Web pages. HTML does not have sophisticated abilities such as performing math calculations and interacting with the user.

5 I. Introduction to Applets Most browsers have a special version of the JVM for running applets. This JVM greatly restricts what an applet can do. Applets cannot delete files, read the contents of files, or create files on the user's system. Applets cannot run any other program on the user's system. Applets cannot execute operating system procedures on the user's system. Applets cannot retrieve information about the user's system, or user's identity. Applets cannot make network connections with any system except the server from which the applet was transmitted.

6 I. Introduction to Applets If an applet displays a window, it will automatically have a message such as “Warning Applet Window” display in it. It lets the user know that the window was not displayed by an application on his system.

7 II. A Brief Introduction to HTML When creating a Web page, we use Hypertext Markup Language (HTML) to create a file that can be read and processed by a Web browser. Hypertext Hypertext can contain a link to another Web page, or another location in the same Web page. When the user clicks on the hypertext, it loads the Web page or the location that the hypertext is linked to.

8 II. A Brief Introduction to HTML Markup Language It allows us to mark up a text file by inserting special instructions. These instructions tell the browser how to format the text and create any hypertext link. To make a web page, we create a text file that contains HTML instructions (tags), as well as the text that should be displayed on the web page. HTML document.html file name extension

9 II. A Brief Introduction to HTML Most HTML tags come in pairs. Text Tag names are case-insensitive. Document Structure Tags Web page with a title only My First Web Page

10 II. A Brief Introduction to HTML Web page with a body Java Applications and Applets There are two types of programs you can create with Java: Applications and Applets.

11 II. A Brief Introduction to HTML Text Formating Tags

12 II. A Brief Introduction to HTML Header Levels This is an H1 Header This is an H1 Header This is an H1 Header This is an H1 Header This is an H1 Header This is an H1 Header This is regular unformatted text.

13 II. A Brief Introduction to HTML To center a line of text in the browser window. To display text in boldface To display text in italics

14 Creating Breaks in Text The tag causes a line break to appear at the point in the text where it is inserted. The tag causes a paragraph break. A paragraph break inserts more space into the text than a line break. The tag causes a horizontal rule.

15 Inserting Links A link is some element in a Web page that can be clicked on by the user. When the user clicks the link, another Web page is displayed. Text The Text is the text that will be displayed in the Web page. Address is often referred as a uniform resource locator (URL).

16 III. Creating Applets with Swing An applet as a GUI application that runs under the control of a Web browser. Instead of displaying its own window, an applet appears in the browser's window. The differences between GUI application code and applet code: A GUI application class inherits from JFrame. An applet class inherits from JApplet. A GUI application class has a constructor. An applet class does not normally have a constructor. Instead, it has a method named init that performs the same operations as a constructor. The init method accepts no arguments and has a void return type.

17 The following methods are not called in an applet: setTitle SetSize SetDefaultCloseOperation Pack setVisible There is no static main method needed to create an instance of the applet class. The browser creates an instance of the class automatically.

18 An applet that displays a label

19 Running an Applet To run an applet, we create an HTML document with an APPLET tag Filename.class : The name of the applet's.class file. Wide : The width of the applet in pixels High : The height of the applet in pixels

20 SimpleApplet.html

21 Handling Events in an Applet In an applet, events are handled with event listeners exactly as they are in GUI applications. An example: Write a web page with an applet. It has a text field where the user can enter a Fahrenheit temperature and a Converter button that converts the temperature to centigrade and displays it in a read-only text field.

22

23

24

25

26

27 IV. Using AWT for Portability Applets that use Swing components may be incompatible with some browsers. Microsoft Internet Explorer Old versions of Netscape Navigator... To make sure that an applet is compatible with all Java-enabled browsers, use AWT components instead of Swing.

28 IV. Using AWT for Portability

29 IV. Using AWT for Portability The Swing classes were intentionally designed with constructors and methods that are similar to those of their AWT counterparts. Events are handled in the same way for each set of classes. An example: Write a version of the TempConverter applet that has been rewritten to use AWT components instead of Swing components.

30

31

32

33

34

35 IV. Using AWT for Portability The only modifications that were made were as follows: The JApplet, JPanel, JLabel, JTextField, and JButton classes were replaced with the Applet, Panel, Label, TextField, and Button classes. The import javax.swing.*; statement was removed.

36 V. Drawing Shapes Java allows us to draw lines and graphical shapes such as rectangles, ovals, and arcs. These lines and shapes are drawn directly on components. This allows a frame or a panel to become a canvas for drawings. The XY Coordinate System The X coordinate identifies a pixel's horizontal location. The Y coordinate identifies a pixel's vertical location.

37 V. Drawing Shapes The coordinates of the pixel in the upper-left corner of a component are (0, 0). X and Y coordinates on a 300 pixel wide by 200 pixel high component (0, 0) (0, 199) (299, 199) (299, 0)

38 V. Drawing Shapes Graphical Objects Each component has an internal object that inherits from the Graphics class, which is part of java.awt package. Some of the Graphics class methods void setColor(Color c) Color getColor() void drawLine(int x1, int y1, int x2, int y2) void drawRect(int x, int y, int width, int height) void fillRect(int x, int y, int width, int height)

39 V. Drawing Shapes void drawOval(int x, int y, int width, int height) void fillOval(int x, int y, int width, int height) void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) void drawPolygon(int[] xPoints, int[] yPoints, int numPoints) void fillPolygon(int[] xPoints, int[] yPoints, int numPoints) void drawString(String str, int x, int y) void setFont(Font f)

40 V. Drawing Shapes To get a reference to a component's Graphics object: Override the paint method in any class that extends as follows: JApplet JFrame Any AWT class, including Applet and Frame public void paint(Graphics g)

41 V. Drawing Shapes public void paint(Graphics g) The paint method is responsible for displaying, or painting a component on the screen. It is automatically called when the component is first displayed and is called again any time the component needs to be redisplayed. The Graphics object that belongs to that component is automatically passed as an argument. By overriding the paint method, we can use the Graphics object argument to draw graphics on the component. We should always call the superclass's paint method before doing anything else. This ensures that the component will be displayed properly on the screen.

42

43 LineDemo.html Line Demo

44

45

46

47

48

49 V. Drawing Shapes The repaint method We do not call a component's paint method. It is automatically called when the component must be redisplayed. Sometimes, however, we might want to force the application or applet to call the paint method. Call the repaint method The repaint method clears the surface of the component and then calls the paint method.

50 V. Drawing Shapes Drawing on Panels To draw on a panel, we need to get a reference to the panel's Graphics object and then use the object's methods to draw. To get a reference to the panel's Graphics object: overriding the paintComponent method. This is true not only for JPanel objects, but also for all Swing components except JApplet and JFrame. The paintComponent method is automatically called when the component needs to be redisplayed.

51 V. Drawing Shapes An example: Write an applet that show a window. A set of check boxes is displayed in a JPanel component on the right side of the window. The white area in the left side of the window is another JPanel component. When one of the check boxes is selected, a shape appears in the JPanel component in the left side.

52

53

54

55

56 VI. Handling Mouse Events Handling Mouse Events The mouse generates two types of events: Mouse events To handle mouse events we need to create a mouse listener class. Mouse motion events To handle mouse motion events we need to create a mouse motion listener class.

57 VI. Handling Mouse Events A mouse listener class (which must implement the MouseListener interface) can respond to any of the following events: 1.The mouse button is pressed. 2.The mouse button is released. 3.The mouse button is clicked (pressed, then released without moving the mouse). 4.The mouse cursor enters a component's screen space. 5.The mouse cursor exits a component's screen space.

58 VI. Handling Mouse Events Methods required by the MouseListener interface

59 VI. Handling Mouse Events The MouseEvent object contains data about the mouse event. getX() : returns the X coordinates of the mouse cursor when the event occurs. getY() : returns the Y coordinates of the mouse cursor when the event occurs. We can register the mouse listener object with a component using the addMouseListener method.

60 VI. Handling Mouse Events A mouse motion listener (which must implement the MouseMotionListener interface) can respond to the following events: The mouse is dragged (the button is pressed and the mouse is moved while the button is held down). The mouse is moved.

61 VI. Handling Mouse Events Methods required by the MouseMotionListener interface

62 VI. Handling Mouse Events

63

64

65

66 VI. Handling Mouse Events Using Adapter Classes Many times when we handle mouse events, we will not be interested in handling every event that the mouse generates. An example: Write an applet which only handles mouse pressed and mouse dragged events. The applet lets you draw rectangles by pressing the mouse button and dragging the mouse inside the applet window. To draw a rectangle, you must drag the mouse cursor to the right and below the position where you initially pressed the mouse button.

67

68

69

70 VI. Handling Mouse Events Instead of implementing the MouseListener or MouseMotionListener interfaces, we can extend our classes from the MouseAdapter or MouseMotionAdapter classes. The MouseAdapter and MouseMotionAdapter classes implement the MouseListener and MouseMotionListener interfaces and provide empty definitions for all of the required methods Java provides an adapter class for all of the interfaces in the API that have more than one method.

71

72

73 VII. Timer Objects Timer objects automatically generate action events at regular time intervals. It is useful when we want a program to perform an operation at certain times or after an amount of time has passed. The general form of Timer class's constructor: Timer(int delay, ActionListener listener) delay : The amount of time between action events, measured in miliseconds. listener : A reference to an action listener that is to be registered with the Timer object. It can be null and we can register an action listener using addActionListener method.

74 VII. Timer Objects Timer class methods

75 VII. Timer Objects For example, a Timer object can be used to perform simple animation by moving a graphic image across the screen by a certain amount at regular time intervals.

76 VII. Timer Objects

77

78 VIII. Playing Audio Sounds that have been stored in an audio file may be played from a Java program. Java applets can play audio files. The file formats directly supported are as follows:.aif or aiff (Macintosh Audio File).au (Sun Audio File).mid or.rmi (MIDI File).wav (Windows Wave File)

79 VIII. Playing Audio In order to play audio files, use the Applet class's play method. void play(URL baseLocation, String filename) To get URL object, it is common to use either the getDocumentBase or getCodeBase methods. getDocumentBase : Returns a URL object containing the location of the HTML file that invoked the applet. play(getDocumentBase(), “mysound.wav”);

80 VIII. Playing Audio getCodeBase : Returns a URL object containing the location of the applet's.class file. play(getDocumentBase(), “mysound.wav”);

81 VIII. Playing Audio Using an AudioClip Object The Applet class's play method loads a sound file, plays it one time, and then releases it for garbage collection. If you need to load a sound file to be played multiple times, you should use an AudioClip object. An AudioClip object is an object that implements the AudioClip interface.

82 VIII. Playing Audio The AudioClip interface specifies three methods: play : Plays a sound one time. loop : Repeatedly plays a sound. stop : Causes a sound to stop playing. The Applet class's getAudioClip method can be used to create an AudioClip object for a given sound file: AudioClip getAudioClip(URL baseLocation, String filename)

83 VIII. Playing Audio AudioClip clip = getAudioClip(getDocumentBase(), “mysound.wav”); To play the sound file: clip.play(); The sound file can be played repeatedly with: clip.loop(); Any time the sound file is being played, the following statement can be used to stop it clip.stop();

84 VIII. Playing Audio

85

86

87 VIII. Playing Audio Playing Audio in an Application The process of getting a reference to an AudioClip object is different in a class that does not extend JApplet class.

88

89

90

91