©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 5 - 1 Chapter 5 Java Graphics Applets.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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?
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Applets Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
Java Graphics Section 1 - Multi-File Graphics Programs Section 2 - The Coordinate System and Graphics Context g Section 3 - The Java Drawing and Painting.
© 2004 Pearson Addison-Wesley. All rights reserved2-1 Introduction to Graphics The last few sections of each chapter of the textbook focus on graphics.
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 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
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.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
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 Java Applets What is an Applet? How do you create.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
A Simple Applet.
Chapter 5 Graphics. Topics Applets Classes used for graphics –Graphics –Point –Dimension –Color.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
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. Graphics Features in Java Topics to be covered Topics to be covered Graphics Basics Graphics Basics Coordinate System Coordinate System Graphics,
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
©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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Standard Graphics in Java,
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 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.
Chapter 15Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 15 l Basic Figures l Colors l Fonts and Other Text Details.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
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.
Canvas and Graphics CS 21a. 9/26/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L17: Canvas.
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.
1 A Simple Applet. 2 Applets and applications An application is an “ordinary” program Examples: Notepad, MS Word, Firefox, Halo, etc. An applet is a Java.
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Graphics & Applets CSC 1051 – Data Structures and Algorithms I
(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.
Creating Graphics in Java CSE301 University of Sunderland Harry R Erwin, PhD.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
Classes Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
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.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
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.
Graphics JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Intro to Graphics from Chapter 2 of Java Software Solutions
Graphics Applets By Mr. Dave Clausen
12 Graphics and Java 2D™.
Chapter 02 Data and Expressions.
Chapter 8 Graphics.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Object Oriented Programming
CSC 1051 – Data Structures and Algorithms I
Graphics Applets By Mr. Dave Clausen
Chapter 10 Graphics.
Graphics -- Introduction
Chapter 8 Graphics.
Outline Character Strings Variables and Assignment
Presentation transcript:

©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 Graphics Classes We introduce four standard classes related to drawing geometric shapes on a window: –java.awt.Graphics –java.awt.Color –java.awt.Point –java.awt.Dimension

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Graphics Class Frames and Applets have a graphics context associated with them. A Graphics object defines a graphics context on which we can draw shapes and text. The Graphics class is in the java.awt package. The state of a Graphics object includes such properties as height, width, foreground and background color and font. Positions within the Graphics context are measured from the upper left corner and they have units of pixels.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Graphics Class Accessor methods are provided for getting height, width, color, font, … Mutator methods are also provided for these properties. The Graphics class has several methods for drawing shapes There is also a method for displaying text. You can also display images.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Graphics Methods

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The Effect of drawRect

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Drawing a Line X Y g.drawLine (10, 20, 150, 45); g.drawLine (150, 45, 10, 20); or

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Drawing an Oval X Y g.drawOval (175, 20, 50, 80); bounding rectangle

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Drawing an Arc An arc is a part of an oval To draw you need to specify –the bounding rectangle for the oval –the starting angle (measured from the x-axis in degrees –the angle the arc subtends

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Drawing an Arc void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) g.drawArc (75, 20, 50, 80, 90, 90);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Drawing a String The drawString method can be used to display text in a Graphics context. void drawString( String text, int x, int y; *The position of the text relative to x and y is shown below

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Rectangles with a 3D Look Another method in the Graphics class allows you to display rectangles that look 3D fill3DRect( int x, int y, int width, int height, boolean raised) The boolean variable will be true if you want the rectangle to look raised and false to make it look lowered.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Polygons A polygon is a closed figure which is defined by a sequence of points called vertexes. The awt package has a Polygon class Create an empty Polygon with Polygon poly = new Polygon() Add another vertex using poly.addPoint( x, y); Use the Graphics drawPolygon and fillPolygon method to display the Polygon g.drawPolygon( poly); g.fillPolygon( poly)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Color Class The java.awt.Color class allows us to designate the color of an object. The RGB scheme combines three values ranging from 0 to 255 for red, green, and blue. Color pinkColor; pinkColor = new Color(255,175,175)

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Color Class There are public class constants defined in the Color class for common colors: –Color.black –Color.blue –Color.green –Color.magenta –Color.orange

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The Color Class Every drawing surface has a background color g.setBackground(Color.white); Every graphics context has a current foreground color –The most recently set foreground color is what is used for drawing g.setColor(Color.blue); g.drawRect(50, 50, 100, 30);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Using Fonts Every Graphics object has a Font associated with it. You can find out what the Font is with Font getFont( void); You can change the Font with void setFont( Font); The Font is part of the state of the graphics object – a String will be drawn with the current Font.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The Font Class A Font has three components –Font name “Monospaced” “Serif” “SansSerif” –Font Style (add BOLD and ITALIC to get both) Font.BOLD Font.ITALIC Font.PLAIN Font Size - integer number of points

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Creating a Font Constructor takes three parameters Font( String fontFace, int fontStyle, int size) Examples Font f1 = new Font( “SansSerif”, Font.BOLD, 24); Font f1 = new Font( “SansSerif”, Font.BOLD + Font.ITALIC, 16);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Font Measurements Sometimes, you want to know how big a String will be when you draw it Create a FontMetrics object for your Graphics context FontMetrics fm = g.getFontMetrics( f1) Use the stringWidth method int width = fm.getWidth( aString);

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Point class A java.awt.Point object designates a point in two- dimensional space. The Point class has public instance variables To assign the position (10, 20) to a Point: Point pt = new Point(); pt.x = 10; pt.y = 20;

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Dimension Class The java.awt.Dimension class can be used to create bounding rectangles that surround shapes. Dimension dim = new Dimension(); dim.width = 40; dim.height = 70; Bounding rectangles

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Applets A Java application is a stand-alone program with a main method (like the ones we've seen so far) that you run from the console. A Java applet is a program that is intended to transported over the Web and executed using a web browser –An applet also can be executed using the appletviewer tool of the Java Software Development Kit –An applet doesn't have a main method –Instead, there are several instance methods that serve specific purposes

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Applets The paint method, for instance, is executed automatically and is used to draw the applet’s contents The paint method accepts a parameter that is an object of the Graphics class

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Applets The class that defines an applet extends the Applet class An applet is embedded into an HTML file using a tag that references the bytecode file of the applet class The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter The HTML applet Tag My Applet <applet code="Einstein.class" width=350 height=175>