Title: Data Structures for Graphics2D
1Data Structures for Graphics(2D)?
Graphics Lecture 4
Lecture 4
4.1
2Introduction
- Classes to represent
- Points
- Lines
- Shapes
- Functions (Methods) to make them draw
- Fitting them into GController and GView
Lecture 4
4.2
3Point2D class
- The most basic thing we need to represent in
graphics is the point. A point is basically two
coordinates x and y which are real numbers.
This suggests that we might define a Java class
to represent a point that looks something like
this
Lecture 4
4.3
4Point2D class
class Point2D public double x 0 public
double y 0 //end class Point2D
Lecture 4
4.4
5Lines
Lecture 4
4.5
6Line2D
Lecture 4
4.6
7Line2D
class Line2D private Point2D src private
Point2D dest //end class Line2D
Lecture 4
4.7
8Line2D
Lecture 4
4.8
9Line2D
Lecture 4
4.9
10Shape2D
Lecture 4
4.10
11Shape2D
class Shape2D private java.util.ListltLine2Dgt
lines new ArrayListltLine2Dgt() //end class
Shape2D
Lecture 4
4.11
12Drawing2D
A drawing can (obviously?) have more than one
shape on it.
Lecture 4
4.12
13Drawing2D
Lecture 4
4.13
14Drawing2D
class Drawing2D private java.util.ListltShape2
Dgt shapes new ArrayListltShape2Dgt()
//Drawing2D
Lecture 4
4.14
15So far, so good.
- So, in full, we have a drawing object, which has
a list of shape objects, which have lists of line
objects, which have two point objects each. This
can be shown as an instance diagram
Lecture 4
4.15
16A data structure in memory
Lecture 4
4.16
17Basic 2D data structure
Lecture 4
4.17
18Adding methods
- At the moment, our classes are capable of storing
data about graphics, but they are missing two
things - a means of actually assembling a structure from
the classes. We need to add constructors and
accessor methods - any drawing functionality we need to add the
actual commands which cause things to be
displayed on the screen.
Lecture 4
4.18
19Point2D
public Point2D(double in_x, double in_y) x
in_x y in_y // end constructor public
double x() return (x) public double
y() return (y)
Lecture 4
4.19
20Line2D
- If we now wanted to use the Point2D class in a
program, we could write something like - Point2D myPoint new Point2D(100,150)
- The newly created point object will now
faithfully remember our coordinates for us. We
can even ask it to tell us what it was we asked
it to remember - System.out.println (( myPoint.x() ,
myPoint.y() ))
Lecture 4
4.20
21Line2D
public Line2D (Point2D src, Point2D dest)
this.src src this.dest dest //end
constructor public void draw(Graphics
g) g.drawLine((int)src.x(), (int)src.y(),
(int)dest.x(), (int)dest.y()) //end draw
Lecture 4
4.21
22Shape2D
- public void addLine(Line2D inLine)?
- lines.add(inLine)
- //end addLine
Lecture 4
4.22
23Shape2D
- public void addLine(double x1, double y1, double
x2, double y2) - Line2D myLine new Line2D(x1, y1, x2, y2)
- lines.add(myLine)
-
- //end addLine
Lecture 4
4.23
24Shape2D
public void draw(Graphics g) for (Line2D l
lines) l.draw(g) //end draw
Lecture 4
4.24
25Drawing2D
- public void addShape(Shape2D inShape)
- shapes.add(inShape)
- //end addShape
- public void draw(Graphics g)
- for (Shape2D s shapes)
- s.draw(g)
-
- //end draw
Lecture 4
4.25
26GController same as before, except
public class GController extends JFrame
private GView myGView private Drawing2D
myDrawing
Lecture 4
4.26
27GController same as before, except
public GController() myGView new
GView() myDrawing new Drawing2D() myGView.s
etDrawing(myDrawing) Shape2D shape1 new
Shape2D() myDrawing.addShape(shape1) Line
line new Line( 50, 50, 50, 150)
shape1.addLine(line) line new Line( 50, 100,
100, 100) shape1.addLine(line)
Lecture 4
4.27
28GView
class GView extends JPanel private Drawing2D
myDrawing public void setDrawing(Drawing2D
inDrawing) myDrawing inDrawing publ
ic void paintComponent(Graphics
g) myDrawing.draw(g) //paintComponent //
Gcanvas
Lecture 4
4.28
29The Completed System
Lecture 4
4.29
30Summary
- So far, you should have learned about
- How to represent a drawing in terms of point,
line, shape and drawing objects - How those classes can be implemented in Java
- How to add methods to those classes which allow
them to be assembled into useful structures - How to add methods which cause the drawings to be
displayed on screen.
Lecture 4
4.30
31Next time.
- Transformations
- Translation
- Scaling
- Rotation
Lecture 4
4.31