Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 2: Graphics In Java Basics of classes –instance variables –methods –overriding methods Graphics class (drawing routines) Other classes –Color –Font."— Presentation transcript:

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

2 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

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

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

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

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

7 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

8 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

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

10 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

11 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

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

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

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

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

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

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

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

19 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

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

21 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

22 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

23 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

24 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

25 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

26 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


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

Similar presentations


Ads by Google