10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.

Slides:



Advertisements
Similar presentations
2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Advertisements

Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
© The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
Event Handling Events and Listeners Timers and Animation.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
 2005 Pearson Education, Inc. All rights reserved Graphics and Java 2D™
1 L38 Graphics and Java 2D™ (3). 2 OBJECTIVES In this chapter you will learn:  To understand graphics contexts and graphics objects.  To understand.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Event Handling. In this class we will cover: Keyboard Events Mouse Events Focus Events Action Interface Multicasting.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction.
Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.
Chapter 11 Java AWT Part I: Mouse Events (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved CheckWriter Application Introducing Graphics and Printing.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
Java Graphics Swing Graphics
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
7/3/00SEM107- © Kamin & ReddyClass 11 - Events - 1 Class 11 - Events r A couple of odds & ends m Component sizes  switch statement r Event types r Catching.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
MSc Workshop - © S. Kamin, U.Reddy Lect 4 - Events - 1 Lecture 4 – Event Handling r Painting r Event types r Catching different event types.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Agenda Introduction. Event Model. Creating GUI Application. Event Examples.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Mouse Listeners Moving the mouse will also generate events like the Timer –To have your program respond, you must implement either or both of MouseListener.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 10.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Event Handling H_Func(Event) { } Event Receiver Object Source Object Registration.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
CS 240 Week 9. EventQueue.invokeLater In Swing, all user interface operations must occur on the “UI thread” – All components should be created on the.
Jaeki Song ISQS6337 JAVA Lecture 10 Applets. Jaeki Song ISQS6337 JAVA Applet Applets run within the Web browser environment –Applets bring dynamic interaction.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Mouse, Keyboard, Sounds, and Images JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Prepared by: Dr. Abdallah Mohamed, AOU-KW Unit7: Event-driven programming 1.
12 Graphics and Java 2D™.
Mouse Event Handling in Java (Review)
Lecture 09 Applets.
Computer Science 209 Graphics and GUIs.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Graphics and Multimedia
JAVA 2 Design and programming of GUI
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Chapter 13: Advanced GUIs and Graphics
Introduction to Computing Using Java
12 Graphics and Java 2D™.
Web Design & Development Lecture 12
Events, Event Handlers, and Threads
Advanced GUIs and Graphics
Presentation transcript:

10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse motion events –Key Event

10/20/2005week72 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/2005week73 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API (not required in this course) –More sophisticated graphics capabilities Drawing custom 2D shapes Filling shapes with colors and patterns

10/20/2005week74 Classes used from Java’s original graphics capabilities and from the Java2D API. Classes and interfaces from the Java2D API that appear in package java.awt Object Color Component Font FontMetrics Graphics Polygon Graphics2D Classes from the Java2D API that appear in package java.awt.geom GradientPaint BasicStroke TexturePaint RectangularShape GeneralPath Line2D RoundRectangle2D Arc2D Ellipse2D Rectangle2D

10/20/2005week75 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/2005week76 Java coordinate system. Units are measured in pixels

10/20/2005week77 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 ) –Called through method repaint

10/20/2005week78 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/2005week79 Color constants and their RGB values

10/20/2005week710 Color methods and color-related Graphics methods

10/20/2005week711 Font Control Class Font –Contains methods and constants for font control –Font constructor takes three arguments Font name –Monospaced, SansSerif, 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/2005week712 Font -related methods and constants

10/20/2005week713 Font -related methods and constants

10/20/2005week714 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/2005week715 Font metrics (not required in this course)

10/20/2005week716 FontMetrics and Graphics methods for obtaining font metrics

10/20/2005week717 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/2005week718 Graphics methods that draw lines, rectangles and ovals

10/20/2005week719 Graphics methods that draw lines, rectangles and ovals (not required in this course)

10/20/2005week720 First version of tictactoe board

10/20/2005week721 First version of tictactoe board Note : a) Color change b) Font change c) drawing line d) drawing filled rect ShowTicTaeToe

10/20/2005week722 Second version of TicTacToe 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/2005week723 Second version of TicTacToe The user has selected row = 0 and column = 1.

10/20/2005week724 Second version of TicTacToe Next the user has selected row = 2 and column = 0. The X and O will be used alternately.

10/20/2005week725 Second version of TicTacToe Changes from earlier version - We now show moves by user. New method used fillOval(x, y, width, height) Note use of repaint() and use of super in paint

10/20/2005week726 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. /week7/TicTacToe/TicTacToe.java

10/20/2005week727 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 are a number of mouse events. There are 2 types of mouse related events - –mouse events –mouse motion events The corresponding listeners are –MouseListeners –MouseMotionListeners

10/20/2005week728 Event handlers for events with mouse Two basic types of mouse events –Mouse events with the event handlers mousePressed, mouseClicked, mouseReleased, mouseEntered and mouseExited –Mouse motion events with the event handlers mouseDragged mouseMoved Your program must include all handlers!!!

10/20/2005week729 What do we wish to achieve? See the sample run of the program: /week7/TicTacToe/TicTacToe2.java

10/20/2005week730 Idea used in the application Display board as usual. Listen for the event “mousePressed” 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/2005week731 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/2005week732 What we wish to achieve? See the sample run of the program: /week7/TicTacToe/TicTacToe3.java

10/20/2005week733 Idea used in the application Everything is the same as in the earlier application. In addition, listen for the event “mouseDragged” 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/2005week734 Structure of program // Import classes just as before public class TicTacToe3 extends JFrame implements MouseListener, MouseMotionListener { // Var declaractions // Constructor //paint method // mouse event methods as needed // other methods as needed // mouse motion events – mouse dragged, // mouse moved // main method }

10/20/2005week735 public void mouseDragged( MouseEvent e ) public void mouseMoved( MouseEvent e ) Additional methods needed public void mouseReleased( MouseEvent e )

10/20/2005week736 Key Event Handling Interface KeyListener –Handles key events Generated when keys on keyboard are pressed and released KeyEvent –Contains virtual key code that represents key..\..\week7\keydemo