JAVA: How To Put Background Image In Swings

At this time, me n my frnz are busy finishing our graduation projects…..the other day , one of my frnz approached me for a discussion as she was not able to PUT A BACKGROUND IMAGE IN A SWINGS WINDOW , so well i spent sum time mugging over it n now at 2.20 AM i get it [ ...Bingo..!!! :) ] ….here i am posting the same code so that it may serve handy for you…

APPROACH :

I’ve been playing around with an ImagePanel which allows you to add an image to a panel and then add components normally to the panel. The goal is to avoid extending every component to draw an image. It does this by setting any component to non-opaque as it is added to the panel. That way the background image shows through.

CODE:

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

public class ImagePanel extends JPanel
{
public static final int TILED = 0;
public static final int SCALED = 1;
public static final int ACTUAL = 2;

private BufferedImage image;
private int style;
private float alignmentX = 0.5f;
private float alignmentY = 0.5f;

public ImagePanel(BufferedImage image)
{
this(image, TILED);
}

public ImagePanel(BufferedImage image, int style)
{
this.image = image;
this.style = style;
setLayout( new BorderLayout() );
}

public void setImageAlignmentX(float alignmentX)
{
this.alignmentX = alignmentX > 1.0f ? 1.0f : alignmentX < 0.0f ? 0.0f : alignmentX;
}

public void setImageAlignmentY(float alignmentY)
{
this.alignmentY = alignmentY > 1.0f ? 1.0f : alignmentY < 0.0f ? 0.0f : alignmentY;

}

public void add(JComponent component)
{
add(component, null);
}

public void add(JComponent component, Object constraints)
{
component.setOpaque( false );

if (component instanceof JScrollPane)
{
JScrollPane scrollPane = (JScrollPane)component;
JViewport viewport = scrollPane.getViewport();
viewport.setOpaque( false );
Component c = viewport.getView();

if (c instanceof JComponent)
{
((JComponent)c).setOpaque( false );
}
}

super.add(component, constraints);
}

protected void paintComponent(Graphics g)
{
super.paintComponent(g);

if (image == null ) return;

switch (style)
{
case TILED :
drawTiled(g);
break;

case SCALED :
Dimension d = getSize();
g.drawImage(image, 0, 0, d.width, d.height, null);
break;

case ACTUAL :
drawActual(g);
break;
}
}

private void drawTiled(Graphics g)
{
Dimension d = getSize();
int width = image.getWidth( null );
int height = image.getHeight( null );

for (int x = 0; x < d.width; x += width)
{
for (int y = 0; y < d.height; y += height)
{
g.drawImage( image, x, y, null, null );
}
}
}

private void drawActual(Graphics g)
{
Dimension d = getSize();
float x = (d.width – image.getWidth()) * alignmentX;
float y = (d.height – image.getHeight()) * alignmentY;
g.drawImage(image, (int)x, (int)y, this);
}

public static void main(String [] args)
throws Exception
{
BufferedImage image = javax.imageio.ImageIO.read( new java.io.File(“c:\\aditi.jpg”) );

ImagePanel north = new ImagePanel(image, ImagePanel.ACTUAL);
north.setImageAlignmentY(1.0f);
JTextArea text = new JTextArea(5, 40);
JScrollPane scrollPane = new JScrollPane( text );
north.add( scrollPane );

ImagePanel south = new ImagePanel(image, ImagePanel.SCALED);
JPanel buttons = new JPanel();
buttons.add( new JButton(“One”) );
buttons.add( new JButton(“Two”) );
JPanel boxes = new JPanel();
boxes.add( new JCheckBox(“One”) );
boxes.add( new JCheckBox(“Two”) );
south.add(buttons, BorderLayout.NORTH);
south.add(boxes, BorderLayout.SOUTH);

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add( north, BorderLayout.NORTH );
frame.getContentPane().add( south, BorderLayout.SOUTH );
frame.pack();
frame.setVisible(true);
}
}

IMAGE NEEDED :

(paste this image in c: drive and save the code file as ImagePanel.java)

aditi.jpg
aditi.jpg

For any queries feel free to comment n ask me…

6 Responses

  1. Hi there Aditi, well the procedure u suggested in the above post is well n good ……. this code will help many of who confront such a prob..

  2. hi thr
    thnx for the code
    it surely helped me with the problem as to how to set two different pictures using different panels in same class

  3. thnx for the code
    i was in need of the same

  4. Hey Aditi,
    i was in a need of such code in my project,
    thanx a lot…

  5. thnx for the code dear, i was in need..

  6. hi..
    very thaks for this simple codings…..

Leave a Reply