Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font.

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.
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.
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 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.
Copyright 2008 by Pearson Education Building Java Programs Graphics Reading: Supplement 3G.
©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.
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.
2D Graphics in Java COMP53 Nov 14, Applets and Applications Java applications are stand-alone programs – start execution with main() – runs in JVM.
A Simple Applet --- Digital Clock import java.awt.*; import java.util.Calendar; public class DigitalColok extends java.applet.Applet implements Runnable.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
©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.
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.
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.
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.
1 Graphical objects We will draw graphics in Java using 3 kinds of objects: DrawingPanel : A window on the screen. Not part of Java; provided by the authors.
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.
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.
Java applet basics, loading & displaying images After this section, you should be able to : –Use the applet tag and applet parameters –Describe what a.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
© 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.
Copyright 2010 by Pearson Education Building Java Programs Graphics reading: Supplement 3G.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 14 Java Fundamentals 2D Graphics Wed. 10/11/00.
Chapter 6: Graphics Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 6 Graphics.
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.
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.
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 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.
(c)2006 E.S.Boese All Rights Reserved.1 Drawing Shapes and Text Chapter 2 – Lecture Slides.
Introductory Graphics Chapter Three. Computer Graphics §Full motion pictures (“Star Wars”) l animated films §Virtual reality §Games §Simulators (air craft.
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)
Introduction to Graphics. Graphical objects To draw pictures, we will use three classes of objects: –DrawingPanel : A window on the screen. –Graphics.
Copyright 2008 by Pearson Education Building Java Programs Graphics reading: Supplement 3G videos: Ch. 3G #1-2.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
The 4 Stages of Program Design  Cryptic Programming Stage  Unstructured, Spaghetti-Programming Stage  Structured Programming Stage  Object Oriented.
CS202 Java Object Oriented Programming GUI Programming – Color and Drawing Methods Chengyu Sun California State University, Los Angeles.
Intro to Graphics from Chapter 2 of Java Software Solutions
12 Graphics and Java 2D™.
Chapter 8 Graphics.
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Building Java Programs
Java Applets.
Building Java Programs
Building Java Programs
Chapter 10 Graphics.
4.14 GUI and Graphics Case Study: Creating Simple Drawings (Cont.)
Graphics -- Introduction
Java Applets.
Chapter 8 Graphics.
Presentation transcript:

Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font –Point –Dimension –Rectangle

Simple Graphics with Java Java provides classes w/ graphics abilities To use these classes we import the packages Many applets extend the Applet class This class inherits many capabilities It also has standard methods (such as paint) which we may override

Importing Packages Basic command: import packagename.*; // All classes in package import packagename.classname; // Specific class Examples: import java.applet.Applet; import java.awt.*;

Class Definition { } {} - block boundaries as in C/C++, may be nested (classes may have nested classes)

Class Header public class Name extends Cname Name - letter followed by arbitrary number of letters, digits, underscores legal: Hello, Arc45, What_a_long_name illegal: pieR-2, 7of9, 4$ofthis avoid: names already in use (Applet, Font, etc.) Cname - should be the name of an existing class (such as Applet)

Class Members Instance variables [ private ] Type Name [ = InitialValue ]; int LongLoc = 50; Methods public | private ReturnType Name ( Arglist ) { Body } public void paint (Graphics g) { g.drawString(“Hello World!”,20,20); }

Accessing Members Basic form: instanceName.memberName –Both instance variables and methods methods also require arguments ex. Robot1.longloc, Robot1.moveForward(10) –A member that produces another instance can be accessed ex. Robot1.nearestRobot.longloc

Class Methods public - can be called by anyone private - only accessible to members of class return type –void if function does not calculate anything –some type indicating type of value calculated

Method Arguments Argument list (Type Name, Type Name,..) Names used within method Method call objectName.methodName(arg list) list of args must match those in method definition example: g.drawString(“Hello World!”,30,30);

Method Names Method name –may be new identifier (new method) –may be the same as a previous method –new version considered to override existing –to override takes into account arguments –same name with different arg types is different Overridden function called when standard function normally called

Overriding Functions Example: paint –Basic method for showing graphical object –Placeholder method attached to base class, inherited by instances –Paint called when screen graphics shown (also when reshown) –User paint overrides base paint (which does nothing), supplies graphics to show object

Inheritance Example: paint in MyClass inherited from Component class (through Applet, Panel, etc.) Many objects are Components (look at this class in the documentation for various capabilities)

Graphics Class paint takes argument member of Graphics Graphics has lots of methods associated with it: void clearRect (int x, int y, int width, int height) paints a rectangle of size width,height with upper left corner at x,y in background color example: g.clearRect(5,8,10,20);

void drawLine (int x1, int y1, int x2, int y2) draws a line starting at x1,y1 ending at x2,y2 void drawRect (int x, int y, int width, int height) draws an unfilled rectangle size width,height at x,y void drawOval (int x, int y, int width, int height) draws an Oval within bounding box specified by x,y and width,height void drawArc (int x, int y, int width, int height, int startAngle, int arcAngle) similar to drawOval except that only the portion of the oval from startAngle to startAngle+arcAngle is shown (angles are in degrees from 0 to 360)

void drawRoundRect (int x, int y, int width, int height, int arcWidth int ArcHeight) draws an unfilled rectangle using x,y and width,height where the corners are rounded void draw3DRect (int x1, int y1, int width, int height, boolean raised) draws an unfilled rectangle that is shaded to look 3D (raised out if argument raised is true, in otherwise) void drawString (String s, int x, int y) places String s at x,y (bottom starts at y)

“Fill” versions of basic functions: similar to draw versions except the resulting object is filled in with the drawing color void fillRect (int x, int y, int w, int h) void fillOval (int x, int y, int w, int h) void fillArc (int x, int y, int w, int h, int s, int e) void fillRoundRect (int x, int y, int w, int h, int aW, int aH) void fill3DRect (int x, int y, int w, int h, boolean raised)

Changing colors: void setColor (Color c) sets drawing color to c can use value Color.name where name can be: black, blue, cyan, darkGray, gray, green, lightGray, magenta, orange, pink, red, white, or yellow can also make a color: Color myColor = new Color(0,255,128); values are amounts of red, green, blue in new color can also create a new color on the fly: g.setColor(Color(128,128,128));

import java.applet.*; import java.awt.*; public class WhatsThis extends Applet { public void paint (Graphics g) { g.setColor(Color.blue); g.drawOval(30,30,30,30); g.setColor(Color.yellow); g.drawOval(47,45,30,30); g.setColor(Color.black); g.drawOval(64,30,30,30); g.setColor(Color.green); g.drawOval(81,45,30,30); g.setColor(Color.red); g.drawOval(98,30,30,30); }

Setting fonts: void setFont (Font f) sets drawing font to f use Font constructor to set font: g.setFont(new Font(“NameStr”,style,size)); possible names: Helvetica, TimesRoman, Courier, Dialog, DialogInput names change in later versions styles: Font.PLAIN, Font.BOLD, Font.ITALIC can add two together Font.BOLD + Font.ITALIC size: any reasonable size

Constructors Most classes have methods for initializing a class member (sometimes more than one) These routines are called constructors Called by ClassName(arg list) Example: Color(rval,gval,bval) Usually used with a new command as in: Color myColor = new Color(128,0,255);

Point class instance variables: x, y (both public) constructor: Point(int x, int y) methods: boolean equals (int x, int y) true if the point p is at location x,y void move (int x, int y) changes the location of point p to x,y void translate (int dx, int dy) updates the location of point p’s x,y valus by dx,dy

Dimension class instance variables: width, height (both public) constructors: Dimension() - sets width,height to 0 Dimension(int w, int h) - sets width,height to w,h Dimension(Dimension d) - copies width,height of object d methods: boolean equals (Dimension d) - true if Dimension d has same width and height as calling object

Rectangle class instance variables: x, y, width, height (public) constructors: Rectangle() - empty rectangle at 0,0 Rectangle(int w, int h) - Rectangle at 0,0 with width,height w,h Rectangle(int x, int y, int w, int h) Rectangle(Point p) - empty rectangle at x,y of Point Rectangle(Dimension d) - rectangle at 0,0 with w,h of Dimension Rectangle(Point p, Dimension d) - rectangle at x,y of Point with w,h of Dimension

methods: void add(int x, int y) - expands rectangle enough to encompass point x,y void add(Point p) - similar to previous with Point void add(Rectangle r) - expand to encompass Rect void grow(int dw, int dh) - “expands” width by dw on both sides and height by dh on both sides boolean equals(Rectangle r) - true if same anchor point and dimensions boolean inside(int x, int y) - true if point within Rect boolean interesects(Rectangle r) - true if intersection not empty

methods: boolean isEmpty() - true if Rectangle empty] void move(int x, int y) - change anchor point to x,y void resize(int w, int h) - change dimensions to w,h void reshape(int x, int y, int w, int h) - change all values of rectangle void translate(int dx, int dy) - change anchor point by values dx,dy Rectangle intersection(Rectangle r) - returns Rectangle consisting of all points in both Rectangle union(Rectangle r) - returns smallest Rectangle containing both

Other useful methods: Applet: void init() - initialization, generally overwritten void resize(int w, int h) - changes size Applet expects to take void resize(Dimension d) - similar Dimension size() - inherited from Component, returns current size of Applet