Jframe background for game problem

i want to make a background image on Jframe.

and i want the background image change like a game (menu page to play game page)?

How can i make a background image on Jframe?

and if i want to make the above effect. what thing i should now about it?

are there any simple method to do this?

do anyone know some tutorial about that?

because i want to understand how it works.Not just copy the code post by anyone.

thanks for coming to teach me.

i am sorry that my english is not very well.hope you guy understand my meaning.

[573 byte] By [nickgoldgodla] at [2007-11-27 4:07:12]
# 1

i want to make a background image on Jframe.

and i want the background image change like a game (menu page to play game page)?

How can i make a background image on Jframe?

Here's one way you could do it.

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class BackgroundImageTest implements ActionListener {

BackgroundImagePanel contentPane;

public void actionPerformed(ActionEvent e) {

contentPane.showNextImage();

}

private JPanel getLast() {

JButton button = new JButton("next image");

button.addActionListener(this);

JPanel panel = new JPanel();

panel.add(button);

return panel;

}

public static void main(String[] args) throws IOException {

String[] names = { "Bird", "Dog", "Cat" };

BufferedImage[] images = new BufferedImage[names.length];

for(int j = 0; j < images.length; j++) {

String path = "images/" + names[j] + ".gif";

images[j] = ImageIO.read(new File(path));

}

BackgroundImageTest test = new BackgroundImageTest();

test.contentPane = new BackgroundImagePanel(images);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setContentPane(test.contentPane);

System.out.println("contentPane layout manager = " +

test.contentPane.getLayout().getClass().getName());

f.getContentPane().add(test.getLast(), "Last");

f.setSize(300,300);

f.setLocation(200,200);

f.setVisible(true);

}

}

class BackgroundImagePanel extends JPanel {

BufferedImage[] images;

int imageIndex = 0;

public BackgroundImagePanel(BufferedImage[] images) {

this.images = images;

setOpaque(true);// required for content panes

setLayout(new BorderLayout()); // default for content pane

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int w = getWidth();

int h = getHeight();

// Center the current image.

int x = (w - images[imageIndex].getWidth())/2;

int y = (h - images[imageIndex].getHeight())/2;

g.drawImage(images[imageIndex], x, y, this);

}

public void showNextImage() {

imageIndex++;

if(imageIndex > images.length-1)

imageIndex = 0;

repaint();

}

}

Images found near bottom of [url=http://java.sun.com/docs/books/tutorial/uiswing/examples/components/index.html]Using Swing Components: Examples[/url].

and if i want to make the above effect. what thing i should now about it?

are there any simple method to do this?

do anyone know some tutorial about that?

Resources in the [url=http://java.sun.com/docs/books/tutorial/index.html]java tutorial[/url] include:

1 — [url=http://java.sun.com/docs/books/tutorial/2d/index.html]Trail: 2D Graphics[/url], especially the [url=http://java.sun.com/docs/books/tutorial/2d/images/index.html]Lesson: Working with Images[/url].

2 — [url=http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html]How to Use Icons[/url] — discusses loading images.

crwooda at 2007-7-12 9:12:28 > top of Java-index,Security,Cryptography...
# 2
thanks again
nickgoldgodla at 2007-7-12 9:12:28 > top of Java-index,Security,Cryptography...