chapter 2: graphics in java basics of classes –instance variables –methods –overriding methods...

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

Upload: shanon-wright

Post on 21-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

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

Chapter 2: Graphics In Java

• Basics of classes– instance variables– methods– overriding methods

• Graphics class (drawing routines)• Other classes

– Color– Font– Point– Dimension– Rectangle

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

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

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

Importing Packages

Basic command:import packagename.*; // All classes in package

import packagename.classname; // Specific class

Examples:import java.applet.Applet;

import java.awt.*;

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

Class Definition

<class header> {

<class body>

}

{} - block boundaries as in C/C++, may be

nested (classes may have nested classes)

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

Class Header

public class Name extends CnameName - letter followed by arbitrary number of

letters, digits, underscoreslegal: 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)

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

Class Members

• Instance variables[ private ] Type Name [ = InitialValue ];

int LongLoc = 50;

• Methodspublic | private ReturnType Name ( Arglist )

{ Body }

public void paint (Graphics g) {

g.drawString(“Hello World!”,20,20); }

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

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

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

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

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

Method Arguments

• Argument list (Type Name, Type Name, ..)Names used within method

• Method callobjectName.methodName(arg list)

list of args must match those in method definition

example: g.drawString(“Hello World!”,30,30);

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

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

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

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

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

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)

Component

MyClass

Container

Panel

Applet

Object

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

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);

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

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)

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

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)

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

“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)

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

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));

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

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);

}

}

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

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.ITALICcan add two together Font.BOLD + Font.ITALIC

size: any reasonable size

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

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);

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

Point classinstance 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

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

Dimension classinstance 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

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

Rectangle classinstance 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

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

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

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

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

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

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