Wednesday, August 1, 2012
Today I am going to post a program to draw partially transparent image using Graphics2D in Java. In this example I am going to paint the background first by drawing ellipses with different colors .The image is broken into tiles using BufferedImage and each tile is given a separate transparency(alpha value) and then drawn above the background. Screenshots are given below
--------------------------------------------------------------------------------------------------------------------------
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
public class TransparentImage extends JPanel {
private BufferedImage mImage;
public static void main(String[] args) {
try {
String filename = "Image.jpeg";
JFrame f = new JFrame("TransparentImage v1.0");
TransparentImage showOff = new TransparentImage(filename);
f.getContentPane().add(showOff);
f.setSize(f.getPreferredSize());
f.setResizable(false);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setVisible(true);
}catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
public TransparentImage(String filename) throws Exception{
// Get the specified image.
InputStream in = getClass().getResourceAsStream(filename);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
mImage = decoder.decodeAsBufferedImage();
in.close();
// Set our size to match the image's size.
setPreferredSize(new Dimension((int)mImage.getWidth(), (int)mImage.getHeight()));
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// Turn on antialiasing
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
drawBackground(g2);
drawImageTile(g2);
}
private void drawBackground(Graphics2D g2) {
// Draw circles of different colors
int side = 45;
int width = getSize().width;
int height = getSize().height;
Color[] colors = { Color.yellow, Color.cyan, Color.orange,
Color.pink, Color.magenta, Color.lightGray };
for (int y = 0; y < height; y += side) {
for (int x = 0; x < width; x += side) {
Ellipse2D ellipse = new Ellipse2D.Float(x, y, side, side);
int index = (x + y) / side % colors.length;
g2.setPaint(colors[index]);
g2.fill(ellipse);
}
}
}
private void drawImageTile(Graphics2D g2) {
int side = 36;
int width = mImage.getWidth();
int height = mImage.getHeight();
for (int y = 0; y < height; y += side) {
for (int x = 0; x < width; x += side) {
// Calculate an appropriate transparency value
float xBias = (float)x / (float)width;
float yBias = (float)y / (float)height;
float alpha = 1.0f - Math.abs(xBias - yBias);
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha));
// Draw the subimage
int w = Math.min(side, width - x);
int h = Math.min(side, height - y);
BufferedImage tile = mImage.getSubimage(x, y, w, h);
g2.drawImage(tile, x, y, null);
}
}
// Reset the composite.
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
}
}
NOTE : in order to run this program perfectly you must have a Image.jpeg file in the same directory as that of your java file.
--------------------------------------------------------------------------------------------------------------------------
Change brightness of image using RescaleOp
Original Image |
Transparent Image |
SOURCE CODE
--------------------------------------------------------------------------------------------------------------------------import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import com.sun.image.codec.jpeg.*;
public class TransparentImage extends JPanel {
private BufferedImage mImage;
public static void main(String[] args) {
try {
String filename = "Image.jpeg";
JFrame f = new JFrame("TransparentImage v1.0");
TransparentImage showOff = new TransparentImage(filename);
f.getContentPane().add(showOff);
f.setSize(f.getPreferredSize());
f.setResizable(false);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
f.setVisible(true);
}catch (Exception e) {
System.out.println(e);
System.exit(0);
}
}
public TransparentImage(String filename) throws Exception{
// Get the specified image.
InputStream in = getClass().getResourceAsStream(filename);
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
mImage = decoder.decodeAsBufferedImage();
in.close();
// Set our size to match the image's size.
setPreferredSize(new Dimension((int)mImage.getWidth(), (int)mImage.getHeight()));
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
// Turn on antialiasing
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
drawBackground(g2);
drawImageTile(g2);
}
private void drawBackground(Graphics2D g2) {
// Draw circles of different colors
int side = 45;
int width = getSize().width;
int height = getSize().height;
Color[] colors = { Color.yellow, Color.cyan, Color.orange,
Color.pink, Color.magenta, Color.lightGray };
for (int y = 0; y < height; y += side) {
for (int x = 0; x < width; x += side) {
Ellipse2D ellipse = new Ellipse2D.Float(x, y, side, side);
int index = (x + y) / side % colors.length;
g2.setPaint(colors[index]);
g2.fill(ellipse);
}
}
}
private void drawImageTile(Graphics2D g2) {
int side = 36;
int width = mImage.getWidth();
int height = mImage.getHeight();
for (int y = 0; y < height; y += side) {
for (int x = 0; x < width; x += side) {
// Calculate an appropriate transparency value
float xBias = (float)x / (float)width;
float yBias = (float)y / (float)height;
float alpha = 1.0f - Math.abs(xBias - yBias);
g2.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, alpha));
// Draw the subimage
int w = Math.min(side, width - x);
int h = Math.min(side, height - y);
BufferedImage tile = mImage.getSubimage(x, y, w, h);
g2.drawImage(tile, x, y, null);
}
}
// Reset the composite.
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
}
}
NOTE : in order to run this program perfectly you must have a Image.jpeg file in the same directory as that of your java file.
--------------------------------------------------------------------------------------------------------------------------
RELATED POSTS
--------------------------------------------------------------------------------------------------------------------------Change brightness of image using RescaleOp
Labels:Graphics2D-Images
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
Links
Blog Archive
-
▼
2012
(
25
)
-
▼
August
(
8
)
- Differences between AWT and Swing
- How to set Java compiler's path in Windows7
- What is the difference between import and #include...
- What is a JIT compiler in Java ?
- Why cant you declare a class private ?
- Java program to create Bubbles using Graphics2D
- Rotate Text in Java using Graphics2D transform
- Java program to draw partially transparent image
-
▼
August
(
8
)
0 comments:
Post a Comment