Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.

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

Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
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.
Chapter 5 Programming Graphics. Chapter Goals To be able to write simple applications To display graphical shapes such as lines and ellipses To use colors.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
1 L36 Graphics and Java 2D™ (1). 2 OBJECTIVES  To understand graphics contexts and graphics objects.  To understand and be able to manipulate colors.
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.
1 Introduction to Applets Overview l What is an Applet? l Steps for creating an applet l What is HTML? l Basic HTML tags l Drawing Simple Graphical shapes.
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.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
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.
Java Concepts Chapter 2 – Graphical Applications Mr. Smith AP Computer Science A.
CS324e - Elements of Graphics and Visualization Java2D 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.
1 Interface Types & Polymorphism & introduction to graphics programming in Java.
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.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 5 - Graphics.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
COP 4331 – OOD&P Lecture 6. Review Midterm Review Complete sample application –See SwingColorTest.java.
Chapter 15 Graphics and Java 2D™ Java How to Program, 8/e (C) 2010 Pearson Education, Inc. All rights reserved.
Graphics in Java hussein suleman uct cs
© 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.
Lab 6: Shapes & Picture Extended Ellipse & Rectangle Classes Stand-Alone GUI Applications.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
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.
Drawing and Filling Geometric Shapes. Java comes with more than just points and lines. Within the Graphics2D class, there are also methods for drawing.
Chapter 7 Graphics. © Daly and Wrigley Objectives Use Graphic Components: ▫ Strings ▫ Lines ▫ Rectangles ▫ Ovals ▫ Arcs Change the color and font of elements.
J McQuillan SE204: 2004/2005: Lecture 4slide 1 The Graphics Class Used when we need to draw to the screen Two graphics classes –Graphics –Graphics2D.
Graphics Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
(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.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
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.
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.
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.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
C Sc 335 Object-Oriented Programming and Design Rick Mercer
Projects: not limited to spec Error checking File filters Create multiple file formats Polygons Filled shapes Etc.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 7 Event-Driven Programming and Basic GUI Objects.
Eleventh Graphics in Java.
12 Graphics and Java 2D™.
Chapter 8 Graphics.
Java Graphics CS 2511.
JAVA 2 Design and programming of GUI
Chapter 10 Graphics.
Chapter 8 Graphics.
Handout-14 Applets and Graphics
Lecture 4: Standard Java Graphics
Presentation transcript:

Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics

Introduction to Java Chapter 8 - Introduction to Java Graphics2 Java Graphics Systems The Java SDK contains to different graphics systems –The Abstract Windowing Toolkit (AWT), which was the original Java graphics system –The Swing package, which is a newer, more flexible graphics system Only the Swing graphics system is taught in this text

Introduction to Java Chapter 8 - Introduction to Java Graphics3 Components and Containers The two principal types of graphics objects are Containers and Components A Component is visual object containing text or graphics –Here, we will work with a completely blank component, known as a “Canvas” A Container is a graphical object that can hold components or other containers –The principal container is a Frame. It is a part of the computer screen surrounded by borders and title bars.

Introduction to Java Chapter 8 - Introduction to Java Graphics4 Displaying Java Graphics To display Java graphics: 1.Create the component or components to display 2.Create a frame to hold the component(s), and place the component(s) into the frame(s). 3.Create a “listener” object to detect and respond to mouse clicks, and assign the listener to the frame. The components in this chapter will be of class JCanvas, and the containers will be of class JFrame

Introduction to Java Chapter 8 - Introduction to Java Graphics5 Displaying Java Graphics (2) Required packages Create component Create “Listener” Create frame Add listener and com- ponent to frame

Introduction to Java Chapter 8 - Introduction to Java Graphics6 Listeners A “Listener” class listens for mouse clicks or keyboard input on a container or component, and responds when it occurs –In this chapter, we will use a “Window” listener to detect mouse clicks and to shut down the program Trap mouse clicks in the “Close Window” box, and exit when one occurs

Introduction to Java Chapter 8 - Introduction to Java Graphics7 Displaying Graphics on a Component The paintComponent method is used to draw graphics on a component. –The calling sequence is: paintComponent( Graphics g ) –The Graphics object must be immediately cast to a java.awt.Graphics2D object before it can be used with Swing graphics –Once this is done, all of the classes in java.awt.geom can be used to draw graphics on the component

Introduction to Java Chapter 8 - Introduction to Java Graphics8 Example: Drawing a Line Create Line2D object Draw line represented by object

Introduction to Java Chapter 8 - Introduction to Java Graphics9 The Graphics Coordinate System Java uses a graphics coor- dinate system with the origin (0,0) in the upper left-hand corner –x axis is positive to the right –y axis is positive down By default, the units of measure are pixels –There are 72 pixels / inch Unit of measure can be changed y-axisx-axis origin

Introduction to Java Chapter 8 - Introduction to Java Graphics10 The Line2D Classes There are two concrete classes for creating lines: Line2D.Float and Line2D.Double. The only difference between them is the units of the calling parameters. Constructors: Line2D.Double( double x1, double y1, double x2, double y2 ) Line2D.Float( float x1, float y1, float x2, float y2 ) These classes create a line from (x 1,y 1 ) to (x 2,y 2 )

Introduction to Java Chapter 8 - Introduction to Java Graphics11 Controlling Object Color The color of a graphics object is controlled by the Graphics2D method setColor. The color may be any object of class java.awt. Color, including the following pre-defined values:

Introduction to Java Chapter 8 - Introduction to Java Graphics12 Controlling Line Width and Style Line width and style is controlled with a BasicStroke object Constructors have the form: BasicStroke(float width); BasicStroke(float width, int cap, int join, float miterlimit, float[] dash, float dash_phase); Can control line width, line cap style, line join style, and dashing pattern

Introduction to Java Chapter 8 - Introduction to Java Graphics13 Example: Setting Color and Stroke Set color Set stroke Draw line Define stroke

Introduction to Java Chapter 8 - Introduction to Java Graphics14 The Rectangle2D Classes There are two classes for creating rectangles: Rectangle2D.Float and Rectangle2D.Double. The only difference between them is the units of the calling parameters. Constructors: Rectangle2D.Double( double x, double y, double w, double h ) Rectangle2D.Float( float x, float y, float w, float h ) These classes create a rectangle with origin (x,y), with width w and height h

Introduction to Java Chapter 8 - Introduction to Java Graphics15 Example: Creating a Rectangle

Introduction to Java Chapter 8 - Introduction to Java Graphics16 The Ellipse2D Classes There are two classes for creating circles and ellipses: Ellipse2D.Float and Ellipse2D. Double. The only difference between them is the units of the calling parameters. Constructors: Ellipse2D.Double( double x, double y, double w, double h); Ellipse2D.Float( float x, float y, float w, float h); These classes create the ellipse that fits in a rectangular box with origin (x,y), with width w and height h

Introduction to Java Chapter 8 - Introduction to Java Graphics17 Example: Creating an Ellipse

Introduction to Java Chapter 8 - Introduction to Java Graphics18 The Arc2D Classes There are two classes for creating arcs: Arc2D.Float and Arc2D.Double. Constructors: Arc2D.Double( double x, double y, double w, double h, double start, double extent, int type ); Arc2D.Float( float x, float y, float w, float h, float start, float extent, int type ); –These classes create an arc that fits in a rectangular box with origin (x,y), with width w and height h. The arc starts at start radians and extends for extent radians. –The type of arc is Arc2D.OPEN, Arc2D.CHORD, or Arc2D.PIE

Introduction to Java Chapter 8 - Introduction to Java Graphics19 Example: Creating Arcs

Introduction to Java Chapter 8 - Introduction to Java Graphics20 Displaying Text Text is displayed using the Graphics2D method drawString. Forms: drawString(String s, int x, int y); drawString(String s, float x, float y); These methods write String s on the component. The point (x,y) specifies the lower-left hand corner of the text box within the component. –Note that this differs from the convention for other 2D graphics objects, where (x,y) is the upper-left hand corner!

Introduction to Java Chapter 8 - Introduction to Java Graphics21 Example: Writing Text Strings This corner is (20,40)

Introduction to Java Chapter 8 - Introduction to Java Graphics22 Setting Fonts Fonts are created with the java.awt.Font class Constructor: Font( String s, int style, int size ) –s is the name for the font to use. –style is the style ( Font.PLAIN, Font.BOLD, Font.ITALIC, or a combination) –size is the font size in points Any font on the system may be used, but certain fonts are guaranteed to be present on any system

Introduction to Java Chapter 8 - Introduction to Java Graphics23 Standard Font Names The following standard fonts are present on any Java implementation:

Introduction to Java Chapter 8 - Introduction to Java Graphics24 Example: Defining Fonts

Introduction to Java Chapter 8 - Introduction to Java Graphics25 Getting Information About Fonts Class java.awt.FontMetrics can be used to get information about a font Constructor: FontMetrics fm = new FontMetrics( Font f ); FontMetrics fm = g2.getFontMetrics(); Methods:

Introduction to Java Chapter 8 - Introduction to Java Graphics26 Example: Getting Font Information Result: Code: