1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.

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

Laboratory Study II October, Java Programming Assignment  Write a program to calculate and output the distance traveled by a car on a tank of.
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?
Graphics Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
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.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
GUI programming AWT(Abstract Windows Toolkit)-GUI components library when Java was introduced AWT was replaced by Swing components import javax.swing.*;
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.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
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™
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L01 (Chapter 13) Graphics.
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.
Graphics and Multimedia. Outline Introduction Graphics Contexts and Graphics Objects Color Control.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Swing Graphics ● Empty Swing containers have no visual appearance except for a background color ● Every JComponent must have a paintComponent method that.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Chapter 16: Graphics. Objectives Learn about the paint() and repaint() methods Use the drawString() method to draw String s using various fonts and colors.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
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.
Modernās Programmēšanas Tehnoloģijas (Advanced Programming Technologies) Edgars Celms, Mārtiņš Opmanis Latvijas Universitātes Matemātikas.
(C) 2010 Pearson Education, Inc. All rights reserved. Omer Boyaci.
Graphics and Multimedia Part #2
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
Nov 061 Size Control How is a component’s size determined during layout and during resize operations? Three factors determine component sizes: The component’s.
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.
 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 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.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
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.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
1 Chapter 3 2D Graphics: Rendering Details  Color spaces, paints stroke types  Affine transforms including translation, rotation, scaling, shearing,
(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.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
1 Graphics, Fonts and Color Chapter 9. 2 What is in this chapter: l Graphics class and coordinates l graphics primitives (lines,rectangles,ovals and arcs)
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Graphics in Java Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have any questions.
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.
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.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
12 Graphics and Java 2D™.
Chapter 8 Graphics.
Java Graphics.
Graphics and Multimedia
JAVA 2 Design and programming of GUI
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
12 Graphics and Java 2D™.
HSB Hue-Saturation-Brightness
Chapter 8 Graphics.
Presentation transcript:

1 L36 Graphics and Java 2D™ (1)

2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.  To understand and be able to manipulate fonts.

Introduction Java contains support for graphics that enable programmers to visually enhance applications Java contains many more sophisticated drawing capabilities as part of the Java 2D API Classes – Color – Font, FontMetrics – Graphics2D – Polygon – BasicStroke – GradientPaint, TexturePaint – Java 2D shape classes

4 Fig | Classes and interfaces used in this chapter from Java’s original graphics capabilities and from the Java 2D API. [Note: Class Object appears here because it is the superclass of the Java class hierarchy.]

Introduction Java coordinate system – Upper-left corner of a GUI component has the coordinates (0, 0) – Contains x-coordinate (horizontal coordinate) - horizontal distance moving right from the left of the screen – Contains y-coordinate (vertical coordinate) - vertical distance moving down from the top of the screen Coordinate units are measured in pixels. A pixel is a display monitor’s smallest unit of resolution.

6 Fig | Java coordinate system. Units are measured in pixels.

Graphics Contexts and Graphics Objects A Java graphics context enables drawing on the screen Class Graphics – Manages a graphics context and draws pixels on the screen – An abstract class – contributes to Java’s portability Method paintComponent – Used to draw graphics – Member of class JComponent, subclass of Component – Graphics object passed to paintComponent by the system when a lightweight Swing component needs to be repainted – If programmer needs to have paintComponent execute, a call is made to method repaint

Color Control Class Color declares methods and constants for manipulating colors in a Java program Every color is created from a red, a green and a blue component – RGB values

9 Fig | Color constants and their RGB values.

10 Fig | Color methods and color-related Graphics methods.

11 Outline ColorJPanel.java (1 of 2) Method paintComponent paints JPanel Set current drawing color with method setColor Draw filled rectangle using current color Draw text value of current colorSet current drawing color, specify float arguments to Color constructor Set current drawing color using Color constant

12 Outline ColorJPanel.java (2 of 2) Retrieving RGB values using methods getRed, getGreen and getBlue

13 Outline ShowColors.java

Color Control JColorChooser GUI component enables application users to select colors – Method showDialog creates a JColorChooser object, attaches it to a dialog box and displays the dialog – Modal dialog – Allows the user to select a color from a variety of color swatches – Tabs – Swatches, HSB and RGB

15 Outline ShowColors2JFrame.java (1 of 2) Import JColorChooser class

16 Outline ShowColors2JFrame.java (2 of 2) Display JColorChooser dialog Reference to parent componentTitle bar text Initial selected color Change background color of JPanel

17 Outline ShowColors2.java (1 of 2)

18 Outline ShowColors2.java (2 of 2) Select a color from one of the color swatches.

19 Fig | HSB and RGB tabs of the JColorChooser dialog. Sliders to select the red, green and blue color components

Font Control Class Font – Constructor takes three arguments—the font name, font style and font size Font name – any font currently supported by the system on which the program is running Font style – Font.PLAIN, Font.ITALIC or Font.BOLD. Font styles can be used in combination Font sizes – measured in points. A point is 1/72 of an inch. – Methods getName, getStyle and getSize retrieve information about Font object – Graphics methods getFont and setFont retrieve and set the current font, respectively

21 Fig | Font-related methods and constants. (Part 1 of 2)

22 Fig | Font-related methods and constants. (Part 2 of 2)

23 Outline FontJPanel.java (1 of 2) Creating Font objects Font nameFont style Font size

24 Outline FontJPanel.java (2 of 2) Combining styles Retrieve font name and size of Graphics object’s current Font

25 Outline Fonts.java

26 Font Metrics Font class methods – getFamily – returns name of font family to which the current font belongs – isPlain, isBold, isItalic – used to determine font style Font metrics – precise information about a font – Height – Descent – amount a character dips below the baseline – Ascent – amount a character rises above the baseline – Leading – the interline spacing – Class FontMetrics declares several methods for obtaining font metrics

27 Fig | Font metrics.

28 Fig | FontMetrics and Graphics methods for obtaining font metrics.

29 Outline MetricsJPanel.java (1 of 2) Retrieve FontMetrics object of current Font Retrieve font metric values

30 Outline MetricsJPanel.java (2 of 2)

31 Outline Metrics.java