-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlip.java
More file actions
64 lines (56 loc) · 1.67 KB
/
Flip.java
File metadata and controls
64 lines (56 loc) · 1.67 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
/****************************************************************************
* Flavio Andrade
* 4-27-2017
* Compilation: javac Flip.java
* Execution: java Flip Images/Ferrari.jpeg
Use this class to flip and image horizontally or vertically.
*****************************************************************************
*/
import java.awt.image.BufferedImage;
public class Flip {
public BufferedImage image, newImage;
public Rotate rot;
public String name;
public int width, height, diagonal;
// Construct a new Flip object by reading an image file.
public Flip (String filename) {
name = filename;
rot = new Rotate(name);
diagonal = rot.diagonal;
width = rot.width;
height = rot.height;
}
/* Flip the image horizontally or vertically. True for horizontal flip,
false for vertical.
*/
public void flip(boolean a) {
newImage = new BufferedImage(diagonal + 10, diagonal + 10, BufferedImage.TYPE_INT_RGB);
int x = 0;
int y = 0;
for (int h = 0; h < height; h++) {
for (int c = 0; c < width; c++) {
x = c - width / 2;
y = h - height / 2;
int color = rot.image.getRGB(c, h);
// Flip horizontal, else flip vertically.
if (a)
newImage.setRGB(x + (diagonal + 10) / 2, -y + (diagonal + 10) / 2, color);
else {
newImage.setRGB(-x + (diagonal + 10) / 2, y + (diagonal + 10) / 2, color);
}
}
}
}
// Save the image to the current directory.
public void save(String name) {
rot.gray.save(this.newImage, name);
}
// Test case.
public static void main(String[] args) {
String name = args[0];
Flip flip = new Flip(name);
flip.flip(true);
flip.rot.showImage(flip.newImage);
flip.save("flipped");
}
}