-
Notifications
You must be signed in to change notification settings - Fork 18
/
ImageReadWritePanel.java
93 lines (76 loc) · 2.67 KB
/
ImageReadWritePanel.java
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
// sets up the GUI to embed an image
// works with ImageReadWrite.java
// import graphics
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageFilter;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import javax.swing.JPanel;
public class ImageReadWritePanel extends JPanel{
private BufferedImage image;
public ImageReadWritePanel(){
this.image = null;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
if(this.image != null){
// scale the image to the size of the window
double imageWidth = this.image.getWidth();
double imageHeight = this.image.getHeight();
double panelWidth = this.getWidth();
double panelHeight = this.getHeight();
double scaleX = 1.0;
double scaleY = 1.0;
if(imageWidth > imageHeight){
scaleX = panelWidth / imageWidth;
scaleY = scaleX;
}
else{
scaleY = panelHeight / imageHeight;
scaleX = scaleY;
}
// perform the scaling operation
AffineTransform transform = new AffineTransform();
transform.setToScale(scaleY, scaleY);
// convolution to affect the image
float[] kernelData = {
-0.125f, -0.125f, -0.125f,
-0.125f, 2.000f, -0.125f,
-0.125f, -0.125f, -0.125f
};
// create the kernel of the convolution
Kernel kernel = new Kernel(3, 3, kernelData);
// create the convolution operator
ConvolveOp operator = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
// create the filter
BufferedImageFilter filter = new BufferedImageFilter(operator);
// store filtered image
BufferedImage filteredImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
// apply the filter
operator.filter(image, filteredImage);
// set up and draw screen
g2d.setColor( Color.BLACK );
g2d.fillRect(0,0, (int) panelWidth, (int) panelHeight) ;
// prepare to draw image
AffineTransform scale = new AffineTransform() ;
scale.setToScale( panelWidth, panelHeight );
int xMin = image.getMinX();
int xMax = xMin + image.getWidth();
int yMin = image.getMinY();
int yMax = yMin + image.getHeight();
// draw image
g2d.drawImage(filteredImage, transform, this);
}
}
public void addImage(BufferedImage image){
this.image = image;
this.repaint();
}
}