我目前有一个背景图像,可以延伸到整个屏幕,前面有一个较小的图像。我希望前景图像在屏幕上移动。如果有人可以提供一个例子,或者帮助解决我的问题,那就太好了。到目前为止,我有以下代码:
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);
// }
}
下面的类绘制一个红色的球,它在屏幕上滑动。但是,当我添加此面板时,它用白色背景填充周围空间,我看不到原始背景图像。基本上我只是希望红色球在背景图像前面的屏幕上滑动。
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;
}
}
答案 0 :(得分:1)
在java中不可能重叠像jlabel,jbutton等jcomponent。,为了实现组件的重叠,
将jcomponents放在JLayered窗格
上JLayeredPane LPne = new javax.swing.JLayeredPane();
然后可以反复重叠任意数量的jcomponent,
尝试这种方式。