Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.

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

Graphics You draw on a Graphics object The Graphics object cannot directly be created by your code, instead one is generated when the method paintComponent.
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
1 Graphics -- Introduction zThe use of graphics is common among modern software systems  Java has strong API support for graphics in the java.awt (abstract.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Graphics Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Swing II.
May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS Lecture 7-1 A picture's worth a thousand words.
Slides prepared by Rose Williams, Binghamton University Chapter 18 Applets.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
CS102--Object Oriented Programming Lecture 19: – The Swing Package (II) Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
Chapter 18 Swing II Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics F The architecture.
© The McGraw-Hill Companies, 2006 Chapter 10 Graphics and event- driven programs.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Drawing pictures with Java. JFrame: the basic Java window The swing package contains classes, objects and methods that can be used to create a consistent.
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.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
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.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Review of Graphics in Java,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes.
(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.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 14 : Swing II King Fahd University of Petroleum & Minerals College of Computer Science.
Lecture 3.1 Using Graphics with JFrame. © 2006 Pearson Addison-Wesley. All rights reserved javax.swing.JFrame - int x - int y - int width - int.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Intro to Graphics from Chapter 2 of Java Software Solutions
Eleventh Graphics in Java.
Chapter 18 Swing II Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage Copyright © 2016 Pearson Inc.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
Making a Smile Face Pepper.
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II

© 2006 Pearson Addison-Wesley. All rights reserved19-2 The Graphics Class Coordinate System for Graphics Objects When drawing objects on the screen, Java uses a coordinate system where the origin point (0,0) is at the upper-left corner of the screen area used for drawing –The x-coordinate (horizontal) is positive and increasing to the right –The y- coordinate(vertical) is positive and increasing down –All coordinates are normally positive –Units and sizes are in pixels –The area used for drawing is typically a JFrame or JPanel

© 2006 Pearson Addison-Wesley. All rights reserved19-3 Coordinate System for Graphics Objects The point (x,y) is located x pixels in from the left edge of the screen, and down y pixels from the top of the screen When placing a rectangle on the screen, the location of its upper-left corner is specified When placing a figure other than a rectangle on the screen, Java encloses the figure in an imaginary rectangle, called a bounding box, and positions the upper-left corner of this rectangle

© 2006 Pearson Addison-Wesley. All rights reserved19-4 Screen Coordinate System

© 2006 Pearson Addison-Wesley. All rights reserved19-5 The Method paint and the Class Graphics Almost all Swing and Swing-related components and containers have a method called paint The method paint draws the component or container on the screen –It is already defined, and is called automatically when the figure is displayed on the screen –However, it must be redefined in order to draw geometric figures like circles and boxes –When redefined, always include the following: super.paint(g);

© 2006 Pearson Addison-Wesley. All rights reserved19-6 The Method paint and the Class Graphics Every container and component that can be drawn on the screen has an associated Graphics object –The Graphics class is an abstract class found in the java.awt package This object has data specifying what area of the screen the component or container covers –The Graphics object for a JFrame specifies that drawing takes place inside the borders of the JFrame object

© 2006 Pearson Addison-Wesley. All rights reserved19-7 The Method paint and the Class Graphics The object g of the class Graphics can be used as the calling object for a drawing method –The drawing will then take place inside the area of the screen specified by g The method paint has a parameter g of type Graphics –When the paint method is invoked, g is replaced by the Graphics object associated with the JFrame –Therefore, the figures are drawn inside the JFrame

© 2006 Pearson Addison-Wesley. All rights reserved19-8 Drawing a Very Simple Face (part 1 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-9 Drawing a Very Simple Face (part 2 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-10 Drawing a Very Simple Face (part 3 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-11 Drawing a Very Simple Face (part 4 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-12 Drawing a Very Simple Face (part 5 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-13 Some Methods in the Class Graphics ( part 1 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved19-14 Some Methods in the Class Graphics ( part 2 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved19-15 Some Methods in the Class Graphics ( part 3 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved19-16 Some Methods in the Class Graphics ( part 4 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved19-17 Drawing Ovals An oval is drawn by the method drawOval –The arguments specify the location, width, and height of the smallest rectangle that can enclose the oval g.drawOval(100, 50, 300, 200); A circle is a special case of an oval in which the width and height of the rectangle are equal g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);

© 2006 Pearson Addison-Wesley. All rights reserved19-18 Drawing a Happy Face (Part 1 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-19 Drawing a Happy Face (Part 2 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-20 Drawing a Happy Face (Part 3 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-21 Drawing a Happy Face (Part 4 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-22 Drawing a Happy Face (Part 5 of 5)

© 2006 Pearson Addison-Wesley. All rights reserved19-23 Drawing Arcs Arcs are described by giving an oval, and then specifying a portion of it to be used for the arc –The following statement draws the smile on the happy face: g.drawArc(X_MOUTH, Y_MOUTH, MOUTH_WIDTH, MOUTH_HEIGHT, MOUTH_START_ANGLE, MOUTH_ARC_SWEEP); –The arguments MOUTH_WIDTH and MOUTH_HEIGHT determine the size of the bounding box, while the arguments X_MOUTH and Y_MOUTH determine its location –The last two arguments specify the portion made visible

© 2006 Pearson Addison-Wesley. All rights reserved19-24 Specifying an Arc (Part 1 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved19-25 Specifying an Arc (Part 2 of 2)

© 2006 Pearson Addison-Wesley. All rights reserved19-26 Rounded Rectangles A rounded rectangle is a rectangle whose corners have been replaced by arcs so that the corners are rounded g.drawRoundRect(x, y, width, height, arcWidth, arcHeight) –The arguments x, y, width, and height determine a regular rectangle in the usual way –The last two arguments arcWidth, and arcHeight, specify the arcs that will be used for the corners –Each corner is replaced with an quarter of an oval that is arcWidth pixels wide and arcHeight pixels high –When arcWidth and arcHeight are equal, the corners will be arcs of circles

© 2006 Pearson Addison-Wesley. All rights reserved19-27 A Rounded Rectangle

© 2006 Pearson Addison-Wesley. All rights reserved19-28 paintComponent for Panels A JPanel is a JComponent, but a JFrame is a Component, not a JComponent –Therefore, they use different methods to paint the screen Figures can be drawn on a JPanel, and the JPanel can be placed in a JFrame –When defining a JPanel class in this way, the paintComponent method is used instead of the paint method –Otherwise the details are the same as those for a JFrame

© 2006 Pearson Addison-Wesley. All rights reserved19-29 paintComponent Demonstration (Part 1 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved19-30 paintComponent Demonstration (Part 2 of 4) 0

© 2006 Pearson Addison-Wesley. All rights reserved19-31 paintComponent Demonstration (Part 3 of 4) 0

© 2006 Pearson Addison-Wesley. All rights reserved19-32 paintComponent Demonstration (Part 4 of 4) 0

© 2006 Pearson Addison-Wesley. All rights reserved19-33 Action Drawings and repaint The repaint method should be invoked when the graphics content of a window is changed –The repaint method takes care of some overhead, and then invokes the method paint, which redraws the screen –Although the repaint method must be explicitly invoked, it is already defined –The paint method, in contrast, must often be defined, but is not explicitly invoked

© 2006 Pearson Addison-Wesley. All rights reserved19-34 An Action Drawing (Part 1 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-35 An Action Drawing (Part 2 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-36 An Action Drawing (Part 3 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-37 An Action Drawing (Part 4 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-38 An Action Drawing (Part 5 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-39 An Action Drawing (Part 6 of 7)

© 2006 Pearson Addison-Wesley. All rights reserved19-40 An Action Drawing (Part 7 of 7)