I currently have a background image that stretches to the whole screen, and a smaller image in front. I want the foreground image to move across the screen. If anyone could provide an example, or help fix my problem, that would be great. So far, I have the following code:
public Video(BufferedImage foreground, BufferedImage background){
pane = this.getContentPane(); //get the content pane to place components
pane.setLayout(null); //use absolute positioning (using Insets)
//set up frame
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setVisible(true);
this.setBounds(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
//set the bounds of the layered pane
lpane.setBounds(0, 0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
this.add(lpane, BorderLayout.CENTER);
//instantiating the panels
this.backgroundPanel = new ImagePanel(background);
this.foregroundPanel = new ImagePanel(foreground);
//set bounds of the panel
backgroundPanel.setBounds(0, 0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
//foregroundPanel.setBounds(200, 100,350,350);
//add the panels to the layered pane
lpane.add(this.backgroundPanel, new Integer(0), 0);
PongPanel p = new PongPanel();
p.setBounds(0, 0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
p.setOpaque(false);
lpane.add(p, new Integer(1), 0);
p.start();
// lpane.add(foregroundPanel, new Integer(1), 0);
// lpane.add(new KeyBidings());
// Pong p = new Pong();
// p.setVisible(true);
// p.start();
this.foregroundPanel.setLocation(500,250);
this.waitFor(5);
this.foregroundPanel.setLocation(250,250);
//this.showImageAt(100,0);
// int width = this.foregroundPanel.getBounds().width;
// int height = this.backgroundPanel.getBounds().height;
// for(int k=10; k<500; k++){
// this.showImageAt(k,0);
// this.waitFor(5);
// }
}
The following class draws a red ball and it slides across the screen. However, When I add this panel it fills the surrounding space with a white background, and I can't see the original background image. Basically I just want the red ball to slide across the screen in front of the background image.
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PongPanel extends JPanel implements Runnable
{
int x_pos = 10;
int y_pos = 100;
int radius = 50;
public PongPanel()
{
//Set to exit on close
// this.addComponentListener(new WindowAdapter()
// {
// @Override
// public void windowClosing(WindowEvent we)
// {
// System.exit(0);
// }
// });
this.setSize(300, 250);
}
public void start()
{
Thread th = new Thread(this);
th.start();
}
public static void main(String args[])
{
JFrame frame = new JFrame();
PongPanel panel = new PongPanel();
frame.add(panel);
frame.setBounds(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),(int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
panel.start();
frame.setVisible(true);
}
@Override
public void run()
{
//infini loop
while (true)
{
x_pos+=1;
repaint();
try
{
//Sleep thread for 20 milliseconds
Thread.sleep(20);
}
catch (InterruptedException ex)
{
//do nothing
}
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(getFrame(), 0, 0, this);
}
private Image getFrame() {
BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics g = img.getGraphics();
//g.setColor(Color.white);
//g.fillRect(0, 0, getWidth(), getHeight());
// Apply our own painting effect
Graphics2D g2d = (Graphics2D) g.create();
// 50% transparent Alpha
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
g2d.setColor(getBackground());
g2d.fill(getBounds());
g.setColor(Color.RED);
g.fillOval(x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
g.dispose();
return img;
}
}