Applets Session 8. Java Simplified / Session 8 / 2 of 31 Review The Abstract Windowing Toolkit (AWT) is a set of classes that allow us to create a graphical.

Slides:



Advertisements
Similar presentations
Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Advertisements

Java Applet & JavaScript SNU OOPSLA Lab. October 2005.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
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.
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.
18-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.
1 Review of applets & Computer Graphis GUILecture 6 Review of Applets & Compute Graphics GUI Overview  Introduction to Graphics. Applets: a quick review.
Applets, Graphical User Interfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads.
Applets. An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from a browser You.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
26-Jun-15 Applets. 2 An applet is a Panel that allows interaction with a Java program A applet is typically embedded in a Web page and can be run from.
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.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
CSTP WS00CS423 (cotter)1 Java Applets Objective: Learn how to develop Java programs that interact with users through a Web browser.
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.
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 2 - Part 2 Creating a Java Application and Applet.
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.
Applets.
JAPPLET.
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 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Cs884(Prasad)java12AWT1 Abstract Windowing Toolkit Support for Graphical User Interface (Event-driven programming)
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Java Applets. An applet is a Panel that allows interaction with a Java program. A applet is typically embedded in a Web page and can be run from a browser.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Java Applets: GUI Components, Events, Etc. Ralph Westfall June, 2010.
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.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include: a rich set of user interface components; a.
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 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Copyright © Curt Hill Applets A different type of program.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
Creating a Java Application and Applet
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
CHAPTER Agenda Applets Servelets Browsers HelloWorld.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
CSI 3125, Preliminaries, page 1 Event Handling. CSI 3125, Preliminaries, page 2 Event Handling An Event Change in the state of an object is known as event.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Events and Event Handling
Java Applets.
Applets.
Lecture 09 Applets.
INFSY 547: WEB-Based Technologies
UNIT-5.
Java Applets.
Events, Event Handlers, and Threads
Java Programming COMP-417 Applet
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
APPLET PROGRAMMING.
Presentation transcript:

Applets Session 8

Java Simplified / Session 8 / 2 of 31 Review The Abstract Windowing Toolkit (AWT) is a set of classes that allow us to create a graphical user interface and accepts user input through the keyboard and the mouse. A component is anything that can be placed on a user interface and be made visible or resized. Commonly used examples of components are textfields, labels, checkboxes, textareas. Frame and Panel are commonly used containers for standalone applications. A Panel is generally used to group many smaller components together. JDK1.2 follows the Event Delegation Model in which the program registers handlers called listeners, with the objects.

Java Simplified / Session 8 / 3 of 31 Review Contd… KeyboardFocusManager class was developed to know which component GAINED_FOCUS or LOST_FOCUS. It was added in the JDK 1.4. Events are dispatched in an orderly manner. One event has to be fully handled before another event can be handled. Each component has its own set of Keys for traversing. ContainerOrderFocusTraversalPolicy and DefaultFocusTraversalPolicy are the two standard FocusTraversalPolicy which can be implemented by the client. Programmatic traversal is also possible by using methods of KeyboardFocusManager class.

Java Simplified / Session 8 / 4 of 31 Objectives Define an applet Differentiate between Java Applications and Java Applets Create an applet Identify how parameters are passed to applets Discuss event handling with Applets Explain Classes such as Graphics class Font class FontMetrics class Color class

Java Simplified / Session 8 / 5 of 31 Applets Created by subclassing from 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.

Java Simplified / Session 8 / 6 of 31 Difference between Applets and Applications Applets are created by extending the java.applet.Applet class. There is no such constraint for an application. Applets run on any browser. Applications run using Java interpreter. An applet is basically designed for deploying on the web. An application is designed to work as a standalone program.

Java Simplified / Session 8 / 7 of 31 Difference between Applets and Applications Contd… Applet must contain at least one public class failing which the compiler reports an error. It is not mandatory to declare main() for an applet. In case of application, main() has to be included in a public class. Output to an Applet’s window is done by using different AWT methods such as drawString(). In case of an application System.out.println() method is used. Execution of applets begin with the init() method. Execution of applications begins with main() method.

Java Simplified / Session 8 / 8 of 31 Life cycle of an Applet 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.

Java Simplified / Session 8 / 9 of 31 Life cycle of an Applet Contd… 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. The methods are as follows:

Java Simplified / Session 8 / 10 of 31 Redraw Applet stop( ) Start state start( ) paint( ) Life cycle of an Applet Contd… Applet Working Applet Born Applet Displayed Idle State Applet Destroyed Initialization state destroy( ) Destroy Applet init( )

Java Simplified / Session 8 / 11 of 31 A simple applet Output import java.awt.*; import java.applet.*; public class FirstApplet extends Applet { String str; public void init() { str = "Java is interesting!"; } public void paint(Graphics g) { g.drawString(str, 70, 80); }

Java Simplified / Session 8 / 12 of 31 Create a HTML page to display the applet Then type the following at command prompt: appletviewer abc.html where abc.html is the name of the html file. Creating an Applet An applet is compiled using the Java compiler: javac javac Firstapplet.java

Java Simplified / Session 8 / 13 of 31 Displaying images using Applets Output /* */ import java.awt.*; import java.applet.*; public class DisplayImage extends Applet { Image img; public void init() { img = getImage(getCodeBase(),"duke.gif"); } public void paint(Graphics g) { g.drawImage(img,20,20,this); }

Java Simplified / Session 8 / 14 of 31 Displaying images using Applets Contd… getCodeBase() method gets the base URL of the applet 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.

Java Simplified / Session 8 / 15 of 31 Passing parameters Parameters are passed to the applet using the tag in the HTML file. Parameter value is retrieved in the applet using the getParameter() method which returns a string. Parameters allow the user to control certain factors of the applet.

Java Simplified / Session 8 / 16 of 31 Example import java.awt.*; import java.applet.*; public class ImageDemo extends Applet { Image img; public void init() { String imagename = getParameter("image"); img = getImage(getCodeBase(),imagename); } public void paint(Graphics g) { g.drawImage(img,20,20,this); }

Java Simplified / Session 8 / 17 of 31 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.

Java Simplified / Session 8 / 18 of 31 Handling events with applets While designing applets we need to trap these events and provide suitable actions to be performed in response to each of those events To handle the events, event handlers are available that must be suitably manipulated The procedure to be followed when an event is generated are: determine the type of the event determine the component which generated the event write appropriate code to handle the event Clicking or pressing the Enter key on GUI components generates an event

Java Simplified / Session 8 / 19 of 31 Example /* */ import java.awt.*; import java.applet.*; import java.awt.event.*; public class Mousey extends Applet implements MouseListener, MouseMotionListener { int x1, y1, x2, y2; public void init() { setLayout(new FlowLayout()); setBounds(100,100,300,300); addMouseListener(this); addMouseMotionListener(this); this.setVisible(true); } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { x1 = e.getX(); y1 = e.getY(); } public void mouseMoved(MouseEvent e) {} public void mouseReleased(MouseEvent e) { x2 = e.getX(); y2 = e.getY(); repaint(); } public void mouseEntered(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void paint(Graphics g) { g.drawRect(x1, y1, x2-x1, y2-y1); x2 = 0; y2 = 0; } Output

Java Simplified / Session 8 / 20 of 31 Graphics, Colors, and Fonts Any GUI based program without images or colors looks dull and lifeless. To enhance their appearance, it is recommended that we use images wherever possible. General procedure to draw images would be: Obtain the URL or path of the image to be displayed. Decide upon the position (coordinates) at which image is to be displayed. Supply all these information using an appropriate method.

Java Simplified / Session 8 / 21 of 31 Graphics class Graphics class is a part of the java.awt package. It has to be imported into the program. Drawing operations is accomplished using this class. Apart from text, it is possible to draw images, rectangles, lines, polygons and various other graphical representations.

Java Simplified / Session 8 / 22 of 31 Example public void mousePressed(MouseEvent e) { x1 = e.getX(); x2 = e.getY(); } public void mouseMove(MouseEvent e) { x3 = e.getX(); x4 = e.getY(); repaint(); } public void mouseReleased(MouseEvent e) { x3 = e.getX(); x4 = e.getY(); repaint(); } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} /* */ import java.applet.*; import java.awt.*; import java.awt.event.*; public class Painting extends Applet implements ActionListener, MouseListener { Button bdraw = new Button("Draw Rectangle"); int count = 0,x1,x2,x3,x4; public void init() { BorderLayout border = new BorderLayout(); setLayout(border); add(bdraw, BorderLayout.PAGE_END); bdraw.addActionListener(this); addMouseListener(this); this.setVisible(true); } public void mouseClicked(MouseEvent e) {}

Java Simplified / Session 8 / 23 of 31 Example Contd… Output public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if("Draw Rectangle".equals(str)) { count = 1; repaint( ); } public void paint(Graphics g) { if(count == 1) { g.drawRect(x1,x2,(x3-x1),(x4-x2)); x3 = x4 = 0; }

Java Simplified / Session 8 / 24 of 31 Font class One of the constructor of the Font class is: public Font(String name, int style, int pointsize) name can be “Times New Roman”, “Arial” and so on. style can be Font.PLAIN, Font.BOLD, Font.ITALIC pointsize for fonts can be 11,12,14,16 and so on. java.awt.Font class is used to set or retrieve fonts.

Java Simplified / Session 8 / 25 of 31 /* */ import java.applet.*; import java.awt.*; public class FontDemo extends Applet { public void paint(Graphics g) { String quote = "Attitude is the mind’s paintbrush; it can color any situation "; Font objFont = new Font("Georgia",Font.ITALIC,16); g.setFont(objFont); g.drawString(quote,20,20); } Example Output

Java Simplified / Session 8 / 26 of 31 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.

Java Simplified / Session 8 / 27 of 31 Example /* */ import java.applet.*; import java.awt.*; public class TextCentre extends Applet { public void paint(Graphics g) { String myquote = "Happiness is an attitude."; Font objFont = new Font("Times New Roman", Font.BOLD|Font.ITALIC, 24); FontMetrics fm = getFontMetrics(objFont); g.setFont(objFont); int numx = (getSize().width - fm.stringWidth(myquote))/2; int numy = getSize().height/2; g.drawString(myquote,numx,numy); } Output

Java Simplified / Session 8 / 28 of 31 Determining Available Fonts We should always know which fonts are available on the machine. We can use a method called getAvailableFontFamilyNames () defined in the GraphicsEnvironment class. The syntax of the method is as follows: String[] getAvailableFontFamilyNames(): returns an array of Strings that contains the names of the available font families. Font[] getAllFonts(): returns an array of Font objects for all the available fonts.

Java Simplified / Session 8 / 29 of 31 Color class Objects of Color class 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.

Java Simplified / Session 8 / 30 of 31 Summary An Applet is a Java program that can be executed with the help of a Java enabled browser. Every user-defined applet must extend the java.applet.Applet class. A user defined applets inherits all the methods of Applet class... tags are used within a HTML file to embed a class file. The default layout for an applet is FlowLayout. Images can be drawn on an applet by means of the paint(), getImage() and drawImage() methods. Whenever the user performs an action such as moving the mouse, pressing a key, releasing the key and so on, an event is generated. We can make use of event handler classes and interfaces to handle these events.

Java Simplified / Session 8 / 31 of 31 Summary Contd… Event handling in applets in the simplest form can be handled by overriding the mouseDown(), mouseUp(), mouseDrag() methods. The Graphics class is used to draw objects like text, lines ovals and arcs on the screen. The Font class is used to make text look attractive in the output of a Java program. The FontMetrics class is used to obtain information about a Font. GraphicsEnvironment class has methods to get information about the available fonts in the system. The Color class is used to add colors to an application or applet.

Java Simplified / Session 8 / 32 of 31 Assignment Animating a Series of Images