A reflected image: effect makes an illusion as if the image was reflected in water : Image « Advanced Graphics « Java






A reflected image: effect makes an illusion as if the image was reflected in water

  

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);
  }
}

   
    
  








Related examples in the same category

1.Create an image that does not support transparency
2.Create an image that supports transparent pixels
3.Create an image that supports arbitrary levels of transparency
4.Creating a buffered image using Component.createImage().
5.Creating a Buffered Image from an Image
6.Drawing on a Buffered Image
7.If the buffered image supports transparency
8.Converting a Buffered Image (BufferedImage) from an Image
9.Getting and Setting Pixels in a Buffered Image
10.Scaling a Buffered Image
11.Shearing a Buffered Image
12.Translating a Buffered Image
13.Rotating a Buffered Image
14.Flipping a Buffered Image
15.Flip the image horizontally
16.Flip the image vertically and horizontally, equivalent to rotating the image 180 degrees
17.2D Image Draw
18.Transform image
19.Creating a Image Zoomer using Graphics2D
20.Gaussian Blur DemoGaussian Blur Demo
21.Intermediate ImagesIntermediate Images
22.Image reflectionImage reflection
23.Bloom DemoBloom Demo
24.Box Blur DemoBox Blur Demo
25.Brightness Increase DemoBrightness Increase Demo
26.Blur an ImageBlur an Image
27.Blur our image: Blur means an unfocused image
28.Enlarge Image With AnimationEnlarge Image With Animation
29.Image ZoomingImage Zooming
30.Simple image handling and drawing interactionSimple image handling and drawing interaction
31.Unsharp Mask DemoUnsharp Mask Demo
32.Create a grayscale image with Java 2D tools
33.A 3x3 kernel that embosses an image.
34.A 3x3 kernel that blurs an image.
35.A 3x3 kernel that sharpens an image.
36.Embossing a Buffered Image
37.Brighten the image by 30%
38.Darken the image by 10%
39.Extend RGBImageFilter to create ColorFilter class
40.Extend RGBImageFilter to create AlphaFilter class
41.Enlarging an image by pixel replication
42.Shrinking an image by skipping pixels