Data Structures for Graphics2D - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Data Structures for Graphics2D

Description:

The most basic thing we need to represent in graphics is the point. A point is basically two coordinates x ... We need to add constructors and accessor methods ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 32
Provided by: ianfer8
Category:

less

Transcript and Presenter's Notes

Title: Data Structures for Graphics2D


1
Data Structures for Graphics(2D)?
Graphics Lecture 4
Lecture 4
4.1
2
Introduction
  • Classes to represent
  • Points
  • Lines
  • Shapes
  • Functions (Methods) to make them draw
  • Fitting them into GController and GView

Lecture 4
4.2
3
Point2D 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
4
Point2D class
class Point2D public double x 0 public
double y 0 //end class Point2D
Lecture 4
4.4
5
Lines
Lecture 4
4.5
6
Line2D
Lecture 4
4.6
7
Line2D
class Line2D private Point2D src private
Point2D dest //end class Line2D
Lecture 4
4.7
8
Line2D
Lecture 4
4.8
9
Line2D
Lecture 4
4.9
10
Shape2D
Lecture 4
4.10
11
Shape2D
class Shape2D private java.util.ListltLine2Dgt
lines new ArrayListltLine2Dgt() //end class
Shape2D
Lecture 4
4.11
12
Drawing2D
A drawing can (obviously?) have more than one
shape on it.
Lecture 4
4.12
13
Drawing2D
Lecture 4
4.13
14
Drawing2D
class Drawing2D private java.util.ListltShape2
Dgt shapes new ArrayListltShape2Dgt()
//Drawing2D
Lecture 4
4.14
15
So 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
16
A data structure in memory
Lecture 4
4.16
17
Basic 2D data structure
Lecture 4
4.17
18
Adding 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
19
Point2D
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
20
Line2D
  • 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
21
Line2D
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
22
Shape2D
  • public void addLine(Line2D inLine)?
  • lines.add(inLine)
  • //end addLine

Lecture 4
4.22
23
Shape2D
  • 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
24
Shape2D
public void draw(Graphics g) for (Line2D l
lines) l.draw(g) //end draw
Lecture 4
4.24
25
Drawing2D
  • 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
26
GController same as before, except
public class GController extends JFrame
private GView myGView private Drawing2D
myDrawing
Lecture 4
4.26
27
GController 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
28
GView
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
29
The Completed System
Lecture 4
4.29
30
Summary
  • 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
31
Next time.
  • Transformations
  • Translation
  • Scaling
  • Rotation

Lecture 4
4.31
Write a Comment
User Comments (0)
About PowerShow.com