Java Graphics CS 2511.

Slides:



Advertisements
Similar presentations
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.
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 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.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
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 Programming. In this class, we will cover: The difference between AWT and Swing Creating a frame Frame positioning Displaying information in.
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.
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.
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.
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.
Web Design & Development Lecture 18. Java Graphics.
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.
Introducing Graphics There are generally two types of graphics facilities in Java –Drawing –GUIs We concentrate on drawing here We will draw on a Graphics.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
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.
Advanced User Interfaces with Java SD’98 - Session 3206 Ted Faison Faison Computing Inc.
Java Graphics Swing Graphics
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
Graphics and Java 2D. 2 Introduction Java’s graphics capabilities –Drawing 2D shapes –Controlling colors –Controlling fonts Java 2D API –More sophisticated.
Object-Oriented Programming (Java), Unit 19 Kirk Scott 1.
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.
-Sai Phalgun Tatavarthy. Outline What is a “Graphing Calculator”? Graphics, Graphics2D classes Methods of Graphics object Custom Painting The paintComponent()
Java Graphics Stuart Hansen 11/6/03. What’s Wrong with OpenGL Graphics – not GUI –No real support of text boxes, buttons, etc. Procedural - not OOP –No.
Creating Your Own Widgets CompSci 230 S Software Construction.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
(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.
1 GUIs, Layout, Drawing Rick Mercer. 2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces (GUIs)
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,
Java Dynamic Graphics.
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
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.
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.
Java Graphics Chris North cs3724: HCI. Presentations peter hou Vote: UI Hall of Fame/Shame?
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.
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.
GUI Programming in Java Hao Jiang Boston College April, 2009.
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Eleventh Graphics in Java.
12 Graphics and Java 2D™.
Creating Your Own Widgets
Java Graphics.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
JAVA 2 Design and programming of GUI
Java Graphics The basic rendering mechanism is the drawing system that controls when and how programs can draw on a graphics component. When a component.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Chapter 49 Java 2D.
Lecture 4: Standard Java Graphics
Presentation transcript:

Java Graphics CS 2511

Swing Graphics Empty Swing containers have no visual appearance except for a background color Every JComponent must have a paintComponent method that is called when the component is first made visible or needs to be redrawn for some reason The JPanel component is a lightweight container, making it suitable as a drawing area A common way to do graphics is to extend the JPanel class and override the paintComponent method

The paintComponent Method Called by the JVM when this component needs to be redrawn A single argument, the component's graphics context (class: Graphics), is passed when paintComponent is called A Graphics object contains: the component on which it draws the current color and font location origin clipping information and more

Graphics vs. Graphics2D The Graphics class has limitations: Cannot use real number coordinates Cannot draw dotted, dashed, or variable-width lines Cannot easily draw complex curves or fill complex shapes Cannot use textures or gradient colors to fill shapes The newer Graphics2D class extends Graphics and provides these capabilities All GUI components use a Graphics2D object but paintComponent passes a Graphics object for backward compatibility

General Approach public class MyPanel extends JPanel { // instance variables public MyPanel() { // public constructor } // public methods // private helper methods public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; // drawing messages sent to g2d ...

The paintComponent Method super.paintComponent(g) is called first to ensure that painting responsibilities defined in JPanel are carried out You should not call paintComponent directly; it is called by the JVM when it needs to You can indirectly call paintComponent on a component by using component.repaint()

Some Basic Graphics Methods void setColor(Color color) void setFont(Font font) void drawString(String text, int x, int y) (x,y)is the coordinate of the lower left corner of the drawn string's leftmost character

Graphics Example import javax.swing.*; import java.awt.*; public class GraphicsPanel extends JPanel { public GraphicsPanel() { setPreferredSize(new Dimension(200,200)); setBackground(Color.magenta); // panel color } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D)g; g2D.setColor(Color.blue); // drawing color g2D.setFont(new Font("Helvetica", Font.BOLD, 24)); g2D.drawString("Hello World", 25, 25);

Graphics Example (cont'd) import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MainFrame extends JFrame { public MainFrame() { setSize(new Dimension(500,300)); setLocation(100,100); addWindowListener(new WindowAdapter () { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); getContentPane().setLayout( new FlowLayout(FlowLayout.CENTER)); GraphicsPanel gp = new GraphicsPanel(); getContentPane().add(gp); setVisible(true); public static void main(String[] args) { new MainFrame();

Display

Notes On The Example GraphicsPanel extends JPanel so that the paintComponent method can be overridden If you forget to call super's paintComponent method, you can get pixels from another desktop frame as background garbage The background color is associated with the panel; the paint color with the Graphics2D object The MainFrame class extends JFrame and an instance of it is created in the main method

Drawing Shapes You can draw any object that implements the java.awt.Shape interface. Example: suppose g2D is a Graphics2D object: Shape s = ...; g2D.draw(s); The Java library supplies a number of classes that implement the Shape interface type.

Inheritance Hierarchy of Geometric Shape Classes

Line Shapes java.awt.geom.Line2D is an abstract class with two concrete subclasses that are also inner classes: Line2D.Double Line2D.Float A Line2D object represents a line segment in (x,y) coordinate space. To create a line segment, first create its endpoints using the java.awt.geom.Point2D class

Line Example import java.awt.geom.*; ... public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D)g; Point2D.Double corner1 = new Point2D.Double(100, 50); Point2D.Double corner2 = new Point2D.Double(50, 150); Point2D.Double corner3 = new Point2D.Double(150, 150); Line2D.Double side1 = new Line2D.Double(corner1, corner2); Line2D.Double side2 = new Line2D.Double(corner2, corner3); Line2D.Double side3 = new Line2D.Double(corner3, corner1); g2D.draw(side1); g2D.draw(side2); g2D.draw(side3); }

An Easier Way: Polygons java.awt.Polygon implements the Shape interface. Specify the x and y coordinates of a closed polygon's vertices with the following constructor: Polygon(int[] xpoints, int[] ypoints, int npoints) import java.awt.*; ... public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D)g; Polygon triangle = new Polygon(new int[] {100, 50, 150}, new int[] {50, 150, 150}, 3); g2D.draw(triangle); // produces same triangles as before }

Rectangular Shapes Abstract subclasses of RectangularShape: Rectangle2D, RoundRectangle2D Ellipse2D, Arc2D Each has concrete .Double and .Float subclasses Each constructor requires x,y coordinate of upper left corner of bounding rectangle, and also the rectangle's width and height Use draw to draw an outline of the shape in the current color. Use fill to fill the shape with the current color.

Additional Parameters for Rounded Rectangles arcWidth (x,y) arcHeight height width

Example public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D)g; Rectangle2D.Double rect1 = new Rectangle2D.Double(25,25,100,100); g2D.setColor(Color.black); g2D.draw(rect1); RoundRectangle2D.Double rect2 = new RoundRectangle2D.Double(50,50,100,100,80,30); g2D.setColor(Color.green); g2D.fill(rect2); Ellipse2D.Double rect3 = new Ellipse2D.Double(75,75,100,80); g2D.setColor(Color.blue); g2D.fill(rect3); }

New Display

Additional Parameters for Arcs width -270 90 (x,y) ±180 height 270 -90 angularExtent startAngle Closure type for the arc: OPEN, CHORD, or PIE

Arc Example public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D)g; Arc2D.Double arc = new Arc2D.Double(25,25,150,100,0,120,Arc2D.PIE); g2D.setColor(Color.black); g2D.fill(arc); arc = new Arc2D.Double(25,25,150,100,120,120,Arc2D.PIE); g2D.setColor(Color.green); arc = new Arc2D.Double(25,25,150,100,240,120,Arc2D.PIE); g2D.setColor(Color.orange); }

Arc Display

Arc Example Modified public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2D = (Graphics2D)g; Arc2D.Double arc = new Arc2D.Double(25,25,150,100,0,120,Arc2D.CHORD); g2D.setColor(Color.black); g2D.fill(arc); arc = new Arc2D.Double(25,25,150,100,120,120,Arc2D.CHORD); g2D.setColor(Color.green); new Arc2D.Double(25,25,150,100,240,120,Arc2D.CHORD); g2D.setColor(Color.orange); }

New Arc Display

More Features Other things you can do with Shapes: Check for containment of points and rectangles Get bounding rectangle Check for intersection with another bounding rectangle Other things you can do with 2D graphics contexts: Change stroke properties Draw images Perform rotations, scalings, and translations