Wednesday, April 4, 2012

Adding Image above another Image using the class Toolkit and Graphics2D

Program Description:

Today I have created an easy and short Java program on how to add image above another image using the class Toolkit and Graphics2D. This technique is very useful in creating maps, games, applications that involve layers, and so on. There are many java programs out there which is similar to this but some of the programs contain codes and techniques that are hard to understand and some of them doesn't run, so I created this program to give you a more simple and easy way on how to put image above another image.

In this program, you will also learn how to link two programs that have different extensions.

Download: imageAboveAnotherImage.rar

Output:
Code:

Main Program (JFrame)

/**
* File: imageAboveAnotherImage.java
* Tiltle: Adding Image Above Another Image
* Author: http://java-code-complete.blogspot.com/
*/

//Java Core Package
import javax.swing.*;

//Java Extension Package
import java.awt.*;

public class imageAboveAnotherImage extends JFrame {

//Initializing the class pluginExt
private pluginExt px;

//Setting up GUI
public imageAboveAnotherImage() {

//Setting up the Title of the Window
super("Adding Image Above Another Image");

//Set Size of the Window (WIDTH, HEIGHT)
setSize(455,540);

//Exit Property of the Window
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Constructing the class pluginExt
px = new pluginExt(this);

//Setting up the container ready for the components to be added.
Container pane = getContentPane();

//Adding the class pluginExt in the container so it will be visible when running the program
pane.add(px);

//Setting up the content of the container using the class "pluginExt"
setContentPane(px);

//Lock the size of the window
setResizable(false);

/**Set all the Components Visible.
* If it is set to "false", the components in the container will not be visible.
*/
setVisible(true);

}

//Main Method
public static void main(String[] args) {

imageAboveAnotherImage iaai = new imageAboveAnotherImage();

}
}

Extension Program (JPanel)

/**
* File: pluginExt.java
* Tiltle: Adding Image Above Another Image (class extention)
* Author: http://java-code-complete.blogspot.com/
*/

//Java Core Package
import javax.swing.*;

//Java Extension Package
import java.awt.*;

public class pluginExt extends JPanel {

//Initializing the class "imageAboveAnotherImage"
imageAboveAnotherImage iaai;

//Initializing the images to be displayed
Image image1, image2, image3;

//Setting up GUI
public pluginExt(imageAboveAnotherImage iaai) {

//Constructing the class "Toolkit" which will be used to manipulate our images.
Toolkit kit = Toolkit.getDefaultToolkit();

//Getting all 3 images we have in the folder
image1 = kit.getImage("image1.jpg");
image2 = kit.getImage("image2.jpg");
image3 = kit.getImage("image3.gif");

}

//Manipulate Images with JAVA2D API. . creating a paintComponent method.
public void paintComponent(Graphics comp) {

//Constructing the class Graphics2D. Create 2D by casting the "comp" to Graphics2D
Graphics2D comp2D = (Graphics2D)comp;

//creating a graphics2d using the images in the folder and place them in a specific coordinates.
comp2D.drawImage(image1, 0, 0, this);
comp2D.drawImage(image2, 100, 150, this);
comp2D.drawImage(image3, 200, 350, this);

}
}

No comments:

Post a Comment