Graphics mouse and mouse motion events Key Event
- Slides: 36
Graphics, mouse and mouse motion events, Key. Event Agenda • Classes in AWT for graphics • Example java programs – Graphics – Mouse events – Mouse motion events – Key Event 10/20/2005 week 7 1
Classes in Abstract Windowing Toolkit • • Color - define and manipulate colors Font Graphics - draw Strings, lines, rectangles etc We will illustrate these classes through an example - a tictactoe board and how to handle moves by the user. 10/20/2005 week 7 2
Introduction • Java’s graphics capabilities – Drawing 2 D shapes – Controlling colors – Controlling fonts • Java 2 D API (not required in this course) – More sophisticated graphics capabilities • Drawing custom 2 D shapes • Filling shapes with colors and patterns 10/20/2005 week 7 3
Classes used from Java’s original graphics capabilities and from the Java 2 D API. Object Color Component Font. Metrics Graphics Polygon Classes and interfaces from the Java 2 D API that appear in package java. awt Graphics 2 D Basic. Stroke Gradient. Paint Texture. Paint Classes from the Java 2 D API that appear in package java. awt. geom General. Path Line 2 D Rectangular. Shape Arc 2 D 10/20/2005 week 7 Ellipse 2 D Rectangle 2 D 4
coordinate system • Java’s coordinate system – Scheme for identifying all points on screen – Upper-left corner has coordinates (0, 0) – Coordinate point composed of x-coordinate and y-coordinate 10/20/2005 week 7 5
Java coordinate system. Units are measured in pixels 10/20/2005 week 7 6
Graphics Contexts and Graphics Objects • Graphics context – Enables drawing on screen – Graphics object manages graphics context • Controls how information is drawn – Class Graphics is abstract • Cannot be instantiated • Contributes to Java’s portability – Class Component method paint takes Graphics object public void paint( Graphics g ) 10/20/2005 week 7 – Called through method repaint 7
Color Control • Class Color – Defines methods and constants for manipulating colors – Colors are created from red, green and blue components • RGB values • Check the API page for Color 10/20/2005 week 7 8
Color constants and their RGB values 10/20/2005 week 7 9
Color methods and color-related Graphics methods 10/20/2005 week 7 10
Font Control • Class Font – Contains methods and constants for font control – Font constructor takes three arguments • Font name – Monospaced, Sans. Serif, etc. • Font style – Font. PLAIN, Font. ITALIC and Font. BOLD • Font size – Measured in points (1/72 of inch) • Check the API page for Font 10/20/2005 week 7 11
Font-related methods and constants 10/20/2005 week 7 12
Font-related methods and constants 10/20/2005 week 7 13
Font Control • Font metrics – Height – Descent (amount character dips below baseline) – Ascent (amount character rises above baseline) – Leading (difference between descent and ascent) 10/20/2005 week 7 14
Font metrics 10/20/2005 week 7 (not required in this course) 15
Font. Metrics and Graphics methods for obtaining font metrics 10/20/2005 week 7 16
Drawing Lines, Rectangles and Ovals • Class Graphics – Provides methods for drawing lines, rectangles and ovals • All drawing methods require parameters width and height 10/20/2005 week 7 17
Graphics methods that draw lines, rectangles and ovals 10/20/2005 week 7 18
Graphics methods that draw lines, rectangles and ovals (not required in this course) 10/20/2005 week 7 19
First version of tictactoe board 10/20/2005 week 7 20
First version of tictactoe board Note : a) Color change b) Font change c) drawing line d) drawing filled rect 10/20/2005 week 7 Show. Tic. Tae. Toe 21
Second version of Tic. Tac. Toe In this version we want the user to be able to make a move. We will only show a single O or X. The user can specify the row and column to select. Alternately a O or an X will be displayed. 10/20/2005 week 7 22
Second version of Tic. Tac. Toe The user has selected row = 0 and column = 1. 10/20/2005 week 7 23
Second version of Tic. Tac. Toe Next the user has selected row = 2 and column = 0. The X and O will be used alternately. 10/20/2005 week 7 24
Second version of Tic. Tac. Toe Changes from earlier version • We now show moves by user. • New method used fill. Oval(x, y, width, height) • Note use of repaint() and use of super in paint 10/20/2005 week 7 25
Idea : a) While(forever) b) Display the board c) get row and column from user d) determine where the move is to be displayed e) display X and O in alternate iterations How to display X ? Use 2 lines. How to display 0 ? Use filled circle. 10/20/2005 /week 7/Tic. Tac. Toe. java week 7 26
Next version of Tictactoe • In this version, we wish to incorporate mouse events so that users can specify the move by clicking in the desired position. There a number of mouse events. • There are 2 types of mouse related events – mouse motion events • The corresponding listeners are – Mouse. Listeners – Mouse. Motion. Listeners 10/20/2005 week 7 27
Event handlers for events with mouse • Two basic types of mouse events – Mouse events with the event handlers • mouse. Pressed, mouse. Clicked, mouse. Released, mouse. Entered and mouse. Exited – Mouse motion events with the event handlers • mouse. Dragged • mouse. Moved • 10/20/2005 Your program must week 7 include all handlers!!! 28
What do we wish to achieve? See the sample run of the program: /week 7/Tic. Tac. Toe 2. java 10/20/2005 week 7 29
Idea used in the application • Display board as usual. • Listen for the event “mouse. Pressed” • When it happens, find out where it occurred. • Determine which square it is • Show a circle or a X in the designated square. • Note that in this application we are not concerned with mouse motions!! 10/20/2005 week 7 30
Final version of Tictactoe • In this final version, we wish to have the ability to change our mind after pressing a mouse button to specify the position of X or O. • This can be done by dragging the mouse. • The position becomes final only when the mouse is released • This illustrates the use of mouse motion handlers 10/20/2005 week 7 31
What we wish to achieve? See the sample run of the program: /week 7/Tic. Tac. Toe 3. java 10/20/2005 week 7 32
Idea used in the application • Everything is the same as in the earlier application. • In addition, listen for the event “mouse. Dragged” • When it happens, find out the current position of the mouse. • As the mouse moves from square to square, move the figure with it. • The move is final only when mouse is released. 10/20/2005 week 7 33
Structure of program // Import classes just as before public class Tic. Tac. Toe 3 extends JFrame implements Mouse. Listener, Mouse. Motion. Listener { // Var declaractions // Constructor //paint method // mouse event methods as needed // other methods as needed // mouse motion events – mouse dragged, // mouse moved // main method week 7 34 } 10/20/2005
Additional methods needed public void mouse. Dragged( Mouse. Event e ) public void mouse. Moved( Mouse. Event e ) public void mouse. Released( Mouse. Event e ) 10/20/2005 week 7 35
Key Event Handling • Interface Key. Listener – Handles key events • Generated when keys on keyboard are pressed and released • Key. Event – Contains virtual key code that represents key . . week 7keydemo 10/20/2005 week 7 36
- Chapter 2 section 1 describing motion answer key
- What is acceleration
- Motion section 1 describing motion
- Describing motion section 1 answer key
- Graphics monitor and workstation in computer graphics
- Little mouse where is your house
- Simple event and compound event
- Windows mouse events
- Computer mouse event is
- Computer mouse event is
- Mutually exclusive events vs not mutually exclusive events
- 3d viewing devices in computer graphics ppt
- Near miss analysis
- Independent event vs dependent event
- Dependent events examples
- Event contents
- Bridge breaks in central java killing 5
- Newsworthy event (s); background event (s); sources
- Motion graphics definition
- What is motion specification in computer graphics
- Motion specification in computer graphics
- Range of motion active and passive
- Business key activities
- Key partners key activities key resources
- Punic wars who fought
- Review exercise terms and events answer key
- Shm formula list
- An object in motion stays in motion
- Motion section 1 describing motion
- Things fall apart chapter 18-19 summary
- Renaissance characteristics
- Summary of the renaissance
- The crucible act 1 characters map
- Independent events
- Probability of compound events answer key
- Vocabulary viper
- What is key events in a story