-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image2d.java
122 lines (103 loc) · 3.55 KB
/
Image2d.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.awt.*;
import java.util.Collections;
import java.util.LinkedList;
import javax.swing.JComponent;
import javax.swing.JFrame;
// Manipulation for images
public class Image2d {
public int width; // width of the image
public int height; // height of the image
public int scaling=10;
public int ofs=50;
public java.util.List<ColoredPolygon> coloredPolygons; // colored polygons in the image
public java.util.List<Edge> edges; // edges to add to separate polygons
// Constructor that instantiates an image of a specified width and height
public Image2d(int width, int height) {
this.width = width;
this.height = height;
coloredPolygons = Collections.synchronizedList(new LinkedList<ColoredPolygon>());
edges = Collections.synchronizedList(new LinkedList<Edge>());
}
// Return the width of the image
public int getWidth() {
return width;
}
// Return the height of the image
public int getHeight() {
return height;
}
// Return the colored polygons of the image
public java.util.List<ColoredPolygon> getColoredPolygons() {
return coloredPolygons;
}
// Return the edges of the image
public java.util.List<Edge> getEdges() {
return edges;
}
// Create the polygon with xcoords, ycoords and color
public void addPolygon(int[] xcoords, int[] ycoords, Color color) {
int[] x_scaled= new int[xcoords.length];
int[] y_scaled= new int[ycoords.length];
for (int i=0; i<xcoords.length; i++) {
x_scaled[i] = scaling*xcoords[i]+ofs;
y_scaled[i] = height-(scaling*ycoords[i]+ofs);
}
coloredPolygons.add(new ColoredPolygon(x_scaled, y_scaled, color));
}
// Create the edge with coordinates x1, y1, x2, y2
public void addEdge(int x1, int y1, int x2, int y2, int width) {
edges.add(new Edge(scaling*x1+ofs, height-(scaling*y1+ofs), scaling*x2+ofs, height-(scaling*y2+ofs), width));
}
// Clear the picture
public void clear() {
coloredPolygons = Collections.synchronizedList(new LinkedList<ColoredPolygon>());
edges = Collections.synchronizedList(new LinkedList<Edge>());
}
}
// Image2d component
class Image2dComponent extends JComponent {
private static final long serialVersionUID = -7710437354239150390L;
private Image2d img;
public Image2dComponent(Image2d img) {
this.img = img;
setPreferredSize(new Dimension(img.getWidth(), img.getHeight()));
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// set the background color
Dimension d = getSize();
g2.setBackground(Color.white);
g2.clearRect(0,0,d.width,d.height);
// draw the polygons
synchronized (img.getColoredPolygons()) {
for (ColoredPolygon coloredPolygon : img.getColoredPolygons()) {
g2.setColor(coloredPolygon.color);
g2.fillPolygon(coloredPolygon.polygon);
g2.drawPolygon(coloredPolygon.polygon);
}
}
// draw the edges
g2.setColor(Color.white);
synchronized (img.getEdges()) {
for (Edge edge : img.getEdges()) {
g2.setStroke(new BasicStroke(edge.width));
g2.drawLine(edge.x1, edge.y1, edge.x2, edge.y2);
}
}
}
}
// Frame for the vizualization
class Image2dViewer extends JFrame {
private static final long serialVersionUID = -7498525833438154949L;
static int xLocation = 0;
Image2d img;
public Image2dViewer(Image2d img) {
this.img = img;
this.setLocation(xLocation, 0);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
add(new Image2dComponent(img));
pack();
setVisible(true);
xLocation += img.getWidth();
}
}