Skip to content
MarkyVasconcelos edited this page Feb 24, 2011 · 2 revisions

With this class, it's possible to create screen on Swing with a background image and still has components over it.

To use:

  • Create an instance with one or more images;
  • Choose FillType, optional, this is the how the image will going to resize if the area is greater than itself, values are: RESIZE (grow), CENTER, SIDE_BY_SIDE (Like Windows desktop). For default the FillType is RESIZE;
  • Add it on a Container, like a JFrame for example;
  • Add components on it, exactly as JPanel;
  • Show it.
The following code do it: ```java import java.awt.Dimension; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException;

import javax.imageio.ImageIO; import javax.swing.JFrame;

import com.towel.swing.img.JImagePanel;

public class JImagePanelSingleTest { public static void main(String[] args) throws Throwable { JImagePanel panel = new JImagePanel( loadImage("/home/marcos/imgs/1.png"));

	JFrame frame = new JFrame();
	frame.setPreferredSize(new Dimension(100, 100));
	frame.add(panel);
	frame.pack();
	frame.setLocationRelativeTo(null);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
}

private static BufferedImage loadImage(String file) throws IOException {
	return ImageIO.read(new File(file));
}

}


With it we got a screen with the file 1.png as background and it's resized to the maximum of the screen.

To don't grow, set the FillType as this example:

```java
public class JImagePanelSingleTest {
	public static void main(String[] args) throws Throwable {
		JImagePanel panel = new JImagePanel(
				loadImage("/home/marcos/imgs/1.png"));

		panel.setFillType(JImagePanel.FillType.CENTER);

		JFrame frame = new JFrame();
		frame.setPreferredSize(new Dimension(100, 100));
		frame.add(panel);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	private static BufferedImage loadImage(String file) throws IOException {
		return ImageIO.read(new File(file));
	}
}

To use the loop mode, is just needed to use the constructor with needs a long and a image array, the first parameter is the time to change between each image, in milliseconds. When created, a Thread is fired to actualize the images, it's daemon and will not keep the program running if the main Thread finish. It's possible to use the FillType within the loop mode too.

The code is almost the same:

public class JImagePanelLoopTest {
	public static void main(String[] args) throws Throwable {
		JImagePanel panel = new JImagePanel(10, new BufferedImage[] {
				loadImage("/home/marcos/imgs/1.png"),
				loadImage("/home/marcos/imgs/2.png"),
				loadImage("/home/marcos/imgs/3.png"),
				loadImage("/home/marcos/imgs/4.png"),
				loadImage("/home/marcos/imgs/5.png"),
				loadImage("/home/marcos/imgs/6.png"),
				loadImage("/home/marcos/imgs/7.png"),
				loadImage("/home/marcos/imgs/8.png"),
				loadImage("/home/marcos/imgs/9.png"),
				loadImage("/home/marcos/imgs/10.png"),
				loadImage("/home/marcos/imgs/11.png"),
				loadImage("/home/marcos/imgs/12.png") });

		JFrame frame = new JFrame();
		frame.setPreferredSize(new Dimension(100, 100));
		frame.add(panel);
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	private static BufferedImage loadImage(String file) throws IOException {
		return ImageIO.read(new File(file));
	}
}

The result is an animation.

Clone this wiki locally