(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
Advertisements

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.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 21.
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.
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.
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.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
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.
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 16 : Swing VI Graphics King Fahd University of Petroleum & Minerals College of Computer.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: 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 Drawing Shapes and Text With Applets. 2 Drawing in the paint method import java.awt.*;// access the Graphics object import javax.swing.*;// access to.
Graphics and Multimedia. Introduction The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other.
Web Design & Development Lecture 18. Java Graphics.
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.
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.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 20.1 Test-Driving the Screen Saver Application.
CSE 219 Computer Science III Images. HW1 Has been posted on Blackboard Making a Game of Life with limited options.
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.
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.
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.
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Lecture 8.3 The Use of JComponent. © 2006 Pearson Addison-Wesley. All rights reserved More About the Standard Drawing Classes java.awt.Container.
Introduction to Java Simple Graphics. Objects and Methods Recall that a method is an action which can be performed by an object. –The action takes place.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
Swing Hierarchy Swing classes derived from JComponent will be lightweight, written entirely in Java. The top-level Swing windows are heavyweight. They.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 21.1 Test-Driving the Painter Application.
Merete S COLLEGEFACULTY OF ENGINEERING & SCIENCE Graphics ikt403 – Object-Oriented Software Development.
Building Java Programs Supplement 3G Graphics Copyright (c) Pearson All rights reserved.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 27.1 Test-Driving the Drawing Shapes Application.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
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.
Intro to Graphics from Chapter 2 of Java Software Solutions
Chapter 8 Graphics.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
Chapter 8 Graphics.
Presentation transcript:

(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto the screen.  Class JPanel (from package javax.swing ) provides an area on which we can draw.

Painting  When a GUI needs to change its visual appearance it performs a paint operation  Swing components generally repaint themselves as needed  Painting code executes on the event- dispatching thread ◦ If painting takes a long time, no events will be handled during that time

Example import javax.swing.*; import java.awt.*; public class Painting extends JPanel { public Painting() {} public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor( Color.yellow ); g.fillOval( 10,10,50,50 ); g.setColor( Color.black ); g.drawOval( 10,10,50,50 ); } public static void main( String args[] ) { JFrame win = new JFrame( "Painting" ); win.setSize(100, 100); win.getContentPane().add( new Painting() ); win.setvisible(true); }}

The Graphics Object  The Graphics object both a context for painting and methods for performing the painting.  The graphics context consists of state such as the current painting color, the current font, and the current painting area ◦ The color and font are initialized to the foreground color and font of the component just before the invocation of paintComponent

The Coordinate System  Each component has its own integer coordinate system ◦ Ranging from (0, 0) to (width - 1, height - 1) ◦ Each unit represents the size of one pixel

Color  Combinations of Red, Green, Blue  Each [0, 255]  Java: new Color(255, 150, 0) Hokie Orange

Font  Color and font:  g2.setColor( new Color(r,g,b) );  g2.setFont( new Font(…) );  new font(“Serif”, Font.BOLD, Font.Italic, 18);

Re-Paint  Screen is like a painter’s canvas  All windows paint on the same surface!  Windows don’t “remember” whats under them  Need to re-paint when portions are newly exposed  Receive Repaint events  Open, resize, bring to front  When other windows in front move, resize, close

(C) 2010 Pearson Education, Inc. All rights reserved.

 The keyword extends creates a so-called inheritance relationship.  The class from which DrawPanel inherits, JPanel, appears to the right of keyword extends.  In this inheritance relationship, JPanel is called the superclass and DrawPanel is called the subclass.

(C) 2010 Pearson Education, Inc. All rights reserved.  JPanel has a paintComponent method, which the system calls every time it needs to display the JPanel.  The first statement in every paintComponent method you create should always be super.paintComponent( g );  JPanel methods getWidth and getHeight return the JPanel ’s width and height, respectively.  Graphics method drawLine draws a line between two points represented by its four arguments. The first two are the x- and y-coordinates for one endpoint, and the last two arguments are the coordinates for the other endpoint.

(C) 2010 Pearson Education, Inc. All rights reserved.  To display the DrawPanel on the screen, place it in a window.  Create a window with an object of class JFrame.  JFrame method setDefaultCloseOperation with the argument JFrame.EXIT_ON_CLOSE indicates that the application should terminate when the user closes the window.  JFrame ’s add method attaches the DrawPanel (or any other GUI component) to a JFrame.  JFrame method setSize takes two parameters that represent the width and height of the JFrame, respectively.  JFrame method setVisible with the argument true displays the JFrame.  When a JFrame is displayed, the DrawPanel ’s paintComponent method is implicitly called

(C) 2010 Pearson Education, Inc. All rights reserved.  Graphics methods drawRect and drawOval  Method drawRect requires four arguments. The first two represent the x- and y-coordinates of the upper-left corner of the rectangle; the next two represent the rectangle’s width and height.  To draw an oval, method drawOval creates an imaginary rectangle called a bounding rectangle and places inside it an oval that touches the midpoints of all four sides.  Method drawOval requires the same four arguments as method drawRect. The arguments specify the position and size of the bounding rectangle for the oval.

(C) 2010 Pearson Education, Inc. All rights reserved.

 Colors displayed on computer screens are defined by their red, green, and blue components (called RGB values) that have integer values from 0 to 255.  The higher the value of a component color, the richer that shade will be in the final color.  Java uses class Color in package java.awt to represent colors using their RGB values.  Class Color contains 13 predefined static Color objects— BLACK, BLUE, CYAN, DARK_GRAY, GRAY, GREEN, LIGHT_GRAY, MAGENTA, ORANGE, PINK, RED, WHITE and YELLOW.

(C) 2010 Pearson Education, Inc. All rights reserved.  Class Color also contains a constructor of the form:  public Color( int r, int g, int b )  so you can create custom colors by specifying the red, green and blue component values.  Graphics methods fillRect and fillOval draw filled rectangles and ovals, respectively.  Graphics method setColor sets the current drawing color.

(C) 2010 Pearson Education, Inc. All rights reserved.

 Drawing arcs in Java is similar to drawing ovals—an arc is simply a section of an oval.  Graphics method fillArc draws a filled arc.  Method fillArc requires six parameters.  The first four represent the bounding rectangle in which the arc will be drawn.  The fifth parameter is the starting angle on the oval, and the sixth specifies the sweep, or the amount of arc to cover.  Starting angle and sweep are measured in degrees, with zero degrees pointing right.  A positive sweep draws the arc counterclockwise.  Method drawArc requires the same parameters as fillArc, but draws the edge of the arc rather than filling it.  Method setBackground changes the background color of a GUI component.

(C) 2010 Pearson Education, Inc. All rights reserved.

 Class Point (package java.awt ) represents an x-y coordinate.  We use objects of this class to store the coordinates of each mouse drag event.  Class Graphics is used to draw.  MouseEvent method getPoint obtains the Point where the event occurred.  Method repaint (inherited from Component ) indicates that a Component should be refreshed on the screen as soon as possible.

(C) 2010 Pearson Education, Inc. All rights reserved.

 Graphics method fillOval draws a solid oval.  Four parameters represent a rectangular area (called the bounding box) in which the oval is displayed.  The first two are the upper-left x-coordinate and the upper-left y- coordinate of the rectangular area.  The last two represent the rectangular area’s width and height.  Method fillOval draws the oval so it touches the middle of each side of the rectangular area.

(C) 2010 Pearson Education, Inc. All rights reserved.