-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixelGrid.java
More file actions
286 lines (235 loc) · 6.65 KB
/
Copy pathPixelGrid.java
File metadata and controls
286 lines (235 loc) · 6.65 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package week6;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import javax.imageio.ImageIO;
public class PixelGrid {
private ArrayList<ArrayList<Color>> grid;
private Stack<DeletedInfo> command;
private Random random;
/**
* this constructor stores color values of a buffered image
* from a png file and copies them into an arrayList of arrayList of colors
*
* @param filename
*/
public PixelGrid(String filename) {
// allocate memory for grid
readPixelGridFromFile(filename);
this.random = new Random();
}
/**
* this constructor has a built in seed
* in order to test random with a passed in png
* @param filename
* @param seed
*/
public PixelGrid(String filename, int seed) {
// allocate memory for grid
readPixelGridFromFile(filename);
this.random = new Random(seed);
}
/**
* this helper method makes the constructors less repetitive
* it stores color values of a buffered image from a png and copies
* it over to a grid
* @param filename
*/
private void readPixelGridFromFile(String filename) {
this.grid = new ArrayList<ArrayList<Color>>();
this.command = new Stack<DeletedInfo>();
try (Scanner scan = new Scanner(new File(filename))) {
File originalFile = new File(filename);
BufferedImage oldImg = ImageIO.read(originalFile);
for (int y = 0; y < oldImg.getHeight(); y++) { // rows
ArrayList<Color> row = new ArrayList<Color>(); // makes a new copy of row eveytime for loop
for (int x = 0; x < oldImg.getWidth(); x++) { // columns
int pixel = oldImg.getRGB(x, y);
// pixel is one value in grid
// Creating a Color object from pixel value
Color originalColor = new Color(pixel);
row.add(originalColor);
}
grid.add(row);
}
} catch (Exception ex) {
System.err.println("out of bounds exception");
ex.printStackTrace();
}
}
/**
* this is a constructor used for testing
* it is given a grid and a seed(for random testing)
* and makes a deep copy of and array list of array list of colors
* @param givenGrid
* @param seed
*/
public PixelGrid(ArrayList<ArrayList<Color>> givenGrid, int seed) {
this.grid = new ArrayList<ArrayList<Color>>();
this.command = new Stack<DeletedInfo>();
this.random = new Random(seed);
for (int i = 0; i < givenGrid.size(); i++) {
ArrayList<Color> copy = new ArrayList<Color>();
for (int j = 0; j < givenGrid.get(i).size(); j++) {
copy.add(givenGrid.get(i).get(j));
}
grid.add(copy);
}
}
/**
* this method finds the column in the grid that
* has the biggest sum of blue value
* @return
*/
public int findBluest() {
int column = 0;
int bluestCol = 0;
for (int col = 0; col < grid.get(0).size(); col++) {
int blueSum = 0;
for (int row = 0; row < grid.size(); row++) {
blueSum += grid.get(row).get(col).getBlue();
}
if (blueSum > bluestCol) {
bluestCol = blueSum;
column = col;
}
}
return column;
}
/**
* this method first saves the current command that the user inputted
* into a stack,
* then changes the color of a column passed in
* and changes the column to all blue or red pixels
* based on the boolean input
*
* @param col
* @param isBlue
*/
public void changeColor(int col, boolean isBlue) {
if (!grid.isEmpty()) {
ArrayList<Color> column = new ArrayList<Color>();
for (int c = 0; c < grid.get(0).size(); c++) {
for (int row = 0; row < grid.size(); row++) {
column.add(grid.get(row).get(col));
}
}
command.push(new DeletedInfo(col, column));
if (isBlue) {
for (int i = 0; i < grid.size(); i++) {
this.grid.get(i).set(col, Color.BLUE);
}
} else {
for (int i = 0; i < grid.size(); i++) {
this.grid.get(i).set(col, Color.RED);
}
}
}
}
/**
* this method feeds values of the pixelgrid into a buffered image
* then saves it to a particular name based on the boolean
* @param counter
* @param quit
*/
public void save(int counter, boolean quit) {
BufferedImage newImg = new BufferedImage(grid.get(0).size(), grid.size(), BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < grid.size(); x++) {
for (int y = 0; y < grid.get(0).size(); y++) {
newImg.setRGB(y, x, grid.get(x).get(y).getRGB());
}
}
if (quit == false) {
File newFile = new File("temp_0" + counter + "x.png");
try {
ImageIO.write(newImg, "png", newFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
File newFile = new File("newIMG.png");
try {
ImageIO.write(newImg, "png", newFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* this method makes chooses a random int, which highlights
* a random column in another function
* @return
*/
public int random() {
int x = this.random.nextInt(grid.size() - 1);
return x;
}
/**
* this method pops the previous saved stack of commands
*/
public void undoDelete() {
if (!command.isEmpty()) {
DeletedInfo prevCommand = command.pop();
int indexToAddBack = prevCommand.getIndex();
ArrayList<Color> colorsToAddBack = prevCommand.getColumn();
int numRow = this.grid.size();
for (int r = 0; r < numRow; r++) {
this.grid.get(r).add(indexToAddBack, colorsToAddBack.get(r));
}
System.out.println("there are no commands to undo");
}
}
/**
* this method removes a column from the grid
* @param col
*/
public void remove(int col) {
for (int i = 0; i < grid.size(); i++) {
this.grid.get(i).remove(col);
}
}
/*
* this method produces a string representation
* of a arraylist of arraylist of colors
*
* copied from integerGridLL
*/
@Override
public String toString() {
StringBuilder printList = new StringBuilder();
for (int i = 0; i < grid.size(); i++) {
String inner = grid.get(i).toString();
printList.append(inner);
}
return printList.toString();
}
/*
* main function to test and print
*/
public static void main(String[] args) {
ArrayList<ArrayList<Color>> pg = new ArrayList<ArrayList<Color>>();
ArrayList<Color> inner1 = new ArrayList<Color>();
inner1.add(Color.BLUE);
inner1.add(Color.GREEN);
inner1.add(Color.GREEN);
ArrayList<Color> inner2 = new ArrayList<Color>();
inner2.add(Color.BLUE);
inner2.add(Color.RED);
inner2.add(Color.BLUE);
ArrayList<Color> inner3 = new ArrayList<Color>();
inner3.add(Color.GREEN);
inner3.add(Color.RED);
inner3.add(Color.GREEN);
pg.add(inner1);
pg.add(inner2);
pg.add(inner3);
System.out.println(pg.toString());
}
}