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
| public class ExempleImageColor {
private static final String IMAGE = "suricate.jpg";
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIconLabel imagePanel = new ImageIconLabel(new ImageIcon(ExempleImageColor.class.getResource(IMAGE)));
JPanel colorPanel = new JPanel(new GridBagLayout());
JLabel label = new JLabel();
label.setHorizontalAlignment(JLabel.CENTER);
label.setPreferredSize(new Dimension(100, 100));
label.setMinimumSize(imagePanel.getPreferredSize());
colorPanel.add(label);
frame.add(imagePanel);
frame.add(colorPanel, BorderLayout.EAST);
imagePanel.addListener((x, y, color) -> colorPanel.setBackground(color));
imagePanel.addListener((x, y, color) -> label.setForeground(getContrastColor(color)));
imagePanel.addListener((x, y, color) -> label.setText(x + "," + y));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static Color getContrastColor(Color color) {
double y = (299 * color.getRed() + 587 * color.getGreen() + 114 * color.getBlue()) / 1000;
return y >= 128 ? Color.BLACK : Color.WHITE;
}
public static interface ImageIconLabelClickListener extends EventListener {
void clicked(int x, int y, Color color);
}
public static class ImageIconLabel extends JLabel {
private BufferedImage image;
private EventListenerList eventListener = new EventListenerList();
public ImageIconLabel(URL url) {
this(read(url));
}
private static BufferedImage read(URL url) {
try {
return ImageIO.read(url);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public ImageIconLabel(BufferedImage bufferedimage) {
super();
setImage(bufferedimage);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
Color color = image==null?null;new Color(getRGB(x, y));
Arrays.stream(eventListener.getListeners(ImageIconLabelClickListener.class)).forEach(l -> {
l.clicked(x, y, color);
});
}
});
}
public ImageIconLabel(Icon icon) {
this(toBufferedImage(icon));
}
private static BufferedImage toBufferedImage(Icon icon) {
BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
return image;
}
public void setImage(URL url) {
setImage(read(url));
}
public void setImage(BufferedImage image) {
this.image = image;
if ( image!=null ) {
super.setIcon(new ImageIcon(image));
}
else {
super.setIcon(null);
}
}
@Override
public void setIcon(Icon icon) {
if ( icon!=null ) {
setImage(toBufferedImage(icon));
}
else {
super.setIcon(null);
}
}
public int getRGB(int x, int y) {
return image.getRGB(x, y);
}
public void addListener(ImageIconLabelClickListener listener) {
removeListener(listener);
eventListener.add(ImageIconLabelClickListener.class, listener);
}
public void removeListener(ImageIconLabelClickListener listener) {
eventListener.remove(ImageIconLabelClickListener.class, listener);
}
}
} |
Partager