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?

Slides:



Advertisements
Similar presentations
Chapter 13 Graphics.
Advertisements

Custom Painting Gestione della Grafica customizzata Vedi anche:
Applets and Graphics.
Chapter 15 Graphics. To paint, you need to specify where to paint. Each component has its own coordinate system with the origin (0, 0) at the upper-left.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
Web Design & Development Lecture 19. Java Graphics 2.
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.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
AWT Components. 2 Using AWT Components 4 Component –Canvas –Scrollbar –Button –Checkbox –Label –List –Choice –TextComponent –TextArea –TextField 4 Component.
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 Programming. Introduction GOAL: Build the Indexer Client Event-driven vs. Sequential programs Terminology – Top-level windows are called “frame.
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.
Zhang & Liang, Computer Graphics Using Java 2D and 3D (c) 2007 Pearson Education, Inc. All rights reserved. 1 Chapter 2 2D Graphics: Basics.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
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 simple applications To display graphical shapes such as lines and ellipses To use colors.
Multithreading : animation. slide 5.2 Animation Animation shows different objects moving or changing as time progresses. Thread programming is useful.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
Graphical User Interface Bonus slides Interaction Between Components & Drawing.
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.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 14 Graphics.
1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details Graphics Objects.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 13 Graphics.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
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.
©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.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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.
1 Features of Java CS 3331 Fall Outline  Abstract class  Interface  Application --- animation applets.
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.
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.
Chapter 6: Graphics Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 6 Graphics.
Chapter 6: Graphics Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 6 Graphics.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 Object-Oriented Languages.
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.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
(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.
Features of Java CS 3331 Sections and 5.5.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
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)
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.
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.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
12 Graphics and Java 2D™.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
JAVA 2 Design and programming of GUI
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
Chapter 13 Graphics.
Chapter 13 Graphics.
Presentation transcript:

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?

(0, 0) (x, y) x y x-axis y-axis Java Coordinate System (0, 0) Conventional Coordinate System

 A rectangular area  Part of a user interface  Called a frame  Contains title bar

 Cannot draw directly on JFrame object  This is a container  Contains other objects  Create a panel upon which to draw

 Drawing Strings  Drawing Lines  Drawing Rectangles  Drawing Ovals  Drawing Arcs  Drawing Polygons

 paint() method  Write own method to override default  Method header  public void paint(Graphics g)  Graphics object  Preconfigured with the appropriate state for drawing on the component  repaint() method  Use when window needs to be updated  Calls paint() method  Creates Graphics object

 drawString() method  Draw String in JFrame window  Requires three arguments  String  x-axis coordinate  y-axis coordinate  Member of Graphics class

 setFont() method  Requires Font object  Instruct Graphics object to use a font  somegraphicsobject.setFont(someFont);  setColor() method  Designate Graphics color  Use 13 Color class constants as arguments  Create any Color object  Color someColor = new Color(r, g, b);  Values range from 0 to 255

 setColor() method  Designate Graphics color  Use 13 Color class constants as arguments  Create any Color object  Color someColor = new Color(r, g, b);  Values range from 0 to 255

drawString(String s, int x, int y); (0,0) (getWidth(),0) (0, getHeight())(getWidth(),getHeight()) (x, y) → String goes here

 drawLine() method  Draw straight line between any two points  Takes four arguments  x- and y-coordinates of line’s starting point  x- and y-coordinates of the line’s ending point

drawLine(int x1, int y1, int x2, int y2); (0,0) (getWidth(),0) (0, getHeight())(getWidth(),getHeight()) (x1, y1) (x2, y2)

 drawRect() method  Draw outline of rectangle  fillRect() method  Draw solid or filled rectangle  Both require four arguments  x- and y-coordinates of upper-left corner of rectangle  Width and height of rectangle

drawRect(int x, int y, int w, int h); fillRect(int x, int y, int w, int h); (x,y) h w h w

 clearRect() method  Draws rectangle  Requires four arguments  x- and y-coordinates of upper-left corner of rectangle  Width and height of rectangle  Appears empty or “clear”  drawRoundRect() method  Create rectangles with rounded corners  Requires six arguments

drawRoundRect(int x, int y, int w, int h, int aw, int ah); fillRoundRect(int x, int y, int w, int h, int aw, int ah); (x,y) h w aw/2 ah/2

 drawOval() and fillOval() methods  Draw ovals using the same four arguments that rectangles use drawOval(int x, int y, int w, int h); fillOval(int x, int y, int w, int h); (x,y) h w

 drawArc() method arguments  x-coordinate of upper-left corner of imaginary rectangle that represents bounds of imaginary circle that contains arc  y-coordinate of same point  Width of imaginary rectangle that represents bounds of imaginary circle that contains arc  Height of same imaginary rectangle  Beginning arc position  Arc angle

 fillArc() method  Create solid arc (x,y) h w drawArc(int x, int y, int w, int h, int angle1, int angle2); fillArc(int x, int y, int w, int h, int angle1, int angle2);

 draw3DRect() method  Minor variation on drawRect() method  Draw rectangle that appears to have “shadowing” on two edges  Contains Boolean value argument  true if rectangle darker on right and bottom  false if rectangle darker on left and top  fill3DRect() method  Create filled three-dimensional rectangles

 drawPolygon() method  Draw complex shapes  Requires three arguments  Integer array holds series of x-coordinate positions  Second array holds series of corresponding y- coordinate positions  Number of pairs of points to connect

 fillPolygon() method  Draw solid shape  If beginning and ending points not identical  Two endpoints connected by straight line before polygon filled with color  addPoint() method  Add points to polygon indefinitely

int[] x = {40, 70, 60, 45, 20}; int[] y = {20, 40, 80, 45, 60}; g.drawPolygon(x, y, x.length); (x[0], y[0]) (x[1], y[1]) (x[2], y[2]) (x[3], y[3]) (x[4], y[4])

import java.awt.Graphics; import javax.swing.JPanel; public class DrawingPanel extends JPanel { public void paintComponent (Graphics g) { g.drawRect (50, 50, 20, 20); // square g.drawRect (100, 50, 40, 20); // rectangle g.drawOval (200, 50, 20, 20); // circle g.drawOval (250, 50, 40, 20); // oval g.drawString ("Square", 50, 90); g.drawString ("Rectangle", 100, 90); g.drawString ("Circle", 200, 90); g.drawString ("Oval", 250, 90); // continued on next slide

g.fillRect (50, 100, 20, 20); // square g.fillRect (100, 100, 40, 20); // rectangle g.fillOval (200, 100, 20, 20); // circle g.fillOval (250, 100, 40, 20); // oval g.drawLine (50, 150, 300, 150); // line g.drawArc (50, 150, 250, 100, 0, 180); // arc g.fillArc (100, 175, 200, 75, 90, 45); // arc } // end paintComponent } // end DrawingPanel

import javax.swing.JFrame; public class DrawingSamples { public static void main (String [ ] args) { JFrame aWindow = new JFrame (); aWindow.setSize (350, 300); // width x height aWindow.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); aWindow.setTitle ("Samples of shapes we can draw"); DrawingPanel panel = new DrawingPanel (); aWindow.add (panel); aWindow.setVisible (true); }

 You can display a string at any location in a panel.  Can you display it centered?  To do so, you need to use the FontMetrics class to measure the exact width and height of the string for a particular font.  A FontMetrics can measure the following attributes:

 Leading  Amount of space between baselines  Ascent  Height of uppercase character from baseline to top of character  Descent  Measures part of characters that “hang below” baseline  Height of font  Sum of leading, ascent, and descent

 getFontMetrics() method  Discover font’s height  Returns FontMetrics object  Use FontMetrics class methods with object to return Font ’s statistics  public int getLeading()  public int getAscent()  public int getDescent()  public int getHeight()

 stringWidth() method  Returns integer width of a String  Requires name of String  Member of FontMetrics class getHeight() getLeading() getAscent() getDescent()