Free Online Courses for Software Developers - MrBool
× Please, log in to give us a feedback. Click here to login
×

You must be logged to download. Click here to login

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

×

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

How to display, flip and provide a reflection to the Image with Java

In this tutorial we will see how to display, flip and make a reflection into image with java

Talking about the Buffered Image, it plays a very significant role while working with images in Java and is often used to manipulate with images. The same is built up in memory in order to achieve efficiency. Let us see the process to implement the same.

  • We copy the image pixels into the Buffered Image,
  • Manipulate the pixels
  • Draw the result on the panel.

Displaying an Image

Here in the first example, we will basically display the image on the panel. Let’s see how we do this.

Listing 1: Displaying an image on the panel

DisplayImage.java

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DisplayImage extends JPanel {

   Image castle;
   Dimension size;

    public DisplayImage() {
        size = new Dimension();
        castle = new ImageIcon(this.getClass().getResource("bluesky.png")).getImage();
        size.width = castle.getWidth(null);
        size.height = castle.getHeight(null);
        setPreferredSize(size);
    }

    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        g2d.drawImage(castle, 0, 0, null);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("Blue Sky");
        frame.add(new DisplayImage());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Here we display the Blue Sky in our example on the panel.

Listing 2: Displaying the Blue Sky

castle = new ImageIcon(this.getClass().getResource("bluesky.png")).getImage();

We would be making use of the ImageIcon class in order to load the image.

Listing 3: Loading Image

g2d.drawImage(castle, 0, 0, null);

The drawImage() method helps to draws the image on the panel at the specified position. ImageObserver class is the last parameter which is more often than not used for asynchronous loading. One can just enter null there in case you do not need asynchronous loading of the images.

Blue Sky

Figure 1: Blue Sky

How to flip the image?

Let us see how the image can be flipped. We will be going to filter the image making use of afilter() method that is transforming images.

Listing 4: Flip image

ImageFlip.java

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class ImageFlip extends JPanel {

    Image castle;
    BufferedImage bufferedImage;

    public ImageFlip() {
        castle = new ImageIcon("slanec.jpg").getImage();
        bufferedImage =  new BufferedImage(castle.getWidth(null), 
                 castle.getHeight(null), BufferedImage.TYPE_INT_RGB);
    }

    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        Graphics gb = bufferedImage.getGraphics();
        gb.drawImage(castle, 0, 0, null);
        gb.dispose();

        AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
        tx.translate(-castle.getWidth(null), 0);
        AffineTransformOp op = new AffineTransformOp(tx, 
                                AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        bufferedImage = op.filter(bufferedImage, null);

        g2d.drawImage(castle, 10, 10, null);
        g2d.drawImage(bufferedImage, null, 290, 10);
    }

    public static void main(String[] args) {

        JFrame frame = new JFrame("Flip image");
        frame.add(new ImageFlip());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(570, 230);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Here in the above example, we basically flipped our image horizontally.

Listing 5: Horizontal flip

AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
 tx.translate(-castle.getWidth(null), 0);

What do you mean by flipping of an image? It is nothing but scaling it and translating it, hence we perform an AffineTransform operation.

Listing 6: AffineTransform operation

AffineTransformOp op = new AffineTransformOp(tx, 
                         AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
 bufferedImage = op.filter(bufferedImage, null)

We have this as one of the filtering operations available that could be also done by pixel manipulation. However Java offers high level classes that makes the job or task easy to manipulate images. Here in our example, the AffineTransformOp class performs scaling and translation on the image pixels.

Listing 7: Performs scaling

g2d.drawImage(castle, 10, 10, null);
 g2d.drawImage(bufferedImage, null, 290, 10);

Finally, both the images are drawn to see the difference.

Flipped image

Figure 2: Flipped image

How to display a reflected image?

Let us see the process to see a reflected image. This beautiful effect makes an illusion of the Egyptian temple in water.

Listing 8: Reflect Image

Reflection.java

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;


public class Reflection extends JComponent {

    private BufferedImage image;

    public Reflection() {
        try {
            image = ImageIO.read(new File("slanec.jpg"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void paintComponent(Graphics g) {

        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.setPaint(new GradientPaint(0, 0, Color.black, 0, height,
                                       Color.darkGray));
        g2d.fillRect(0, 0, width, height);
        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);
    }

    public Dimension getPreferredSize() {
        return new Dimension(320, 390);
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Reflection");
        Reflection r = new Reflection();
        frame.add(new Reflection());
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
Reflection

Figure 3: Reflection

Conclusion

The tutorial intended to cover the basics on display of the image, how to flip the image and ultimately display the reflection of an image.



Software Developer from India. I hold Master in Computer Applications Degree and is well versed with programming languages such as Java, .Net, C and C++ and possess good working knowledge on Mobile Platforms as well.

What did you think of this post?
Services
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually – in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits – in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login