-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageEditModel.java
More file actions
142 lines (119 loc) · 3.68 KB
/
Copy pathImageEditModel.java
File metadata and controls
142 lines (119 loc) · 3.68 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
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.IOException;
import javax.swing.undo.AbstractUndoableEdit;
import javax.swing.undo.CannotRedoException;
import javax.swing.undo.CannotUndoException;
import javax.swing.undo.UndoManager;
import javax.swing.undo.UndoableEdit;
public class ImageEditModel {
private BufferedImage image;
private UndoManager undoManager = new UndoManager();
public ImageEditModel(String chemin) {
try {
image = ImageIO.read(new File(chemin));
} catch (IOException e) {
e.printStackTrace();
}
}
public void fillZone(Rectangle z, int[][] pixels) {
int xStart = (int) z.getX();
int yStart = (int) z.getY();
int width = (int) z.getWidth();
int height = (int) z.getHeight();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixelValue = pixels[y][x];
image.setRGB(xStart + x, yStart + y, pixelValue);
}
}
}
public void clearZone(Rectangle z) {
Color color = Color.WHITE;
int srgb = color.getRGB();
int width = (int) z.getWidth();
int height = (int) z.getHeight();
int[][] pixels = new int[height][width];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
pixels[y][x] = srgb;
}
}
fillZone(z, pixels);
}
public BufferedImage getImage() {
return image;
}
public void updateImage(BufferedImage newImage) {
image = newImage;
}
public void saveCut(Rectangle z) {
BufferedImage subImage = image.getSubimage((int) z.getX(), (int) z.getY(), (int) z.getWidth(), (int) z.getHeight());
Coupe c = new Coupe((int) z.getX(), (int) z.getY(), (int) z.getWidth(), (int) z.getHeight(), subImage);
c.doit();
UndoableEdit cutEdit = new CutEdit(c);
undoManager.addEdit(cutEdit);
}
public boolean canUndo() {
return undoManager.canUndo();
}
public boolean canRedo() {
return undoManager.canRedo();
}
public void performUndo() {
try {
if (undoManager.canUndo()) {
undoManager.undo();
}
} catch (CannotUndoException ex) {
ex.printStackTrace();
}
}
public void performRedo() {
try {
if (undoManager.canRedo()) {
undoManager.redo();
}
} catch (CannotRedoException ex) {
ex.printStackTrace();
}
}
private class Coupe {
private Rectangle z;
private int[][] pixels;
public Coupe(int x, int y, int width, int height, BufferedImage image) {
z = new Rectangle(x, y, width, height);
pixels = new int[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
pixels[i][j] = image.getRGB(x + j, y + i);
}
}
}
public void doit() {
clearZone(z);
}
public void undo() {
fillZone(z, pixels);
}
}
private class CutEdit extends AbstractUndoableEdit {
private Coupe c;
public CutEdit(Coupe coupe) {
c = coupe;
}
@Override
public void undo() throws CannotUndoException {
super.undo();
c.undo();
}
@Override
public void redo() throws CannotRedoException {
super.redo();
c.doit();
}
}
}