-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImagePanel.java
More file actions
64 lines (59 loc) · 1.79 KB
/
ImagePanel.java
File metadata and controls
64 lines (59 loc) · 1.79 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
/**
* A JPanel component for displaying a buffered image, and for
* applying filters to the buffered image by generating a PixelImage,
* applying filters, and getting a new buffered image to display from the PixelImage
*
* @author Richard Dunn, Tim Gesell, modified by Pranav Kadekodi
* @version November 5th 2023
*/
import javax.swing.JPanel;
import javax.imageio.ImageIO;
import java.io.File;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.Graphics;
public class ImagePanel extends JPanel {
private BufferedImage bi;
private SnapShop s;
public ImagePanel(SnapShop s) {
bi = null;
this.s = s;
}
public void loadImage(File imageFile) {
BufferedImage img = null;
try
{
img = ImageIO.read(imageFile);
}
catch (java.io.IOException e)
{
e.printStackTrace();
return;
}
int width = img.getWidth(this);
int height = img.getHeight(this);
bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
biContext.drawImage(img, 0, 0, null);
setPreferredSize(new Dimension(bi.getWidth(), bi.getHeight()));
revalidate();
s.pack();//resizes SnapShop JFrame to fit the new picture
s.repaint();//repaints SnapShopJFrame to show the new picture
}
public void paint(Graphics g) {
super.paint(g);
if (bi != null) {
g.drawImage(bi, 0, 0, this);
}
}
public void applyFilter(Filter f) {
if (bi == null) {
return;
}
PixelImage newImage = new PixelImage(bi);
f.filter(newImage);
bi = newImage.getImage();
repaint();
}
}