Mega Code Archive

 
Categories / Java Tutorial / 2D Graphics
 

A reflected image

import java.awt.AlphaComposite; import java.awt.Color; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import javax.swing.JComponent; import javax.swing.JFrame; public class ImageReflection extends JComponent {   public void paintComponent(Graphics g) {     try {       BufferedImage image = ImageIO.read(new File("yourImage.jpg"));       Graphics2D g2d = (Graphics2D) g;       int width = getWidth();       int height = getHeight();       int imageWidth = image.getWidth();       int imageHeight = image.getHeight();       int gap = 20;       float opacity = 0.4f;       float fadeHeight = 0.3f;       g2d.translate((width - imageWidth) / 2, height / 2 - imageHeight);       g2d.drawRenderedImage(image, null);       g2d.translate(0, 2 * imageHeight + gap);       g2d.scale(1, -1);       BufferedImage reflection = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);       Graphics2D rg = reflection.createGraphics();       rg.drawRenderedImage(image, null);       rg.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));       rg.setPaint(new GradientPaint(0, imageHeight * fadeHeight, new Color(0.0f, 0.0f, 0.0f, 0.0f),           0, imageHeight, new Color(0.0f, 0.0f, 0.0f, opacity)));       rg.fillRect(0, 0, imageWidth, imageHeight);       rg.dispose();       g2d.drawRenderedImage(reflection, null);     } catch (Exception e) {     }   }   public static void main(String[] args) {     JFrame frame = new JFrame("Reflection");     ImageReflection r = new ImageReflection();     frame.add(r);     frame.setSize(300, 300);     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     frame.setLocationRelativeTo(null);     frame.setVisible(true);   } }