-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPicture.java
More file actions
287 lines (262 loc) · 7.92 KB
/
Picture.java
File metadata and controls
287 lines (262 loc) · 7.92 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
287
import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.text.*;
import java.util.*;
import java.util.List; // resolves problem with java.awt.List and java.util.List
/**
* A class that represents a picture. This class inherits from SimplePicture and
* allows the student to add functionality to the Picture class.
*
* @author Barbara Ericson ericson@cc.gatech.edu
*/
public class Picture extends SimplePicture {
///////////////////// constructors //////////////////////////////////
/**
* Constructor that takes no arguments
*/
public Picture() {
/*
* not needed but use it to show students the implicit call to super() child
* constructors always call a parent constructor
*/
super();
}
/**
* Constructor that takes a file name and creates the picture
*
* @param fileName the name of the file to create the picture from
*/
public Picture(String fileName) {
// let the parent class handle this fileName
super(fileName);
}
/**
* Constructor that takes the width and height
*
* @param height the height of the desired picture
* @param width the width of the desired picture
*/
public Picture(int height, int width) {
// let the parent class handle this width and height
super(width, height);
}
/**
* Constructor that takes a picture and creates a copy of that picture
*
* @param copyPicture the picture to copy
*/
public Picture(Picture copyPicture) {
// let the parent class do the copy
super(copyPicture);
}
/**
* Constructor that takes a buffered image
*
* @param image the buffered image to use
*/
public Picture(BufferedImage image) {
super(image);
}
////////////////////// methods ///////////////////////////////////////
/**
* Method to return a string with information about this picture.
*
* @return a string with information about the picture such as fileName, height
* and width.
*/
public String toString() {
String output = "Picture, filename " + getFileName() + " height " + getHeight() + " width " + getWidth();
return output;
}
/** Method to set the blue to 0 */
public void zeroBlue() {
Pixel[][] pixels = this.getPixels2D();
for (Pixel[] rowArray : pixels) {
for (Pixel pixelObj : rowArray) {
pixelObj.setBlue(0);
}
}
}
/** Method to set the blue to 0 */
public void keepOnlyBlue() {
Pixel[][] pixels = this.getPixels2D();
for (Pixel[] rowArray : pixels) {
for (Pixel pixelObj : rowArray) {
pixelObj.setRed(0);
pixelObj.setGreen(0);
}
}
}
/** Method to set the blue to 0 */
public void negate() {
Pixel[][] pixels = this.getPixels2D();
for (Pixel[] rowArray : pixels) {
for (Pixel pixelObj : rowArray) {
pixelObj.setRed(255 - pixelObj.getRed());
pixelObj.setGreen(255 - pixelObj.getGreen());
pixelObj.setBlue(255 - pixelObj.getBlue());
}
}
}
/** Method to set the blue to 0 */
public void grayscale() {
Pixel[][] pixels = this.getPixels2D();
for (Pixel[] rowArray : pixels) {
for (Pixel pixelObj : rowArray) {
pixelObj.setRed((int) pixelObj.getAverage());
pixelObj.setGreen((int) pixelObj.getAverage());
pixelObj.setBlue((int) pixelObj.getAverage());
}
}
}
/**
* Method that mirrors the picture around a vertical mirror in the center of the
* picture from left to right
*/
public void mirrorVertical() {
Pixel[][] pixels = this.getPixels2D();
Pixel leftPixel = null;
Pixel rightPixel = null;
int width = pixels[0].length;
for (int row = 0; row < pixels.length; row++) {
for (int col = 0; col < width / 2; col++) {
leftPixel = pixels[row][col];
rightPixel = pixels[row][width - 1 - col];
rightPixel.setColor(leftPixel.getColor());
}
}
}
/** Mirror just part of a picture of a temple */
public void mirrorTemple() {
int mirrorPoint = 276;
Pixel leftPixel = null;
Pixel rightPixel = null;
int count = 0;
Pixel[][] pixels = this.getPixels2D();
// loop through the rows
for (int row = 27; row < 97; row++) {
// loop from 13 to just before the mirror point
for (int col = 13; col < mirrorPoint; col++) {
count++;
leftPixel = pixels[row][col];
rightPixel = pixels[row][mirrorPoint - col + mirrorPoint];
rightPixel.setColor(leftPixel.getColor());
}
}
System.out.println(count + " is the count.");
}
public void mirrorDiagonal() {
Pixel[][] pixels = this.getPixels2D();
int width = pixels.length;
int height = pixels[0].length;
double movement = pixels[0].length/(double)width;
HashMap<Integer, Double> coords = new HashMap<>();
for (double col = 0,row = 0; col < pixels[0].length&&row < pixels.length; col++,row++) {
coords.put((int)row, col);
}
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double k = coords.get(x);
if (y > (int)k)continue;
Pixel topPixel = pixels[(int) x][(int) y];
Pixel bottomPixel = pixels[(int) (width - 1 - x)][(int) (height - 1 - y)];
bottomPixel.setColor(topPixel.getColor());
}
}
}
public void mirrorArms() {
int mirrorPoint = 225; // this a row
Pixel upPixel = null;
Pixel downPixel = null;
int count = 0;
Pixel[][] pixels = this.getPixels2D();
// loop through the rows
for (int row = 157; row < mirrorPoint; row++) {
// loop from 13 to just before the mirror point
for (int col = 102; col < 170; col++) {
count++;
upPixel = pixels[row][col];
downPixel = pixels[row][mirrorPoint - col + mirrorPoint];
upPixel.setColor(downPixel.getColor());
}
for (int col = 239; col < 295; col++) {
count++;
upPixel = pixels[row][col];
downPixel = pixels[col][mirrorPoint - row + mirrorPoint];
downPixel.setColor(upPixel.getColor());
}
}
System.out.println(count);
}
/**
* copy from the passed fromPic to the specified startRow and startCol in the
* current picture
*
* @param fromPic the picture to copy from
* @param startRow the start row to copy to
* @param startCol the start col to copy to
*/
public void copy(Picture fromPic, int startRow, int startCol) {
Pixel fromPixel = null;
Pixel toPixel = null;
Pixel[][] toPixels = this.getPixels2D();
Pixel[][] fromPixels = fromPic.getPixels2D();
for (int fromRow = 0, toRow = startRow; fromRow < fromPixels.length
&& toRow < toPixels.length; fromRow++, toRow++) {
for (int fromCol = 0, toCol = startCol; fromCol < fromPixels[0].length
&& toCol < toPixels[0].length; fromCol++, toCol++) {
fromPixel = fromPixels[fromRow][fromCol];
toPixel = toPixels[toRow][toCol];
toPixel.setColor(fromPixel.getColor());
}
}
}
/** Method to create a collage of several pictures */
public void createCollage() {
Picture flower1 = new Picture("flower1.jpg");
Picture flower2 = new Picture("flower2.jpg");
this.copy(flower1, 0, 0);
this.copy(flower2, 100, 0);
this.copy(flower1, 200, 0);
Picture flowerNoBlue = new Picture(flower2);
flowerNoBlue.zeroBlue();
this.copy(flowerNoBlue, 300, 0);
this.copy(flower1, 400, 0);
this.copy(flower2, 500, 0);
this.mirrorVertical();
this.write("collage.jpg");
}
/**
* Method to show large changes in color
*
* @param edgeDist the distance for finding edges
*/
public void edgeDetection(int edgeDist) {
Pixel leftPixel = null;
Pixel rightPixel = null;
Pixel[][] pixels = this.getPixels2D();
Color rightColor = null;
for (int row = 0; row < pixels.length; row++) {
for (int col = 0; col < pixels[0].length - 1; col++) {
leftPixel = pixels[row][col];
rightPixel = pixels[row][col + 1];
rightColor = rightPixel.getColor();
if (leftPixel.colorDistance(rightColor) > edgeDist)
leftPixel.setColor(Color.BLACK);
else
leftPixel.setColor(Color.WHITE);
}
}
}
/*
* Main method for testing - each class in Java can have a main method
*/
public static void main(String[] args) {
Picture beach = new Picture("beach.jpg");
beach.explore();
beach.zeroBlue();
beach.explore();
}
} // this } is the end of class Picture, put all new methods before this