Lab 2
Lab 2
Thanh-Sach LE
[email protected]
Question 1
Do the following tasks:
1. Compile the project
2. Explore the code (with helps from Mr. LAM and Mr. PHU) and try to understand it.
Question 2
Using the document accompanied with materiala to create class SpaceMapping in package geom
in “DSA2020_GUI“ .
a File: SpaceMapping.pdf
Question 3
Do the tasks below:
1. Create an instance of SpaceMapping in class WorkingPanel .
1
2.2.1 Print Mouse’s Location: Guideline
1. Create an instance of SpaceMapping in class WorkingPanel
• declare and initialize a data field inside of WorkingPanel , see Line 5, Figure 1.
Question 4
Do the following tasks:
2 java.awt.event.ComponentListener
2
1 public class WorkingPanel extends JPanel
2 implements MouseMotionListener, MouseListener,
3 ItemListener, ActionListener, ComponentListener{
4
20 @Override
21 public void mouseMoved(MouseEvent e) {
22 Point2D logPoint = this.spaceMapping.device2Logic(e.getX(), e.getY
());
23 String message = String.format("mouseMoved: Device(x,y)=(%d,%d);
Logic(x,y)=(%6.2f, %6.2f)", e.getX(), e.getY(), logPoint.getX(),
logPoint.getY());
24 MainFrame.infoPanel.println(message);
25 }
26 //here: similarly for other mouse events
27 @Override
28 public void componentResized(ComponentEvent e) {
29 Dimension size = this.getSize();
30 int xGap = 50, yGap = 20;
31 this.spaceMapping.updateDevViewPort(xGap, size.width-2*xGap, yGap,
size.height-2*yGap);
32 }
33
34 @Override
35 public void componentMoved(ComponentEvent e) {}
36
37 @Override
38 public void componentShown(ComponentEvent e) {}
39
40 @Override
41 public void componentHidden(ComponentEvent e) {}
42 }
3
Figure 2: Demo application for Question 2: students are required to modify “DSA2020_GUI“ to show the
mouse’s position in the device space and the logic space as shown in message areas of the GUI. Adding
New button and drawing axis are exluded from Question 2.
1 package geom;
2 import java.awt.*;
3
4
Question 5
Do the following tasks:
1. Create Class Poin2D in Package geom as shown in Figure 4. You need to fill the code at the
location marked as Reuse code from LAB1 with your answer for LAB1.
2. Modify the code to make Class Point2D be a subclass of Class GeomObject .
3. Fill method linear at the location marked as /*answer here*/ to do:
5. Read document of fillOval then replace Line 53 in Figure 4 to show the point as a circle centering
at the point and having radius = P OIN T _HALF _SIZE.
Please refer to Java Color for more information on using Java Color.
3.4 Graph
A graph can be a polyline (e.g. quadratic or linear line in Figure 5) or and sequence of dots (e.g. sin
function in Figure 5). Graphs are usually used to visualize function or relation between variable x and y.
Functions are defined in an infinite range, so you need viewport for viewing them. Therefore, class Graph
must maintain the following information, see Figure 6 for an overview.
5
1. Viewport on the logic space for viewing graph. Remember, a viewport maintains four values: xMin,
xMax, yMin, and yMax (see class Viewport ).
2. List of points, look at sin function on Figure 5.
3. Mode of drawing; we draw as dots like sin function on Figure 5 or quadratic function on Figure 5.
4. Graph needs to extend from class GeomObject to inherit data fields and methods of geometrical
objects.
Method copyPoints (Line 77-84): assigning array of points received from the parameter to the data
field inside of Graph is not enough. A graph must maintain its viewport; so, it given a list of points,
copyPoints must do two tasks as follows:
1. copies the point list from the parameter.
2. finds the smallest rectangle that contains all the point int the list. It does this by: (a) initialize the
viewport to contain only the first, and (b) for each point in the list it enlarge the viewport to contain
the point (see method addPoint in class Viewport ).
Question 6
Do the following tasks:
1. Create class Graph from the source given partially in Figure 6, 7 and 8.
2. Try to understand the idea of method quaratic to generate a quaratic graph using parameters
given.
3. Copy the idea of quadratic generation, fill code for method sin in Figure 7.
Question 7
Do the following tasks:
1. Add button Graph to toolbar, see Figure 5.
2. When users press on button Graph do:
6
(b) intialize sin , and quad in actionPerformed by calling to Graph.sin and Graph.quadratic
respectively.
(c) Inside of paintComponent , you check if sin / quad is not null, you call draw for sin
and quad . Remember, you need to pass instance of SpaceMapping (data field of class
WorkingPanel ) to draw to allow graphs doing coordinate conversion.
7
1 package geom;
2 import java.awt.*;
3 import java.util.Random;
4 public class Point2D /*answer here: Point2D is a subclass of GeomObject*/{
5 public static int POINT_HALF_SIZE = 2;
6 private double x;
7 private double y;
8 //Add setters and getters here: Reuse code from LAB1
9 public Point2D(){
10 this.x = 0; this.y = 0;
11 }
12 public Point2D(double x, double y){
13 this.x = x; this.y = y;
14 }
15 public Point2D(Point2D oldPoint){
16 this.x = oldPoint.x; this.y = oldPoint.y;
17 }
18 public Point2D clone(){
19 return new Point2D(this.x, this.y);
20 }
21 public String toString(){
22 return String.format("P(%6.2f, %6.2f)", this.x, this.y);
23 }
24 public static Point2D[] generate(int N, double min, double max){
25 //Reuse code from LAB1
26 }
27 public static Point2D[] linear(int N, double a, double b, double xMin,
double xMax){
28 Point2D[] list = new Point2D[N];
29 double step = (xMax - xMin)/(N-1); //xMax inclusive
30 double x = xMin;
31 for(int idx=0; idx < N; idx++){
32 /*answer here*/
33 x += step;
34 }
35 return list;
36 }
37 public static double distanceAB(Point2D a, Point2D b){
38 //Reuse code from LAB1
39 }
40 public double distanceTo(Point2D point){
41 //Reuse code from LAB1
42 }
43
44 @Override
45 public void draw(Graphics g, SpaceMapping mapper) {
46 Graphics2D g2 = (Graphics2D) g;
47 Point2D point = mapper.logic2Device(this.getX(), this.getY() );
48
52 g2.setColor(this.faceColor);
53 g2.fillRect(/*anwser here*/);
54 }
55 }
8
Figure 5: Axis and graph
9
56 package geom;
57 import java.awt.*
58 public class Graph extends GeomObject{
59 public enum GraphMode {
60 LINE,
61 SCATTER
62 };
63 protected Viewport viewport = null;
64 private Point2D[] points = null;
65 private GraphMode mode = GraphMode.LINE;
66
10
101 package geom;
102 import java.awt.*
103 public class Graph extends GeomObject{
104 //here: more code
105 public static Graph sin(double a, double xMin, double xMax, double step
){
106 /*answer here/
107 }
108 public static Graph quadratic(double a, double b, double c, double xMin
, double xMax, double step){
109 int N = (int)((xMax - xMin)/step) + 1;
110 Point2D[] points = new Point2D[N];
111 double x = xMin;
112 for(int idx = 0; idx < N; idx++){
113 double y = a*x*x + b*x + c;
114 points[idx] = new Point2D(x, y);
115 x += step;
116 }
117 return new Graph(points);
118 }
119
11
122 package geom;
123 import java.awt.*
124 public class Graph extends GeomObject{
125 //here: more code
126 @Override
127 public void draw(Graphics g, SpaceMapping mapper) {
128 Graphics2D g2 = (Graphics2D) g;
129 //
130 if(this.mode == GraphMode.LINE){
131 if(this.points == null) return;
132 int[] x = new int[this.points.length];
133 int[] y = new int[this.points.length];
134 for(int idx=0; idx < this.points.length; idx++){
135 Point2D devPoint = mapper.logic2Device(this.points[idx]);
136 x[idx] = (int)devPoint.getX();
137 y[idx] = (int)devPoint.getY();
138 }
139 g2.setColor(this.edgeColor);
140 Stroke style = new BasicStroke(this.line_weight);
141 g2.setStroke(style);
142 g2.drawPolyline(x, y, x.length);
143 }
144 else if(this.mode == GraphMode.SCATTER){
145 for(int idx=0; idx < this.points.length; idx++){
146 this.points[idx].draw(g, mapper);
147 }
148 }
149 else{
150 throw new UnsupportedOperationException("Not supported yet.");
151 }
152 }
153 //here: more code
154 }
12