1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 18 Applets and Multimedia.
Advertisements

Mouse Events and Keyboard Events
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
Event-Driven Programming
Applet Class Hierarchy. Applet Methods called by the system public void init() When the applet is first loaded. Do initialization here. public void start()
Event-Driven Programming
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
1 GUI 5Lecture 7 GUI - V Overview  Using No Layout Manager  Message Dialog Boxes  Running a Program as an Applet and an Application  More on Handling.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 16 Event-Driven Programming.
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.
Mouse Events. Handling Mouse Events Java provides two listener interfaces to handle mouse events: MouseListener;  MouseListener;  MouseMotionListener.
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 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 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.
Timer Objects  regularly generates action events at programmer-specified time intervals  Timer class is in javax.swing package  Timer (int delay, ActionListener.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
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.
The Drawing program – Java Applets
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 16 Applets and Multimedia.
Lec 16 Adding Mouse and KeyEvent handlers to an Applet Class.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Lesson 6 Programming Techniques Event Handling /EvH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
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.
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.
Chapter 16 Multimedia F Audio Files F Playing Audio F Running Audio on a Separate Thread F Displaying Images F Displaying a Sequence of Images  Using.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Practical Session 10: Animation. Animation kinds There are three basic kinds of animation: 1. Moving object 2. Changing pictures 3. Combination of 1 and.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Creating User Interfaces Event-Driven Programming.
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.
עקרונות תכנות מונחה עצמים תרגול 7: אנימציה. בשבוע שעבר  paint  Graphics  repaint.
Nested Classes and Event Handling Chapter 10. Overview We explain how to write Java code that responds to events. The Timer class can be used to respond.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
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.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Lesson 28: More on the GUI button, frame and actions.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
Chapter 14: Multimedia Audio Files Audio Files Playing Audio Playing Audio Running Audio on a Separate Thread Running Audio on a Separate Thread Displaying.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
GUI Programming in Java Hao Jiang Boston College April, 2009.
Import javax.swing.*; class Check { public static void main(String[] args) { JFrame frame = new JFrame("Check"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//DO_NOTHING_ON_CLOSE.
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.
Chapter 14 Event-Driven Programming
Java Applets.
Applets.
Chapter 12 Event-Driven Programming
Chapter 12 Applets and Advanced Graphics
Chapter 13: Advanced GUIs and Graphics
Java Applets.
Chapter 16 Event-Driven Programming
Advanced GUIs and Graphics
Presentation transcript:

1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events  Example Keyboard Events Demo  Audio Files.  Example Using Audio Clips

2 Lecturte 14Lecture 7 Conversions Between Applications and Applets l Conversions between applications and applets are simple and easy. l You can always convert an applet into an application. l You can convert an application to an applet as long as security restrictions are not violated. Converting Applications into Applets 1- Drop the class with the main method that shows the frame 2- Inherit from Japplet, not Jframe 3-Remove the window listener 4 Remove the call to setsize and instead set the size in the applet’s HTML page 5- Remove the call to setTitle

3 Lecturte 14Lecture 7 More on Handling Mouse Events Java provides two listener interfaces, MouseListener and MouseMotionListener, to handle 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.

4 Lecturte 14Lecture 7 // MoveMessageDemo.java: Move a message in a panel // by dragging the mouse import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MoveMessageDemo extends JApplet { // Initialize the applet public void init() { // Create a MoveMessagePanel instance for drawing a message MoveMessagePanel p = new MoveMessagePanel("Welcome to Java"); // Place the message panel in the frame getContentPane().setLayout(new BorderLayout()); getContentPane().add(p); } // This main method enables the applet to run as an application public static void main(String[] args) { // Create a frame JFrame frame = new JFrame("Move Message Using Mouse"); Example Moving message using Mouse

5 Lecturte 14Lecture 7 // Create an instance of the applet MoveMessageDemo applet = new MoveMessageDemo(); // Add the applet instance to the frame frame.getContentPane().add(applet, BorderLayout.CENTER); // Invoke init() and start() applet.init(); applet.start(); // Display the frame frame.setSize(300, 300); // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLO SE); frame.setVisible(true); } // MoveMessagePanel draws a message // This class is defined as inner class class MoveMessagePanel extends MessagePanel implements MouseMotionListener { // Construct a panel to draw string s public MoveMessagePanel(String s) { super(s); this.addMouseMotionListener(this); } // Tell the panel how to draw things public void paintComponent(Graphics g) { // Invoke the paintComponent method in the MessagePanel class super.paintComponent(g); } // Handler for mouse moved event public void mouseMoved(MouseEvent e) { } // Handler for mouse dragged event public void mouseDragged(MouseEvent e) { // Get the new location and repaint the screen setXCoordinate(e.getX()); setYCoordinate(e.getY()); repaint(); } MoveMessageDemo

6 Lecturte 14Lecture 7 l Handling Keyboard Events To process a keyboard event, use the following handlers in the KeyListener interface: More 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

7 Lecturte 14Lecture 7 The KeyEvent Class Methods: getKeyChar() method getKeyCode() method Keys: Home VK_HOME End VK_End Page Up VK_PGUP Page Down VK_PGDN The enter keyVK_ENTER etc...

8 Lecturte 14Lecture 7 Example Keyboard Events Demo l Objective: Display a user-input character. The user can also move the character up, down, left, and right using the arrow keys. // KeyboardEventDemo.java: Receive key input import java.awt.*; import java.awt.event.*; import javax.swing.*; public class KeyboardEventDemo extends JApplet { private KeyboardPanel keyboardPanel = new KeyboardPanel(); // Main method used if run as an application public static void main(String[] args) { // Create a frame JFrame frame = new JFrame("KeyboardEvent Demo"); // Create an instance of the applet KeyboardEventDemo applet = new KeyboardEventDemo(); // Add the applet instance to the frame frame.getContentPane().add(applet, BorderLayout.CENTER); // Invoke init() and start() applet.init(); applet.start();

9 Lecturte 14Lecture 7 // Display the frame frame.setSize(300, 300); // frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.setVisible(true); // Set focus on the keyboardPanel applet.focus(); } // Initialize UI public void init() { // Add the keyboard panel to accept and display user input getContentPane().add(keyboardPanel); // Request focus focus(); } // Set focus on the panel public void focus() { // It is required for receiving key input keyboardPanel.requestFocus(); } Example Keyboard Event (Cont)

10 Lecturte 14Lecture 7 // KeyboardPanel for receiving key input class KeyboardPanel extends JPanel implements KeyListener { private int x = 100; private int y = 100; private char keyChar = 'A'; // Default key public KeyboardPanel() { addKeyListener(this); // Register listener } public void keyReleased(KeyEvent e) { } Example Keyboard Event (Cont)

11 Lecturte 14Lecture 7 public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_DOWN: y += 10; break; case KeyEvent.VK_UP: y -= 10; break; case KeyEvent.VK_LEFT: x -= 10; break; case KeyEvent.VK_RIGHT: x += 10; break; default: keyChar = e.getKeyChar(); } repaint(); } // Draw the character public void paintComponent(Graphics g) { super.paintComponent(g); g.setFont(new Font("TimesRoman", Font.PLAIN, 24)); g.drawString(String.valueOf(keyChar), x, y); } Example Keyboard Event (Cont)

12 Lecturte 14Lecture 7 Audio Files l Audio is stored in files. There are several formats of audio files. JDK 1.2 can play several audio file formats, including.wav and.au files. play(URL url, String filename); Plays the audio clip after it is given the URL and the file name that is relative to the URL. Nothing happens if the audio file cannot be found. l play(getCodeBase(), "soundfile.au"); Plays the sound file soundfile.au, located in the applet’s directory. l play(getDocumentBase(), "soundfile.au"); Plays the sound file soundfile.au, located in the HTML file’s directory.

13 Lecturte 14Lecture 7 Using Audio Clips l public AudioClip getAudioClip(URL url); l public AudioClip getAudioClip(URL url, String name); Either method creates an audio clip. Specify String name to use a relative URL address. l public abstract void play() l public abstract void loop() l public abstract void stop() Use these methods to start the clip, play it repeatedly, and stop the clip, respectively.

14 Lecturte 14Lecture 7 import java.awt.Graphics; import java.awt.Image; import java.applet.AudioClip; /* plays audio sound and displays image */ public class AudioSound extends java.applet.Applet { AudioClip nachid; public void init() { nachid = getAudioClip(getDocumentBase(), "salam_al_malaki.au"); } public void paint(Graphics g) { nachid.loop(); // Looping the salam_al_malaki audio file !!! } example displays images and continuously play a sound clip until the applet window is closed

15 Lecturte 14Lecture 7 Example of playing sound import java.applet.AudioClip; import java.applet.Applet; import java.awt.*; import java.net.URL; import java.net.MalformedURLException; public class AudioTest1 { public static void main(String[] args) { MyFrame frame = new MyFrame(); frame.show(); } // End of main method } // End of class AudioTest1 class MyFrame extends Frame { private Image im; public MyFrame() { setTitle("Audio Using Frames"); setSize(400,400); } // End of MyFrame() constructor public void paint(Graphics g) { String relativeURL = " spacemusic.au "; AudioClip audio; //String relativeURL = "spacemusic"; URL baseURL = null; // Initialize the URL reference URL completeURL = null; // Initialize the URL reference

16 Lecturte 14Lecture 7 try { baseURL = new URL("file:" + System.getProperty("user.dir") + "/"); // Finding out the File System Type } catch (MalformedURLException e) { System.err.println(e.getMessage()); } try { completeURL = new URL(baseURL, relativeURL); } catch (MalformedURLException e){ System.err.println(e.getMessage()); } audio = Applet.newAudioClip(completeURL); audio.loop(); // Playing the sound file in a loop } // End of paint() method } // End of MyFrame class Example (Cont.)