Tag: awt

Creating transparent and shaped windows using Java Swing/AWT

The Java AWT offer various possibilities about window manipulation. Today I’ll talk about two possibilities using class AWTUtilities.

Building a transparent window

To make a Window (JFrame, JDialog …) with alpha effect, you should invoke the method AWTUtilities.setWindowOpacity. This method parameters are: the window to apply transparency and transparency degree, that can be between 0 (zero) and 1, being 0 invisible and 1 totally visible.

JFrame  window = new JFrame("My Window");

//70% of transparency
AWTUtilities.setWindowOpacity(window, .7f);

The result will be:

Changing window shape

To change the window shape you should use the method AWTUtilities.setWindowShape. This method parameters are: window that will be changed and the new shape (java.awt.Shape) of the window.

The most efficiently form to use is to implement the method componentResized(), cause you may recompute the window and components size

Here I’ll use a triangle with 70% of transparency.

final JFrame  window = new JFrame("My Window");

try {
	//Add the ComponentListener to implement componentResized method
	window.addComponentListener(new ComponentAdapter(){
		//building componentResized
		public void componentResized(ComponentEvent e) {
			int[] x = {0,400,800}; //Pontos X do polígono
			int[] y = {600,0,600}; //Pontos Y do polígono

			//Triangle with 800 w x 600 h
			Shape shape = new Polygon(x, y, 3);

			AWTUtilities.setWindowShape(window, shape);

			//70% of transparency
			AWTUtilities.setWindowOpacity(window, 0.7f);
} catch (SecurityException e) {
	// TODO Auto-generated catch block
} catch (IllegalArgumentException e) {
	// TODO Auto-generated catch block

window.setUndecorated(true); //removing title bar

Notice that the method setUndecorated( ) was invoked with true as value. This method are responsible by hide the title bar (one with icon and the maximize, minimize and close buttons). This is really necessary, because with title bar visible the window are not be shaped.

The result:

I hope you enjoy… 😉

Taking Screen Shots with Java

Here I’ll show how to implements a class to take Screen shots.

I thinking about the complexity of a class that takes screen shots and store the files in hard disk and, asking to “uncle G”, I fonded the class Robot, that provide createScreenCapture method.

Now I’ll show how to implement this functionality:

Robot robot = new Robot();
//Setting the rectangle that mark capture area. In this case, will be all screen..
Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

BufferedImage img = robot.createScreenCapture(rect);

Here we defined capture area and obtained a BufferedImage, our image. Now, we needed to persist in hard disk.

//Capturing the ImageWriter and ImageWriterParam
ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam iwp = writer.getDefaultWriteParam();

//Setting compression mode and the image quality

//Persisting the image
writer.setOutput(new FileImageOutputStream(arquivo));

IIOImage iioimage = new IIOImage(img, null, null);

writer.write(null, iioimage, iwp);

We captured the ImageWriter and ImageWriterParam to set the compression method and the image quality.

In line 07 we defined the image quality as 1, where the value can be between 0 (zero), more compression and less quality and 1 (one), less compression and more quality. Then we have kept the file in HD.

We’ve done! Simple, isn’t?

Download this sample here.

See ya!

  • Advertisement

  • Advertisement

  • Copyright © 1996-2010 André L. S.. All rights reserved.
    iDream theme by Templates Next | Powered by WordPress