Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Graphics –Graphical Spreadsheets –Java Pixels and Coordinate System –ObjectEditor Shapes Java Vectors –Variable-Sized Instances Java Enumeration."— Presentation transcript:

1 Graphics –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 Y X (0,0) (3,2) pixels

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

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; placeText(); } public TextModel getText () {return text }; }

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

8 AWT Classes x1, y1 w1 h1 x5, y5 Rectangle r = new Rectangle(x1, y1, h1,w1) Point r = new Point(x5, y5)) r.x  x1 r.x= x4 p.x  x1 Dimension d = r.getSize()  w1 d.width 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 Plotting Points Variable-Size Collection History Elements accessed by ObjectEditor Conventions for exporting elements

13 Conventions for Variable-Sized Collection 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 Write method

14 Read Vs Write Methods Read 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 null Vector Instance Vectors (java.util.Vector) Vector v; James Dean Joe Doe ALoan Instance v = new Vector(); v.addElement(“James Dean”) v.addElement(“Joe Doe”) v.addElement(new ALoan()) Vector Instance v.addElement(new Vector()) v.addElement(5) Object

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

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

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 –Graphical Spreadsheets –Java Pixels and Coordinate System –ObjectEditor Shapes Java Vectors –Variable-Sized Instances Java Enumeration."

Similar presentations


Ads by Google