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
| import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.*;
import javax.swing.*;
public class TestClip extends JPanel implements ComponentListener, Runnable {
public enum Method {
NONE, CLIP, ALPHA_COMPOSITE, TEXTURE;
}
private BufferedImage subImage;
private Ellipse2D clip = new Ellipse2D.Double();
private Method method;
public TestClip(Method method) {
super();
setBackground(Color.RED);
this.method= method;
addComponentListener(this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (subImage != null) {
Graphics2D graphics = (Graphics2D)g.create();
graphics.draw(clip.getBounds2D());
if (subImage.getWidth() != getWidth()) {
graphics.translate(clip.getX(), clip.getY());
}
graphics.drawImage(subImage, 0, 0, null);
graphics.dispose();
}
g.drawString(method.toString(), 10, 10);
}
public void componentHidden(ComponentEvent event) {
}
public void componentMoved(ComponentEvent event) {
}
public void componentResized(ComponentEvent event) {
SwingUtilities.invokeLater(this);
}
public void run() {
int width = getWidth();
int height = getHeight();
//BufferedImage sourceImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
BufferedImage sourceImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
//LinearGradientPaint gradient = new LinearGradientPaint( 0, 0, width, height, new float[] {0, 1/3f, 2/3f, 1}, new Color[] {Color.WHITE, Color.CYAN, Color.BLUE, Color.BLACK});
LinearGradientPaint gradient = new LinearGradientPaint( 0, 0, width, height, new float[] {0, 1/3f, 2/3f, 1}, new Color[] {Color.WHITE, new Color(0, 255, 255, 0), Color.BLUE, Color.BLACK});
Graphics2D graphics = sourceImage.createGraphics();
graphics.setPaint(gradient);
graphics.fillRect(0, 0, width, height);
graphics.dispose();
if (method == Method.NONE) {
subImage = sourceImage;
clip.setFrame(0, 0, width, height);
}
else {
clip.setFrameFromCenter(width/2.0, height/2.0, 10, 10);
subImage = new BufferedImage((int)Math.ceil(clip.getWidth()), (int)Math.ceil(clip.getHeight()), BufferedImage.TYPE_INT_ARGB);
graphics = subImage.createGraphics();
graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics.translate(-clip.getX(), -clip.getY());
switch (method) {
case ALPHA_COMPOSITE: {
graphics.setColor(Color.WHITE);
graphics.fill(clip);
graphics.setComposite(AlphaComposite.SrcIn);
graphics.drawImage(sourceImage, 0, 0, null);
}
break;
case TEXTURE: {
graphics.setPaint(new TexturePaint(sourceImage, new Rectangle(width, height)));
graphics.fill(clip);
}
break;
case CLIP:
default: {
graphics.setClip(clip);
graphics.drawImage(sourceImage, 0, 0, null);
}
}
graphics.dispose();
}
repaint();
}
public void componentShown(ComponentEvent event) {
}
public static void main(String... args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
TestClip[] testClips = new TestClip[4];
testClips[0]= new TestClip(Method.NONE);
testClips[1]= new TestClip(Method.CLIP);
testClips[2]= new TestClip(Method.ALPHA_COMPOSITE);
testClips[3]= new TestClip(Method.TEXTURE);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(testClips.length/2, testClips.length/2));
for (TestClip testClip : testClips ) {
frame.add(testClip);
}
frame.setSize(500, 600);
frame.setVisible(true);
}
});
}
} |
Partager