Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Made with love, by Zachary Langley Applets The Graphics Presentation.
Mouse Events and Keyboard Events
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, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 15 Event-Driven Programming.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 14 Event-Driven Programming.
1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
1 Applets. 2 Motivations When browsing the Web, you frequently see the graphical user interface and animation developed using Java. These programs are.
Event-Driven Programming
Applet class The Applet class provides the essential framework that enables applets to be run by a web browser Applet do not have main method Applet depend.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Object Oriented Programming (OOP) LAB # 5 TA. Maram & TA. Mubaraka TA. Kholood & TA. Aamal.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Java Applets. Applets The term Applet refers to a little application. In JAVA the applet is a java program that is embedded within a HTML document and.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
Applets  The Applet Class  The HTML Tag F Passing Parameters to 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.
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.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
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.
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.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
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.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Applets and Multimedia.
Omer Boyaci.  GUIs are event driven.  When the user interacts with a GUI component, the interaction—known as an event—drives the program to perform.
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.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Index Event Handling Events Event Source Event Listener Event Classes Action Event Class Adjustment event Class Event Source Event Listener Interface Using.
COSC 4126 User Interaction User Interaction capturing and responding to input events.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Creating User Interfaces Event-Driven Programming.
1 Event Handling – Lecture 4 Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
1 Chapter 3 Event-Driven Programming. 2 Objectives F To explain the concept of event-driven programming (§12.2). F To understand event, event source,
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.
Mouse Events GUI. Types of Events  Below, are some of the many kinds of events, swing components generate. Act causing EventListener Type User clicks.
Java Applets Getting Started. Import Files In order to run the applet properly, we have to import some files into our class. These files are: –import.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Chapter 14 Event-Driven Programming
Java Applets.
Applets.
Chapter 12 Event-Driven Programming
Lecture 09 Applets.
Java Applets.
Chapter 12 Applets and Advanced Graphics
Programming in Java Event Handling
Chapter 13: Advanced GUIs and Graphics
Distributed Computing, M. L. Liu
Java Applets.
Java Applets.
Java applets 1/3/2019.
Chapter 16 Event-Driven Programming
Advanced GUIs and Graphics
Presentation transcript:

Jaeki Song ISQS6337 JAVA Lecture 10 Applets

Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction and live animation to an otherwise static HTML page Java is used not only as applets, but also as standalone applications and as a programming language for developing sever-side applications Applications and applets share many common programming features, although they differ slightly in some respects –Every application must have a main method –Java applets do not need a main method

Jaeki Song ISQS6337 JAVA Applets Applications are stand-along program, but applets are programs that are called from within another application –Runs applets within a page on the Internet, or within another program called appletviewer Embedded in HTML code with operning curly bracket and following it immediartely with the closing curly braket

Jaeki Song ISQS6337 JAVA Example <Applet code = “test.class” width = 300 Height = 200> –Code = the name of the compiled applet you are calling –Width = the width of the applet on the screen –Height = the height of the applet on the screen

Jaeki Song ISQS6337 JAVA The Applet Class public class MyApplet extends JApplet { public void init() {... } public void start() {... } public void stop() {... } public void destroy() {... } //your other methods }

Jaeki Song ISQS6337 JAVA Browser Calling Applet Methods

Jaeki Song ISQS6337 JAVA The inti method Invoked when the applet is first loaded and again if the applet is reloaded. public void init( ); can be seen and used by other classes and programs, such as the browser Common functions implemented in this method include loading images, setting up user-interface components, and getting parameters from the tag in the HTML page.

Jaeki Song ISQS6337 JAVA The start method Invoked after the init() method is executed; also called whenever the applet becomes active again after a period of inactivity –For example, when the user returns to the page containing the applet after surfing other Web pages.

Jaeki Song ISQS6337 JAVA The stop Method The opposite of the start() method, which is called when the user moves back to the page containing the applet; the stop() method is invoked when the user moves off the page.

Jaeki Song ISQS6337 JAVA The destroy Method Invoked when the browser exits normally to inform the applet that it is no longer needed and that it should release any resources it has allocated Usually, you will not need to override this method unless you need to release specific resources, such as threads that the applet created.

Jaeki Song ISQS6337 JAVA The JApplet Class To use swing components, it is necessary to create a Java Applet that extends javax.swing.JApplet –JApplet inherits all the methods from the Applet class import javax.swing.*; public class Welcome extends JApplet{ }

Jaeki Song ISQS6337 JAVA Writing Applets Always extends the JApplet class, which is a subclass of Applet for Swing components. Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. Add your own methods and data if necessary. Applets are always embedded in an HTML page.

Jaeki Song ISQS6337 JAVA Viewing Applets <applet code = “test.Welcome.class” width= 300 height = 100>

Jaeki Song ISQS6337 JAVA Example HelloWorld Mortgage

Jaeki Song ISQS6337 JAVA Font class Font constructor takes three arguments –Font name, font style, and font size –General Format Font (FontName, FontStyle, FontSize) Font fntName = new Font (“Sans Serif”, Font.BOLD, 12) Methods –getFont ( ) –setFont (Font f ) Sets the current font to font, style, and size specified by the Font object reference f E.g lblName.setFont(fntName);

Jaeki Song ISQS6337 JAVA The FontMetrics Class Can be used to know precise information about a font, such as height, descent, ascent, and leading By baseline heightascent leading descent

Jaeki Song ISQS6337 JAVA FontMetrics Methods getAscent( ) –Return a value representing the ascent of a font getDescent( ) –Return a value representing the descent of a font getLeading( ) –Return a value representing the leading of a font getHeight( ) –Return a value representing the height of a font

Jaeki Song ISQS6337 JAVA Color class Defines methods and constants for manipulating colors –Pre-defined color –RGB based: 256 colors Constructors –Three arguments Integer and float public Color (int r, int g, int b) public color (float r, float g, float b)

Jaeki Song ISQS6337 JAVA Color Class Methods getRed( ) –Return a value between 0 to 255 representing the red content getGreen( ) –Return a value between 0 to 255 representing the green content getBlue( ) –Return a value between 0 to 255 representing the blue content getColor –Return the current color setColor –Sets the current color

Jaeki Song ISQS6337 JAVA Changing the Color of Text setForeground method –Changes the color of the letter –E.g lblName.setForeground(Color.red); setBackground method –Changes the background color –E.g. setBackground(Color.cyan) Pre-defined colors –black, blue, cyan, darkGray, gray, green, pink, lightGray, magenta, orange, red, white, yellow

Jaeki Song ISQS6337 JAVA Example FontApplet

Jaeki Song ISQS6337 JAVA Mouse Event Many actions taken by the user cause events to occur with mouse –Click the mouse, move it over an object, move away from an object Java allows you to listen for mouse events using a MouseListner or MouseMotionListner

Jaeki Song ISQS6337 JAVA Handling Mouse Events The MouseListener listens for actions such as when the mouse is pressed, released, entered, exited, or clicked. The MouseMotionListener listens for actions such as dragging or moving the mouse.

Jaeki Song ISQS6337 JAVA MouseEvent Handlers mouseEnter(MouseEvent e) and mouseExit(MouseEvent e) –invoked when a mouse enters a component or exits the component mousePressed(MouseEvent e) –Invoked when a mouse is pressed or released mouseClicked(MouseEvent e) –Invoked when a mouse is pressed and then released mouseMoved(MouseEvent e) –Invoked when the mouse is moved without being pressed

Jaeki Song ISQS6337 JAVA Example MouseListener

Jaeki Song ISQS6337 JAVA Keyboard Event Handling Key events are generated when key on the keyboard are pressed and released A class that implements KeyListener must provide definitions for method keyPressed, keyReleased and keyTyped

Jaeki Song ISQS6337 JAVA InputEvent Methods public long getWhen( ) –Returns the time stamp indicating when the event occured public boolean isAltDown( ) –Returns true if the Alt key is down on the event public boolean isControlDown( ) –Returns true if the Control key is down on the event public boolean is MetaDown –Returns true if the right mouse button is pressed public boolean is ShiftDown –Returns true if the Shift key is down on the event

Jaeki Song ISQS6337 JAVA Handling Keyboard Events keyPressed(KeyEvent e) Called when a key is pressed. keyReleased(KeyEvent e) Called when a key is released. keyTyped(KeyEvent e) Called when a key is pressed and then released. To process a keyboard event, use the following handlers in the KeyListener interface:

Jaeki Song ISQS6337 JAVA The KeyEvent Class Methods: getKeyChar() method getKeyCode() method Keys: Home VK_HOME End VK_End Page Up VK_PGUP Page Down VK_PGDN etc..