/
MainPanel.java
154 lines (138 loc) · 5.28 KB
/
MainPanel.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
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
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import javax.imageio.ImageIO;
import javax.swing.*;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new GridLayout(0, 1));
ClassLoader cl = Thread.currentThread().getContextClassLoader();
String path = "example/i03-10.gif";
Image image = Optional.ofNullable(cl.getResource(path)).map(url -> {
try (InputStream s = url.openStream()) {
return ImageIO.read(s);
} catch (IOException ex) {
return makeMissingImage();
}
}).orElseGet(MainPanel::makeMissingImage);
Icon icon = new ImageIcon(image);
JPanel p1 = new JPanel(new GridLayout(1, 2));
p1.add(makeLabel(makeGrayIcon1(image), icon, "ColorConvertOp"));
p1.add(makeLabel(makeGrayIcon2(image), icon, "TYPE_BYTE_GRAY"));
add(p1);
add(makeLabel(makeGrayIcon3(image), icon, "GrayFilter.createDisabledImage"));
JPanel p3 = new JPanel(new GridLayout(1, 2));
p3.add(makeLabel(makeGrayIcon4(image), icon, "GrayFilter(true, 50)"));
p3.add(makeLabel(makeGrayIcon5(image), icon, "GrayImageFilter"));
add(p3);
p1.setBackground(Color.WHITE);
p3.setBackground(Color.WHITE);
setBackground(Color.WHITE);
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
setPreferredSize(new Dimension(320, 240));
}
private static Image makeMissingImage() {
Icon missingIcon = UIManager.getIcon("OptionPane.errorIcon");
int iw = missingIcon.getIconWidth();
int ih = missingIcon.getIconHeight();
BufferedImage bi = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bi.createGraphics();
missingIcon.paintIcon(null, g2, (32 - iw) / 2, (32 - ih) / 2);
g2.dispose();
return bi;
}
private static JLabel makeLabel(Icon image, Icon orgImage, String str) {
JLabel label = new JLabel(str, image, SwingConstants.LEFT);
label.addMouseListener(new MouseAdapter() {
private boolean isGray;
@Override public void mouseClicked(MouseEvent e) {
JLabel l = (JLabel) e.getComponent();
l.setIcon(isGray ? image : orgImage);
isGray ^= true;
}
});
return label;
}
private static Icon makeGrayIcon1(Image img) {
int w = img.getWidth(null);
int h = img.getHeight(null);
BufferedImage source = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics g = source.createGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
ColorConvertOp ccOp = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
return new ImageIcon(ccOp.filter(source, null));
}
private static Icon makeGrayIcon2(Image img) {
int w = img.getWidth(null);
int h = img.getHeight(null);
BufferedImage destination = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
Graphics g = destination.createGraphics();
// // g.setColor(Color.WHITE);
// https://community.oracle.com/thread/1373262 Color to Grayscale to Binary
// g.fillRect(0, 0, w, h); // need to pre-fill(alpha?)
g.drawImage(img, 0, 0, null);
g.dispose();
return new ImageIcon(destination);
}
private static Icon makeGrayIcon3(Image img) {
// GrayFilter1
return new ImageIcon(GrayFilter.createDisabledImage(img));
}
private static Icon makeGrayIcon4(Image img) {
// GrayFilter2
ImageProducer ip = new FilteredImageSource(img.getSource(), new GrayFilter(true, 50));
return new ImageIcon(Toolkit.getDefaultToolkit().createImage(ip));
}
private static Icon makeGrayIcon5(Image img) {
// RGBImageFilter
ImageProducer ip = new FilteredImageSource(img.getSource(), new GrayImageFilter());
return new ImageIcon(Toolkit.getDefaultToolkit().createImage(ip));
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class GrayImageFilter extends RGBImageFilter {
// public GrayImageFilter() {
// canFilterIndexColorModel = false;
// }
@Override public int filterRGB(int x, int y, int argb) {
// int a = (argb >> 24) & 0xFF;
int r = (argb >> 16) & 0xFF;
int g = (argb >> 8) & 0xFF;
int b = argb & 0xFF;
int m = (2 * r + 4 * g + b) / 7; // NTSC Coefficients
// return new Color(m, m, m, a).getRGB();
return (argb & 0xFF_00_00_00) | (m << 16) | (m << 8) | m;
}
}