-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEx3.java
More file actions
149 lines (135 loc) · 4.56 KB
/
Ex3.java
File metadata and controls
149 lines (135 loc) · 4.56 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package Exe.EX3;
import java.awt.Color;
/**
* This class is a simple "inter-layer" connecting (aka simplifing) the
* StdDraw_Ex3 with the Map2D interface.
* Written for 101 java course it uses simple static functions to allow a
* "Singleton-like" implementation.
* You should change this class!
*
* @author
* ID1: 326648532
* ID2: 214266819
*/
public class Ex3 {
private static Map2D _map = null;
static Color _color = Color.blue;
private static String _mode = "";
public static final int WHITE = Color.WHITE.getRGB();
public static final int BLACK = Color.BLACK.getRGB();
static Point2D Prev_point;
public static void main(String[] args) {
int dim = 10; // init matrix (map) 10*10
init(dim);
}
private static void init(int x) {
StdDraw_Ex3.clear();
_map = new MyMap2D(x);
StdDraw_Ex3.setScale(-0.5, _map.getHeight()-0.5);
StdDraw_Ex3.enableDoubleBuffering();
_map.fill(WHITE);
drawArray(_map);
}
public static void drawGrid(Map2D map) {
int w = map.getWidth();
int h = map.getHeight();
for(int i=0;i<w;i++) {
StdDraw_Ex3.line(i, 0, i, h);
}
for(int i=0;i<h;i++) {
StdDraw_Ex3.line(0, i, w, i);
}
}
static public void drawArray(Map2D a) {
StdDraw_Ex3.clear();
StdDraw_Ex3.setPenColor(Color.gray);
drawGrid(_map);
for(int y=0;y<a.getWidth();y++) {
for(int x=0;x<a.getHeight();x++) {
int c = a.getPixel(x, y);
StdDraw_Ex3.setPenColor(new Color(c));
drawPixel(x,y);
}
}
StdDraw_Ex3.show();
}
public static void actionPerformed(String p) {
_mode = p;
if(p.equals("Blue")) {_color = Color.BLUE; }
if(p.equals("White")) {_color = Color.WHITE; }
if(p.equals("Black")) {_color = Color.BLACK; }
if(p.equals("Red")) {_color = Color.RED; }
if(p.equals("Yellow")) {_color = Color.YELLOW; }
if(p.equals("Green")) {_color = Color.GREEN; }
if(p.equals("Clear")) { init(_map.getWidth());}
if(p.equals("20x20")) {init(20);}
if(p.equals("40x40")) {init(40);}
if(p.equals("80x80")) {init(80);}
if(p.equals("160x160")) {init(160);}
drawArray(_map);
}
// Defining a function that identifies the point based on the click in the system
// saves it for the performing actions
public static void mouseClicked(Point2D p) {
System.out.println(p);//print the point
int col = _color.getRGB();//placement
if(_mode.equals("Point")) { // if loop that check if the mode function is worth to the value point
_map.setPixel(p,col );
}
if(_mode.equals("Segment")) {
if(Prev_point==null) { // if loop that check if the last point is worth to noting
Prev_point = p; // if turn in to the loop than put in the last point the value p
_map.setPixel(p,col ); // placement
}else {
_map.setPixel(p,col );// placement
_map.drawSegment(Prev_point,p,col);
Prev_point = null;// placement last point to nothing
}
}
if(_mode.equals("Rect")) {
if(Prev_point==null) { // if loop that check if the placement of last point is nothing
_map.setPixel(p,col ); //placement
Prev_point = p; //placement
}else {
_map.drawRect(Prev_point,p,col); //placement
Prev_point = null; //placement the last point to noting
}
}
if(_mode.equals("Circle")) {
if(Prev_point==null) { // if loop check if the placement in the last point is noting
Prev_point = p;//placement the last point to the value p
}else {
_map.drawCircle(Prev_point,(int)Prev_point.distance(p),col); //placement
Prev_point = null;//placement the last point to the value of noting
}
}
if(_mode.equals("Fill")) { // if loop that check that the function mode is worth to fill
MyMap2D.P=p;//placement
System.out.println("the number of the Pixels are "+_map.fill(p,col));
_mode = "none"; //placement the mode to the value of noting
}
if(_mode.equals("ShortestPath")) {
if(Prev_point==null) { // if loop that check if the placement of the last point is the value of noting
MyMap2D.PshortestPath=p;//placement
Prev_point = p; //placement
}else {
MyMap2D.color=col; //placement
int shortestPathDist =_map.shortestPathDist(Prev_point,p);
if(shortestPathDist>0) { // if loop that check if the short way is above than 0
System.out.println("the length of the Shortest Path is "+shortestPathDist);
}else {
System.out.println("It is not possible to pass a road between these two points");
}
Prev_point = null;//placement the last point to the value noting
_mode = "none";//placement
}
}
if(_mode.equals("Gol")) {
_map.nextGenGol();
}
drawArray(_map);
}
static private void drawPixel(int x, int y) {
StdDraw_Ex3.filledCircle(x, y, 0.3);
}
}