Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphics Graphics Java Vectors Java Enumeration Graphical Spreadsheets

Similar presentations


Presentation on theme: "Graphics Graphics Java Vectors Java Enumeration Graphical Spreadsheets"— Presentation transcript:

1 Graphics Graphics Java Vectors Java Enumeration Graphical Spreadsheets
Java Pixels and Coordinate System ObjectEditor Shapes Java Vectors Variable-Sized Instances Java Enumeration

2 A Square With Text

3 Java Coordinate System
(3,2) (0,0) X pixels Y

4 Describing Shapes x1, y1 x2, y2 Bounding Rectangle h1 h2 w1 w2 x4, y4
hello h3 w3

5 A Square With Text import shapes.RectangleModel;
import shapes.TextModel; public interface SquareWithText { public RectangleModel getSquare(); public void setSquare(RectangleModel newVal); public TextModel getText (); }

6 A Square With Text import shapes.RectangleModel
import shapes.TextModel public class ASquareWithText implements SquareWithText { RectangleModel square; TextModel text; … // constant declarations public ASquareWithText() { square = new RectangleModel (INIT_X, INIT_Y, INIT_SIDE_LENGTH, INIT_SIDE_LENGTH); text = new TextModel(TEXT_STRING); placeText(); } void placeText() {text.setCenter(square.getCenter());} public RectangleModel getSquare () {return square;} public void setSquare (RectangleModel newVal) { square = newVal; public TextModel getText () {return text};

7 ObjectEditor Classes x1, y1 w1 h1 w2 h2 x2, y2 Bounding Rectangle
RectangleModel r = new RectangleModel(x1, y1, h1,w1) LineModel r = new LineModel(x2, y2, h2,w2) x5, y5 hello TextModel r = new TextModel(x4, y4, h4,w4) PointModel r = new PointModel(x5,y5)) OvalModel r = new OvalModel(x3, y3, h3,w3)

8 AWT Classes x1, y1 w1 h1 x5, y5 Point r = new Point(x5, y5))
Rectangle r = new Rectangle(x1, y1, h1,w1) r.x  x1 r.x = x4 p.x  x1 Dimension d = r.getSize() d.width  w1 Point p = r.getLocation()

9 ObjectEditor Vs AWT Classes

10 Methods on ObjectEditor Classes
getX(), setX(), getY(), setY(), getWidth(), setWidth(), getHeight(), setHeight. getCenter(), setCenter(), getNWCorner(), setNWCorner(), getNECorner(), setNECorner(), getSWCorner(), setSWCorner(), getSECorner(), setSECorner(),. isFilled(), setFilled() getColor(), setColor()

11 Shapes Demo

12 Variable-Size Collection
Plotting Points Variable-Size Collection History Elements accessed by ObjectEditor Conventions for exporting elements

13 Conventions for Variable-Sized Collection
Write method import shapes.PointModel; public interface PointHistory { public void addElement (int x, int y); public PointModel elementAt (int index); public int size(); } Arbitrary Type. Read methods

14 Read Vs Write Methods Read Methods Write Methods
Used to get components of object Getter methods size(), addElement() Write Methods Used to change components of object Setter methods addElement(), deleteElement(), replaceElement() Distinction independent of Conventions

15 Vectors (java.util.Vector)
Vector v; Vector Instance null James Dean Object Joe Doe ALoan Instance Vector Instance v = new Vector(); v.addElement(“James Dean”) v.addElement(“Joe Doe”) v.addElement(5) v.addElement(new ALoan()) v.addElement(new Vector())

16 Methods of Class Vector
public final int size() public final Object elementAt(int index) public final void addElement(Object obj) public final void setElementAt(Object obj, int index) public final void insertElementAt(Object obj, int index) public final boolean removeElement(Object obj) public final void removeElementAt(int index) public final int indexOf(Object obj) public final Enumeration elements()

17 Methods of Interface Enumeration (java.util.Enumeration)
public boolean hasMoreElements(); public Object nextElement(); Enumeration elements = vector.elements(); while ( elements.hasMoreElements()) System.out.println(elements.nextElement();

18 Methods of Interface Enumeration (java.util.Enumeration)
public boolean hasMoreElements(); public Object nextElement(); for (Enumeration elements = vector.elements(); elements.hasMoreElements();) System.out.println(elements.nextElement();

19 Using Vector Directly as Point History
Point history user Vector v = new Vector(); Vector Instance null size():  int elementAt(): index  Object PointModel Instance Violating Least Privilege PointModel Instance addElement(): Object  void Vector Instance removeElementAt(): Object  void v.addElement(new APointModel(0,0); v.addElement(new APointModel(10,90); v.addElement(new Vector()); v.removeElementAt(0);

20 Encapsulating Vector in APointHistory
Point history user PointHistory pointHistory = new APointHistory(); APointHistory instance elementAt(): index  PointModel addElement(): int*int  void size():  int Vector v = new Vector(); Vector Instance null size():  int elementAt(): index  Object PointModel Instance addElement(): Object  void removeElementAt(): Object  void pointHistory.addElement(0,0); pointHistory.addElement(new Vector())

21 APointHistory Implementation
import java.util.Vector; public class APointHistory implements PointHistory { Vector contents = new Vector(); public void addElement (int x, int y) { contents.addElement(new PointModel(x, y)); } public PointModel elementAt (int index) { return (PointModel) contents.elementAt(index); public int size() { return contents.size();

22 Animation Animating point

23 Creating Animating Point
static final int ANIMATING_POINT_X = 0, ANIMATING_POINT_Y = 0; PointModel animatingPoint = new PointModel (ANIMATING_POINT_X, ANIMATING_POINT_Y); public PointModel getAnimatingPoint() { return animatingPoint; }

24 Animating the Point public void animate () {
Enumeration points = contents.elements(); while (points.hasMoreElements()) { animatingPoint.setCenter( ((PointModel) points.nextElement()).getCenter()); } animatingPoint.setX(ANIMATING_POINT_X); animatingPoint.setY(ANIMATING_POINT_Y);

25 Animating the Point public synchronized void animate () {
Enumeration points = contents.elements(); while (points.hasMoreElements()) { animatingPoint.setCenter( ((PointModel) points.nextElement()).getCenter()); try { Thread.sleep(ANIMATION_DELAY); } catch (InterruptedException e) { System.out.println("Animation interrupted"); } animatingPoint.setX(ANIMATING_POINT_X); animatingPoint.setY(ANIMATING_POINT_Y);


Download ppt "Graphics Graphics Java Vectors Java Enumeration Graphical Spreadsheets"

Similar presentations


Ads by Google