Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.

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.
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?
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.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
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.
1 Graphics -- Introduction zThe use of graphics is common among modern software systems  Java has strong API support for graphics in the java.awt (abstract.
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.
©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.
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.
Copyright 2006 by Pearson Education 1 Building Java Programs Supplement 3G: Graphics.
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.
Chapter 6: Arrays & More GUI Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002, and used.
©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.
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.
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.
CS324e - Elements of Graphics and Visualization Java2D Graphics.
Graphics. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
1 Graphical User Components (II) Outline JTextArea Creating a Customized Subclass of JPanel JPanel Subclass that Handles Its Own Events Windows: Additional.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
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.
Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The.
Lecture 15: Intro to Graphics Yoni Fridman 7/25/01 7/25/01.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
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.
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.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Graphics & Applets CSC 1051 – Data Structures and Algorithms I
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Tkinter Canvas.
1 Building Java Programs Supplement 3G: Graphics These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold,
(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.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Graphics and Java2D Chapter Java Coordinate System Origin is in _____________ corner –Behind title bar of window X values increase to the ________.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Please open JCreator and follow these steps: 1)Configure  Options 2)JDK Tools 3)Choose Run Applet from drop-down 4)Click Default, then Edit 5)Click the.
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,
10/20/2005week71 Graphics, mouse and mouse motion events, KeyEvent Agenda Classes in AWT for graphics Example java programs –Graphics –Mouse events –Mouse.
Graphics Drawing Things With Java. Today's Topics Course Updates Java Graphics –Java 1.1 versus Java 1.2 (Java 2) Drawing Lines Drawing Shapes.
Intro to Graphics from Chapter 2 of Java Software Solutions
12 Graphics and Java 2D™.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
CSC 1051 – Data Structures and Algorithms I
JAVA 2 Design and programming of GUI
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
Chapter 6: Arrays and Vectors
Presentation transcript:

Java Software Solutions Lewis and Loftus Chapter 7 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphics -- Introduction The use of graphics is common among modern software systems Java has strong API support for graphics in the java.awt (abstract windowing toolkit) package Chapter 7 focuses on: –the coordinate system for Java graphics –the use of color –drawing shapes such as lines, ovals, rectangles, etc. –the use of fonts –basic animation techniques

Java Software Solutions Lewis and Loftus Chapter 7 2 Copyright 1997 by John Lewis and William Loftus. All rights reserved. The Graphics Class An object of the Graphics class represents a particular drawing surface It defines a graphics context in which drawn shapes will be rendered The Graphics class contains methods for drawing various shapes and controlling visual aspects like font and color An applet has a graphics context, which is automatically passed to the paint method when it is called

Java Software Solutions Lewis and Loftus Chapter 7 3 Copyright 1997 by John Lewis and William Loftus. All rights reserved. The Coordinate System A simple two-dimensional coordinate system exists for each graphics context (or drawing surface) Each point on the coordinate system represents a single pixel The top left corner of the area is coordinate A drawing surface has a particular width and height Anything drawn outside of that area will not be visible See Coordinates.java

Java Software Solutions Lewis and Loftus Chapter 7 4 Copyright 1997 by John Lewis and William Loftus. All rights reserved. The Coordinate System x y Y X

Java Software Solutions Lewis and Loftus Chapter 7 5 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Color The Color class is used to define and manage the color in which shapes are drawn Colors are defined by their RGB value, which defines the relative contribution of the primary colors red, green, and blue Each drawing surface has a foreground color and a background color The setColor method of the Graphics class defines the foreground color, and the setBackground method of the component (the applet) sets the background color

Java Software Solutions Lewis and Loftus Chapter 7 6 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Color The Color class contains several predefined colors, defined as public, static constants See Nature.java Many other colors can be defined using the constructor of the Color class Over 16 million colors can be defined, but humans cannot distinguish between many similar colors Furthermore, the hardware of most systems has limitations to the color options available

Java Software Solutions Lewis and Loftus Chapter 7 7 Copyright 1997 by John Lewis and William Loftus. All rights reserved. XOR Mode Drawing in normal mode causes shapes of the same color to blend together Drawing in XOR mode causes the overlapping portions of the shapes to be rendered in a contrasting color This effect can be used to "erase" a shape by redrawing it in the same color in the same spot while in XOR mode See XOR_Demo.java

Java Software Solutions Lewis and Loftus Chapter 7 8 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Drawing Shapes The Graphics class contains methods for drawing several specific shapes: lines, ovals, rectangles, arcs, polygons, and polylines Most shapes can be drawn filled or unfilled A line, drawn with the drawLine method, is always one pixel wide and cannot be filled

Java Software Solutions Lewis and Loftus Chapter 7 9 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Ovals An oval is defined by its bounding rectangle: The methods that draw an oval take four parameters, all integers: drawOval (x, y, width, height) fillOval (x, y, width, height) width height

Java Software Solutions Lewis and Loftus Chapter 7 10 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Ovals The first two parameters are the coordinate of the top-left corner of the bounding rectangle The third and fourth parameters specify the width and height of the bounding rectangle The drawOval method draws an unfilled oval and the fillOval method draws a filled (opaque) oval See Ovals.java and Rotating_Disk.java

Java Software Solutions Lewis and Loftus Chapter 7 11 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Rectangles Rectangles can be drawn –filled or unfilled –with squared or rounded corners –with a slight three-dimensional effect or not The primary parameters for all rectangle drawing methods define the upper left corner of the rectangle and its width and height The shape of the rounded corner of a rounded rectangle are defined by an arc width and height

Java Software Solutions Lewis and Loftus Chapter 7 12 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Rectangles A three dimensional rectangle is shown using a small highlight on two sides of the rectangle The highlight appears on the bottom and right or the top and left as specified by a boolean parameter to the 3D drawing methods See Rectangles.java, Rounded_Rectangles.java, and Three_D_Rectangles.java

Java Software Solutions Lewis and Loftus Chapter 7 13 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Arcs An arc is defined as a segment of an oval The first four parameters to the arc drawing methods define the bounding rectangle of the oval (top left corner, width, and height) The other two parameters define the start angle and the arc angle The start angle indicates where the arc begins and the arc angle determines how far the arc sweeps across its defining oval See Arc.java

Java Software Solutions Lewis and Loftus Chapter 7 14 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Arcs The start angle can be specified using both positive and negative values:

Java Software Solutions Lewis and Loftus Chapter 7 15 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Arcs An arc angle can also be positive or negative A positive arc angle sweeps counterclockwise, and a negative arc angle sweeps clockwise Therefore, the same arc can be specified using four different combinations of start and arc angles Arcs can also be filled or unfilled See Arcs.java

Java Software Solutions Lewis and Loftus Chapter 7 16 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Polygons A polygon is a multisided figure defined by a series of ordered points Line segments connecting the points form the polygon The points are defined by corresponding arrays of x and y coordinate values, and can already be incorporated into an object of the Polygon class Polygons are closed, forming a line segment from the last point back to the first See Polygons.java

Java Software Solutions Lewis and Loftus Chapter 7 17 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Polylines A polyline is similar to a polygon except that it is not closed That is, there is no line segment from the last point back to the first unless explicitly specified They are convenient for specifying certain kinds of complex shapes Polylines cannot be filled See Polylines.java

Java Software Solutions Lewis and Loftus Chapter 7 18 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Fonts A font defines the look of each character when it is printed or drawn The Font class provides methods for specifying fonts in a Java program Each computer system supports a specific set of fonts See Font_Lister.java The setFont method defines the current font for a program

Java Software Solutions Lewis and Loftus Chapter 7 19 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Fonts A font is defined using the Font class constructor and a combination of: –font name –font style: plain, bold, italic, or bold+italic –font size, in points Constants are defined in the Font class to specify the font style See Entropy.java

Java Software Solutions Lewis and Loftus Chapter 7 20 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Animations Simple animations can be accomplished by drawing a shape, then erasing it, then drawing it again in a slightly altered position Erasing can be accomplished through careful use of XOR mode Timing must be controlled so that the animation does not move too fast See Bouncing_Ball.java