Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing.

Slides:



Advertisements
Similar presentations
Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
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?
Georgia Institute of Technology Drawing in Java – part 2 Barb Ericson Georgia Institute of Technology September 2005.
Draw Shapes Introduction to simple graphics. What is a graphics context? An instance of the Graphics class Graphics is ABSTRACT! You can extend Graphics.
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.
Graphics in Java Part II. Lecture Objectives Learn how to use Graphics in Java.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
1 L37 Graphics and Java 2D™ (2). 2 OBJECTIVES To use methods of class Graphics to draw lines,  rectangles,  rectangles with rounded corners,  three-dimensional.
May 11, 1998CS102-02Lecture 7-1 More Graphics in Java CS Lecture 7-1 A picture's worth a thousand words.
 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 12.1 Introduction 12.2 Graphics Contexts and Graphics Objects 12.3 Color Control 12.4 Font Control.
Topics AWT Classes Window Fundamentals Working with Frame Windows Creating a Frame Window in an Applet Creating a Windowed Program Displaying Information.
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.
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 11 - Graphics and Java 2D Outline 11.1 Introduction 11.2 Graphics Contexts and Graphics Objects 11.3 Color Control 11.4 Font Control 11.5 Drawing.
Modernās Programmēšanas Tehnoloģijas (Advanced Programming Technologies) Edgars Celms, Mārtiņš Opmanis Latvijas Universitātes Matemātikas.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 11 - Graphics and Java 2D Outline 11.1 Introduction 11.2 Graphics Contexts and Graphics Objects.
(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.
 Pearson Education, Inc. All rights reserved. 1 Ch 12 Graphics and Java 2D In this chapter you will learn:  To understand graphics contexts.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
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.
Graphics & Java 2D Drawing Two Dimensional Shapes Controlling Fonts Controlling Colors.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
1 Chapter 3 2D Graphics: Rendering Details  Color spaces, paints stroke types  Affine transforms including translation, rotation, scaling, shearing,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 - Graphics and Java2D Outline 11.1Introduction 11.2Graphics Contexts and Graphics Objects 11.3Color.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
(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.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.
6.1 Coordinates The screen of a computer is a grid of little squares called pixels. The color of each pixel can be set individually, and drawing on the.
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Graphics Drawing Things With Java. Today's Topics Course Updates Java Graphics –Java 1.1 versus Java 1.2 (Java 2) Drawing Lines Drawing Shapes.
Java Graphics and Java2D
Intro to Graphics from Chapter 2 of Java Software Solutions
12 Graphics and Java 2D™.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Java Graphics CS 2511.
JAVA 2 Design and programming of GUI
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
CASE Tools Graphical User Interface Programming Using C#
Graphics -- Introduction
12 Graphics and Java 2D™.
Chapter 12 - Graphics and Java 2D
Presentation transcript:

Chapter 28 - Java Graphics and Java2D Outline 28.1Introduction 28.2Graphics Contexts and Graphics Objects 28.3Color Control 28.4Font Control 28.5Drawing Lines, Rectangles and Ovals 28.6Drawing Arcs 28.7Drawing Polygons and Polylines 28.8The Java2D API 28.9Java2D Shapes

Objectives In this chapter, you will learn: –To understand graphics contexts and graphics objects. –To understand and be able to manipulate colors. –To understand and be able to manipulate fonts. –To understand and be able to use Graphics methods for drawing lines, rectangles, rectangles with rounded corners, three-dimensional rectangles, ovals, arcs and polygons. –To use methods of class Graphics2D from the Java2D API to draw lines, rectangles, rectangles with rounded corners, ellipses, arcs and general paths. –To be able to specify Paint and Stroke characteristics of shapes displayed with Graphics2D.

28.1Introduction In this chapter –Draw 2D shapes –Colors –Fonts Java appealing for its graphics support –Has a class hierarchy for its graphics classes and 2D API classes

28.1Introduction

Java coordinate system –(x,y) pairs x - horizontal axis y - vertical axis –Upper left corner is ( 0, 0 ) –Coordinates measured in pixels (smallest unit of resolution)

28.1Introduction

28.2Graphics Contexts and Graphics Objects Graphics context –Enables drawing on screen –Graphics object manages graphics context Controls how information is drawn Has methods for drawing, font manipulation, etc We have used Graphics object g for applets –Graphics an abstract class Cannot instantiate objects Implementation hidden - more portable Class Component –Superclass for many classes in java.awt –Method paint takes Graphics object as argument

28.2Graphics Contexts and Graphics Objects Class Component –paint called automatically when applet starts –paint often called in response to an event Drawing graphics is an event-driven process. –repaint calls update, which forces a paint operation update rarely called directly Sometimes overridden to reduce "flicker" Headers: public void repaint() public void update( Graphics g ) In this chapter –Focus on paint method

28.3Color Control Class Color –Defines methods and constants for manipulating colors –Colors created from red, green, and blue component RGB value: 3 integers from 0 to 255 each, or three floating point values from 0 to 1.0 each Larger the value, more of that color –Color methods getRed, getGreen, getBlue return an integer between 0 and 255 representing amount –Graphics method setColor sets drawing color Takes Color object –Method getColor gets current color setting

28.3Color Control

Outline ShowColors.java (1 of 2)

Outline ShowColors.java (2 of 2)

28.3Color Control Component JColorChooser –Displays dialog allowing user to select a color –Method showDialog First argument: reference to parent Component (window from which dialog being displayed) –Modal dialog - user cannot interact with other dialogs while active Second argument: String for title bar Third argument: Initial selected color Returns a Color object Class Container –Method setBackground - takes Color object –Sets background color

Outline ShowColors2.java (1 of 2)

Outline ShowColors2.java (2 of 2)

28.3Color Control Fig The HSB and RGB tabs of the JColorChooser dialog.

28.3Color Control Fig The HSB and RGB tabs of the JColorChooser dialog.

28.4Font Control

Class Font –Constructor takes three arguments public Font( String name, int style, int size) name: any font supported by system ( Serif, Monospaced ) style: constants FONT.PLAIN, FONT.ITALIC, FONT.BOLD –Combinations: FONT.ITALIC + FONT.BOLD size: measured in points (1/72 of an inch) –Usage: g.setFont( fontObject );

Outline Fonts.java (1 of 2)

Outline Fonts.java (2 of 2)

28.4Font Control Figure 28.9 Using Graphics method setFont to change Font s.

28.4Font Control Class FontMetrics –Has methods for getting font metrics –g.getFontMetrics - returns FontMetrics object

28.4Font Control

Outline Metrics.java (1 of 2)

Outline Metrics.java (2 of 2)

28.4 Font Control Figure Obtaining font metric information.

28.5Drawing Lines, Rectangles and Ovals

Outline LinesRectsOvals.j ava (1 of 2)

Outline LinesRectsOvals.j ava (2 of 2)

28.5Drawing Lines, Rectangles and Ovals Figure Demonstrating Graphics method drawLine. drawLinedrawRectfillRectdraw3DRect fill3DRect fillOvaldrawOvaldrawRoundRectfillRoundRect

28.5Drawing Lines, Rectangles and Ovals

28.6Drawing Arcs Arc –Portion of an oval –Arc angles measured in degrees Starts at a starting angle and sweeps the number of degrees specified by arc angle Positive - counterclockwise Negative - clockwise –When drawing an arc, specify bounding rectangle for an oval –drawArc( x, y, width, height, startAngle, arcAngle ) –fillArc - as above, but draws a solid arc (sector)

28.6Drawing Arcs

Outline DrawArcs.java (1 of 3)

Outline DrawArcs.java (2 of 3)

Outline DrawArcs.java (3 of 3)

28.7Drawing Polygons and Polylines Polygon - multisided shape –In Java, class Polygon java.awt Polyline - series of connected points

28.7Drawing Polygons and Polylines

Outline DrawPolygons.java (1 of 3)

Outline DrawPolygons.java (2 of 3)

Outline DrawPolygons.java (3 of 3)

28.7Drawing Polygons and Polylines Figure Demonstrating drawPolygon and fillPolygon. Result of line 22Result of line 41Result of line 32 Result of line 27

28.8The Java2D API Java2D API –Advanced two dimensional graphics capabilities –Too many capabilities to cover (for overview, see demo) Drawing with the Java2D API –Use instance of class Graphics2D (package java.awt ) Subclass of Graphics Has all graphics capabilities we have previously discussed –Must downcast Graphics reference passed to paint Graphics2D g2d = ( Graphics2D ) g; –This technique used in programs of next section

28.9Java2D Shapes Sample methods from Graphics2D –setPaint ( paintObject ) Paint object is an object of a class that implements java.awt.Paint Can be an object of class Color, GradientPaint, SystemColor, TexturePaint –GradientPaint ( x1, y1, color1, x2, y2, color2, cyclic ) Creates a gradient (slowly changing color) from x1, y1, to x2, y2, starting with color1 and changing to color2 If cyclic true, then cyclic gradient (keeps transitioning colors) –If acyclic, only transitions colors once

28.9Java2D Shapes Sample methods from Graphics2D –fill ( shapeObject ) Draws a filled Shape object Instance of any class that implements Shape ( java.awt ) Ellipse2D.Double, Rectangle2D.Double Double-precision inner classes of Ellipse2D –setStroke( strokeObject ) Set a shape's borders Instance of a class that implements Stroke ( java.awt ) BasicStroke( width ) - One of many constructors –This constructor specifies width in pixels of border

28.9Java2D Shapes Sample methods from Graphics2D –draw ( shapeObject ) Draws specified Shape object –Class BufferedImage Can produce images in color or grayscale Can create patterns by drawing into the BufferedImage object –Class TexturePaint Constructor can take BufferedImage and shape to fill Object of class TexturePaint then drawn using setPaint Book has further details

28.9Java2D Shapes Class Arc2D.Double –Similar to normal arcs, except has another argument at end Arc2D.PIE - close arc with two lines Arc2D.CHORD - draws line from endpoints of arc Arc2D.OPEN - arc not closed Class BasicStroke –Can be used to create customized dashed lines, set how lines end and join

28.9Java2D Shapes Class GeneralPath –A general path is a shape made from lines and curves –Method moveTo Specifies first point in a general path –Method lineTo Draws a line to next point in general path –Method closePath Draws line from last point to point specified in last call to moveTo Other methods –rotate( radians ) - rotate next shape around origin –translate(x, y) - translates origin to x, y

Outline Shapes.java (1 of 4)

Outline Shapes.java (2 of 4)

Outline Shapes.java (3 of 4)

Outline Shapes.java (4 of 4)

Outline Shapes2.java (1 of 3)

Outline Shapes2.java (2 of 3)

Outline Shapes2.java (3 of 3) Program Output