Presentation is loading. Please wait.

Presentation is loading. Please wait.

Miscellaneous Topics #6: Polygons GUI Components and Event Handlers

Similar presentations


Presentation on theme: "Miscellaneous Topics #6: Polygons GUI Components and Event Handlers"— Presentation transcript:

1 Miscellaneous Topics #6: Polygons GUI Components and Event Handlers

2 Polygons and Polylines
Arrays can be helpful in graphics processing For example, they can be used to store a list of coordinates A polygon is a multisided, closed shape A polyline is similar to a polygon except that its endpoints do not meet, and it cannot be filled See PolygonShape.java See RocketApp.java

3 The Polygon Class The Polygon class can also be used to define and draw a polygon It is part of the java.awt package Versions of the overloaded drawPolygon() and fillPolygon() methods take a single Polygon object as a parameter instead of arrays of coordinates A Polygon class has the intersects() methods which returns true if the Polygon intersects with a given rectangle. This method takes four argument, the (x,y) top- left coordinate of the rectangle and its width, and height.

4 Events An event is an object that represents some activity to which we may want to respond For example, we may want our program to perform some action when the following occurs: the mouse is moved the mouse is dragged a mouse button is clicked a graphical button is clicked a keyboard key is pressed a timer expires Events often correspond to user actions, but not always

5 Events and Listeners The Java standard class library contains several classes that represent typical events Components, such as a graphical button, generate (or fire) an event when it occurs A listener object "waits" for an event to occur and responds accordingly We can design listener objects to take whatever actions are appropriate when an event occurs

6 A corresponding listener
Events and Listeners Event Component A component object may generate an event Listener A corresponding listener object is designed to respond to the event When the event occurs, the component calls the appropriate method of the listener, passing an object that describes the event

7 GUI Development Generally we use components and events that are predefined by classes in the Java class library Therefore, to create a Java program that uses a Graphical User Interface (GUI) we must: instantiate and set up the necessary components (like JFrames) implement listener classes for any events we care about establish the relationship between listeners and components that generate the corresponding events Let's now look at one such listener that works for the JFrame components and see how this all comes together

8 KeyListener Example Listener classes are written by implementing a listener interface The KeyAdapter class implements the KeyListener interface An interface is a list of methods that the implementing class must define One method in the KeyListener interface that is implemented in the KeyAdapter class is the KeyTyped method, and this method is sent the event when a keyboard character is typed The Java class library contains interfaces for many types of events

9 KeyListener Example Here is an example of how to add a handler for certain typed keyboard characters to a GraphicsWindow object (named “graphicsWin”) by creating a KeyListener for it’s JFrame: graphicsWin.getFrame.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (ch == 'q' || ch == 'Q') System.exit(0); } });

10 KeyListener Example The keyTyped method only handles alpha-numeric keys that are typed. You need to use the keyPressed() method to handle special keys: graphicsWin.getFrame.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent e) { char ch = e.getKeyChar(); int code = e.getKeyCode(); if (ch == 'q' || ch == 'Q') System.exit(0); if (ch == 'u' || ch == 'U' || code == KeyEvent.VK_UP) paddle.moveUp(); if (ch == 'd' || ch == 'D' || code == KeyEvent.VK_DOWN) paddle.moveDown(); } });

11 KeyListener Example Another approach is to send the characters typed to a separate method like handleKey() to take the appropriate action: graphicsWin.getFrame.addKeyListener(new KeyAdapter() { public void keyTyped(KeyEvent e) { handleKey(e.getKeyChar()); } }); public static void handleKey(char ch) if (ch == 'p' || ch == 'P') pauseSimulation = (pauseSimulation) ? false : true; if (ch == 'q' || ch == 'Q') System.exit(0);

12 KeyListener Example Another approach is to send the characters typed to a separate SubClass like KeyListener below, which has a keyPressed() method to take the appropriate action: graphicsWin.getFrame.addKeyListener(new KeyListener()); public static class KeyListener extends KeyAdapter { public void keyPressed(KeyEvent e) { char ch = e.getKeyChar(); int code = e.getKeyCode(); if (ch == 'u' || ch == 'U' || code == KeyEvent.VK_UP) paddle.moveUp(); if (ch == 'd' || ch == 'D' || code == KeyEvent.VK_DOWN) paddle.moveDown(); } });


Download ppt "Miscellaneous Topics #6: Polygons GUI Components and Event Handlers"

Similar presentations


Ads by Google