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 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| package test;
import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.RadialGradientPaint;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.event.ItemEvent;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
public class Main extends JPanel {
public enum DrawMethod {
NO_CLIP,
CLIP,
CLIP_SHAPE,
SRC_IN_HARD,
MASK_HARD,
SRC_IN_SOFT,
MASK_SOFT;
}
private static final Color TRANSPARENT_COLOR = new Color(0, 0, 0, 0);
private final BufferedImage image;
private final Shape clip;
private final Color hardMask = Color.BLACK;
private final RadialGradientPaint softMask = new RadialGradientPaint(150, 150, 150, new float[]{0, 0.5f, 1}, new Color[]{Color.BLACK, Color.BLACK, TRANSPARENT_COLOR});
public Main() throws IOException {
super();
setDrawMethod(DrawMethod.CLIP);
addPropertyChangeListener("drawMethod", propertyChangeEvent -> {
buffer = null;
repaint();
});
setAntialias(true);
addPropertyChangeListener("antialias", propertyChangeEvent -> {
buffer = null;
repaint();
});
image = ImageIO.read(getClass().getResource("ME0000867589_2.jpg"));
clip = new Ellipse2D.Float(0, 0, 300, 300);
}
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
final Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, getAntialias() ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
try {
final DrawMethod drawMethod = getDrawMethod();
switch (drawMethod) {
case CLIP: {
g2d.setClip(clip);
drawContent(g2d);
}
break;
case CLIP_SHAPE: {
g2d.setPaint(hardMask);
g2d.fill(clip);
}
break;
case SRC_IN_HARD:
case SRC_IN_SOFT: {
final Graphics2D gtemp = initializeBuffer();
initializeMask(gtemp, drawMethod);
gtemp.setComposite(AlphaComposite.SrcIn);
drawContent(gtemp);
gtemp.dispose();
drawBuffer(g2d);
}
break;
case MASK_HARD:
case MASK_SOFT: {
final Graphics2D gtemp = initializeBuffer();
initializeMask(gtemp, drawMethod);
gtemp.setComposite(AlphaComposite.SrcIn);
gtemp.dispose();
drawBuffer(g2d);
}
break;
case NO_CLIP:
default: {
drawContent(g2d);
}
}
} finally {
g2d.dispose();
}
}
private BufferedImage buffer;
private void drawContent(Graphics2D g2d) {
g2d.drawImage(image, 0, 0, null);
}
private void drawBuffer(Graphics2D g2d) {
g2d.drawImage(buffer, 0, 0, null);
}
private Graphics2D initializeBuffer() {
if (buffer == null) {
buffer = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
}
final Graphics2D g2d = buffer.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, getAntialias() ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
g2d.setColor(TRANSPARENT_COLOR);
g2d.fillRect(0, 0, image.getWidth(), image.getHeight());
return g2d;
}
private void initializeMask(Graphics2D g2d, DrawMethod drawMethod) {
final Paint paint = (drawMethod == DrawMethod.SRC_IN_HARD || drawMethod == DrawMethod.MASK_HARD) ? hardMask : softMask;
g2d.setPaint(paint);
g2d.fill(clip);
}
////////////////////////////////////////////////////////////////////////////
public final void setDrawMethod(final DrawMethod value) {
putClientProperty("drawMethod", value);
}
public final DrawMethod getDrawMethod() {
return (DrawMethod) getClientProperty("drawMethod");
}
public final void setAntialias(final boolean value) {
putClientProperty("antialias", value);
}
public final boolean getAntialias() {
return (Boolean) getClientProperty("antialias");
}
////////////////////////////////////////////////////////////////////////////
public static void main(String[] args) {
SwingUtilities.invokeLater(Main::createUI);
}
private static void createUI() {
try {
final Main main = new Main();
final JComboBox combo = new JComboBox(DrawMethod.values());
combo.setSelectedItem(main.getDrawMethod());
combo.addItemListener(itemEvent -> {
switch (itemEvent.getStateChange()) {
case ItemEvent.SELECTED:
final DrawMethod drawMethod = (DrawMethod) combo.getSelectedItem();
main.setDrawMethod(drawMethod);
break;
default:
}
});
final JCheckBox checkBox = new JCheckBox("Antialiasing");
checkBox.setSelected(main.getAntialias());
checkBox.addChangeListener(changeEvent -> {
main.setAntialias(checkBox.isSelected());
});
final JToolBar toolBar = new JToolBar();
toolBar.add(combo);
toolBar.add(checkBox);
toolBar.setFloatable(false);
final JFrame jframe = new JFrame("Test");
jframe.setLayout(new BorderLayout());
jframe.add(main, BorderLayout.CENTER);
jframe.add(toolBar, BorderLayout.NORTH);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setSize(500, 500);
jframe.setVisible(true);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
} |
Partager