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
| /**
*
* @author fabriceb
*/
public class Main extends JComponent {
private LinearGradientPaint skyGradient = new LinearGradientPaint(0, 0, 0, 500, new float[]{0.6f, 0.98f, 1f}, new Color[]{Color.BLUE, Color.CYAN, Color.WHITE});
private LinearGradientPaint houseTexture = new LinearGradientPaint(0, 0, 0, 500, new float[]{0f, 1f}, new Color[]{Color.ORANGE, Color.YELLOW});
private Color transparent = new Color(0, 0, 0, 0);
private BufferedImage offscreenImage = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
public Main() {
setPreferredSize(new Dimension(500, 500));
setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
updateImage();
addMouseListener(new MouseAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void mouseClicked(MouseEvent event) {
int x = event.getX();
int y = event.getY();
mask.subtract(new Area(new Ellipse2D.Float(x - 25, y - 25, 50, 50)));
updateImage();
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
/**
* {@inheritDoc}
*/
@Override
public void mouseDragged(MouseEvent event) {
int x = event.getX();
int y = event.getY();
mask.subtract(new Area(new Ellipse2D.Float(x - 25, y - 25, 50, 50)));
updateImage();
repaint();
}
});
}
private Area mask = new Area(new Rectangle(50, 50, 400, 500));
private void clearBuffer() {
int w = offscreenImage.getWidth();
int h = offscreenImage.getHeight();
Graphics2D g2d = (Graphics2D) offscreenImage.createGraphics();
try {
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, w, h);
g2d.setComposite(AlphaComposite.SrcIn);
g2d.setColor(transparent);
g2d.fillRect(0, 0, w, h);
} finally {
g2d.dispose();
}
}
private void updateImage() {
clearBuffer();
Graphics2D g2d = (Graphics2D) offscreenImage.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.BLACK);
g2d.fill(mask);
g2d.setComposite(AlphaComposite.SrcIn);
g2d.setPaint(houseTexture);
g2d.fill(mask);
}
/**
* {@inheritDoc}
*/
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
try {
g2d.setPaint(skyGradient);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.drawImage(offscreenImage, 0, 0, null);
} finally {
g2d.dispose();
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(main, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
} |
Partager