Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Lecturte 14Lecture 7 Applications of Graphics Overview  Conversions Between Applications and Applets  Handling Mouse Events  Handling Keyboard Events."— Presentation transcript:

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


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

Similar presentations


Ads by Google