Shapes in 2D Graphics

Download Report

Transcript Shapes in 2D Graphics

Shapes in 2D Graphics
Objectives
To give an overview on the Java classes for Graphics.
To present the main classes of java.awt.geom:
Point2D, Line2D, Rectangle2D,…
5/27/2016
Computer Graphics - Lecture 7
1
Graphics, Graphics2D, Geom
Class Graphics (java.awt.*)
- JDK 1.1.
- base class for Graphics2D.
- basic methods (drawing, filling,…) for working with shapes, images.
Graphics2D Class (java.awt)
- extends Graphics; any method from Graphics can be used.
- offers a diversity of methods to for working shapes, transformations, colours
- fundamental class for rendering shapes, texts,…
java.awt.geom package
- provides classes for working with geometrical objects in 2D graphics.
- Point2D + (Line2D, Rectangle2D, Arc2D, Ellipse2D,…)
- coordinates real by using inner classes (Double, Float) in each class.
5/27/2016
Computer Graphics - Lecture 7
2
Point2D
Point2D offers points in real coordinates.
Abstract class with inner classes Double, Float.
Construct a point:
double coordinates: Point2D p = new Point2D.Double();
Point2D p = new Point2D.Double(x,y);
float coordinates: Point2D p = new Point2D.Float();
Point2D p = new Point2D.Float(x,y);
Gives methods for:
- get/set the point location:
p.getX(), p.getY(), p.setLocation(x,y), p.setLocation(p1),
- find the distance between the point p and another point.
p.distance(x,y), p.distance(p1), Point2D.distance(p1,p2)
See the class’ methods.
5/27/2016
Computer Graphics - Lecture 7
3
Point2D: Example
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
public class L8Appl1 extends Applet {
Point2D p1,p2; // declare two double points
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g; // convert g to a Graphics2D
// construct p1 and p2
p1 = new Point2D.Double(100.,100.); g2.drawString(“P1”,100,102);
p2 = new Point2D.Double(200.,200.); g2.drawString(“P2”,200,202);
g2.draw(new Line2D.Double(p1.getX(),p1.getY(),p2.getX(),p2.getY())); // draw a line from p1 to p2
double lenght=p1.distance(p2); // find the distance between p1 and p2.
g.drawString("The distance is : " + lenght,20,20); // write the distance
}
}
Remark: length is also given by Point2D.distance(p1,p2)
5/27/2016
Computer Graphics - Lecture 7
4
5/27/2016
Computer Graphics - Lecture 7
5
Line2D
Line2D offers lines with real coordinates.
Abstract class with two inner classes: Double and Float.
Construct lines as follows:
Line2D line = new Line2D.Double();
Line2D line = new Line2D.Double(p1,p2);
Line2D line = new Line2D.Double(x1,y1,x2,y2);
Similarly for float.
Offer method for:
- get/set methods: coordinates
- contains methods: test if the line contains a point or is contained by a
rectangle.
- intersects methods: test if the line is intersected by another line or
rectangle.
- distance methods: from a point to the line.
5/27/2016
Computer Graphics - Lecture 7
6
Line2D: Example
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
public class L8Appl2 extends Applet {
Line2D l1= new Line2D.Double(), l2= new Line2D.Double(); // construct two lines
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
l1.setLine(50,50,200,200);l2.setLine(200,50,50,200); // set the lines
g2.draw(l1);g2.draw(l2); // draw the lines !!!
if(l1.intersectsLine(l2))g2.drawString("Intersected lines",10,10); // if l1 and l2 are intersected
else g2.drawString("Disjoint lines",10,10);
}
}
5/27/2016
Computer Graphics - Lecture 7
7
5/27/2016
Computer Graphics - Lecture 7
8
Rectangle2D
Class RectangularShape is the base for:
Rectangle2D, RoundRectangle2D, Arc, Ellipse.
Rectangle2D has two inner classes: Double, Float.
Construct rectangles as follows:
Rectangle2D r = new Rectangle2D.Double();
Rectangle2D r = new Rectangle2D.Double(x,y,w,h);
Methods for:
get / set the elements x,y,w,h.
find if the rectangle contains a point or is contained in another
rectangle
intersection with a line or with another rectangle
union with another rectangle.
Remark: RoundRectangle2D is similar.
5/27/2016
Computer Graphics - Lecture 7
9
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
public class L8Appl3 extends Applet {
Rectangle2D r1,r2,r3; // Declare three rectangles
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
r1 = new Rectangle2D.Double(100,100,200,100); // Construct two rectangles
r2 = new Rectangle2D.Double(150,150,200,100);
r3 = new Rectangle2D.Double(); // construct an empty rectangle
g.setColor(Color.red);g2.draw(r1); g.setColor(Color.blue);g2.draw(r2);
if(r1.intersects(r2.getX(),r2.getY(),r2.getWidth(),r2.getHeight())){
// find is r1 intersects r2
Rectangle2D.intersect(r1,r2,r3);
g.setColor(Color.black);g2.draw(r3);
}
}
}
5/27/2016
Computer Graphics - Lecture 7
10
5/27/2016
Computer Graphics - Lecture 7
11
Lines, Rectangles, … = Shapes
A continuous sequence of lines, quadratic and cubic curves presents a shape.
Lines, Rectangles, … are shapes.
Interface Shape represents the support for all the geom classes.
Abstract methods for:
get the bound of the shape. (the rectangle that contains the shape.)
contains methods. (a point is the shape or the shape is in a rectangle.)
intersects methods. (if the shape intersects an rectangle.)
working with PathIterator. (the shape’s boundary.)
Graphics2D draws shapes: g2.draw(shape).
5/27/2016
Computer Graphics - Lecture 7
12
PathIterator Interface
PathIterator: traverse and identify each component of a path.
The Nature of a component:
SEG_MOVETO,SEG_LINETO, SEG_QUADTO,
SEG_CUBICTO, SEG_CLOSETO.
Methods:
currentSegment(coordinates) – return the nature of the segment and its
coordinates in float or double.
next() – move the pathiterator forward by one segment.
isDone() – find if the pathiterator is done.
5/27/2016
Computer Graphics - Lecture 7
13
Construct a General Path
The GeneralPath Class helps to create a shape as a sequence of segments.
Methods to add segments to the GeneralPath.
moveTo(x,y) – move the current path without drawing
lineTo(x,y) – draw a line to (x,y)
quadTo(x1,y1,x2,y2) – draw a quadratic curve to (x1,y1).
curveTo(x1,y1,x2,y2,x3,y3) - draw a cubic curve to (x1,y1).
Append to the general path a shape or a pathiterator.
contains and intersects methods with a point and with a rectangle.
get methods for the current point of the bounds.
5/27/2016
Computer Graphics - Lecture 7
14
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
public class L8Appl4 extends Applet {
GeneralPath path = new GeneralPath(); // declare a GeneralPath
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
construct(200,200,5,100); g2.draw(path);
}
public void construct(float x0,float y0,int n,float r){
float x [] = new float [n];float y [] = new float [n]; // construct the coordinates arrays
for(int i=0;i<n;i++){ // find the coordinates based on the general equation
x[i]=x0+(float)(r*Math.cos(2*i*Math.PI/n)); y[i]=x0+(float)(r*Math.sin(2*i*Math.PI/n));
}
path.moveTo(x[0],y[0]);
// go to the first point of the line
for(int i=0;i<n;i++)path.lineTo(x[(i+1)%n],y[(i+1)%n]); // add to the shape the segments one by one
}
}
5/27/2016
Computer Graphics - Lecture 7
15
5/27/2016
Computer Graphics - Lecture 7
16
The Area Class
Area Class provides methods for combining shapes. (Implement Shape.)
The main operations: union, intersection, subtraction, exclusive or.
Constructing Area
Area ar = new Area(); - empty area
Area ar = new Area(Shape sh); - area for the interior of sh
Methods for
add(ar) – add the area sh to the current area.
intersect(sh) - intersect the area sh to the current area.
subtract(sh) - subtract the area sh to the current area.
Methods inherited from Shape: draw, fill, contains, intersects,…
.
5/27/2016
Computer Graphics - Lecture 7
17
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
public class L8Appl5 extends Applet {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.blue);g.fillRect(0,0,500,500);g.setColor(Color.yellow);
Area area1 = new Area (new Ellipse2D.Double(50,50,200,100));
Area area2 = new Area (new Ellipse2D.Double(100,100,100,200));
area1.add(area2);g2.fill(area1);
}
}
5/27/2016
Computer Graphics - Lecture 7
18
5/27/2016
Computer Graphics - Lecture 7
19
Problems To Solve
1.
Generate n random lines and find how many intersections points are found.
6 intersection points for the above example.
2.
Generate n random rectangles and find the area covered by them.
To Read: Jonathan Knudsen, Java 2D Graphics, chapter 3.
5/27/2016
Computer Graphics - Lecture 7
20