Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction."— Presentation transcript:

1 Jaeki Song ISQS6337 JAVA Lecture 10 Applets

2 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

3 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

4 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

5 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 }

6 Jaeki Song ISQS6337 JAVA Browser Calling Applet Methods

7 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.

8 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.

9 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.

10 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.

11 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{ }

12 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.

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

14 Jaeki Song ISQS6337 JAVA Example HelloWorld Mortgage

15 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);

16 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

17 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

18 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)

19 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

20 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

21 Jaeki Song ISQS6337 JAVA Example FontApplet

22 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

23 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.

24 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

25 Jaeki Song ISQS6337 JAVA Example MouseListener

26 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

27 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

28 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:

29 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..


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

Similar presentations


Ads by Google