Background image with Swing
Hi all,
I'm trying to make a little Strategy game. Now i have to set a background who won't change at all.
So i was thinking: "Maybe i could set the image at the Background and set the characters at the Foreground so they 'walk' over the background!".
But i don't know about this is possible!
Could anyone help me with this or give another helpfull idea?
Regards,
Jasper
[418 byte] By [
Jasper91a] at [2007-11-26 16:26:18]

# 3
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class WalkOnImage extends JPanel {
BufferedImage offscreenImage;
Ellipse2D.Double ball;
protected void paintComponent(Graphics g) {
if(offscreenImage == null) init();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawImage(offscreenImage, 0, 0, this);
g2.setPaint(Color.red);
g2.fill(ball);
}
public Dimension getPreferredSize() {
return new Dimension(400,400);
}
private void init() {
int w = getWidth();
int h = getHeight();
int type = BufferedImage.TYPE_INT_RGB;
offscreenImage = new BufferedImage(w, h, type);
Graphics2D g2 = offscreenImage.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setBackground(getBackground());
g2.clearRect(0,0,w,h);
g2.setPaint(Color.green.darker());
double dia = Math.min(w, h)/3.0;
g2.fill(new Ellipse2D.Double(w/2-dia/2, h/2-dia/2, dia, dia));
g2.dispose();
ball = new Ellipse2D.Double(50, 50, dia/2, dia/2);
}
public static void main(String[] args) {
WalkOnImage test = new WalkOnImage();
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().add(test);
f.pack();
f.setLocation(200,200);
f.setVisible(true);
}
}