Java 如何在另一个运动图像下添加背景图像?
我在做空中交通控制系统项目,我想让飞机在我的地图上移动。 到目前为止,我设法使图像移动(平面),现在我想在下面添加背景图像(地图)。我该怎么做 这是我的密码:Java 如何在另一个运动图像下添加背景图像?,java,image,user-interface,jframe,Java,Image,User Interface,Jframe,我在做空中交通控制系统项目,我想让飞机在我的地图上移动。 到目前为止,我设法使图像移动(平面),现在我想在下面添加背景图像(地图)。我该怎么做 这是我的密码: public class AnimatedPlane extends JFrame { public static void main(String[] args) { AnimatedPlane animatedplane = new AnimatedPlane(); } public Ani
public class AnimatedPlane extends JFrame {
public static void main(String[] args) {
AnimatedPlane animatedplane = new AnimatedPlane();
}
public AnimatedPlane() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Plane");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new AnimationPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class AnimationPane extends JPanel {
private BufferedImage plane;
private int xPos = 0;
private int direction = 1;
public AnimationPane() {
try {
plane = ImageIO.read(new File("H:\\Documents\\NetBeansProjects\\GroupProject\\src\\groupproject\\plane.png"));
Timer timer;
timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
xPos += direction;
if (xPos + plane.getWidth() > getWidth()) {
xPos = getWidth() - plane.getWidth();
direction *= -1;
} else if (xPos < 0) {
xPos = 0;
direction *= -1;
}
repaint();
}
});
timer.setRepeats(true);
timer.setCoalesce(true);
timer.start();
} catch (IOException ex) {
}
}
@Override
public Dimension getPreferredSize() {
return plane == null ? super.getPreferredSize() : new Dimension(plane.getWidth() * 4, plane.getHeight());
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int y = getHeight() - plane.getHeight();
g.drawImage(plane, xPos, y, this);
}
}
}
public类AnimatedPlane扩展JFrame{
公共静态void main(字符串[]args){
AnimatedPlane AnimatedPlane=新的AnimatedPlane();
}
公共动画平面(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
JFrame=新JFrame(“平面”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新的AnimationPane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类AnimationPane扩展了JPanel{
专用缓冲图像平面;
私有int xPos=0;
私有int方向=1;
公共动画窗格(){
试一试{
plane=ImageIO.read(新文件(“H:\\Documents\\NetBeansProject\\GroupProject\\src\\GroupProject\\plane.png”);
定时器;
计时器=新计时器(40,新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件e){
xPos+=方向;
如果(xPos+plane.getWidth()>getWidth()){
xPos=getWidth()-plane.getWidth();
方向*=-1;
}否则如果(xPos<0){
xPos=0;
方向*=-1;
}
重新油漆();
}
});
timer.setRepeats(真);
timer.setCoalesce(真);
timer.start();
}捕获(IOEX异常){
}
}
@凌驾
公共维度getPreferredSize(){
返回平面==null?super.getPreferredSize():新维度(plane.getWidth()*4,plane.getHeight());
}
@凌驾
受保护组件(图形g){
超级组件(g);
int y=getHeight()-plane.getHeight();
g、 drawImage(平面、xPos、y、this);
}
}
}
您不必在“下方”实际绘制它。
您可以先绘制背景,然后绘制所有平面(它们的图像)
比如:
基本上总是这样:
g.drawImage(myBackground, xPos, yPos, this); //Note: xPos and yPos is normally 0 in the beginning, as long as you dont move the map background around
g.drawImage(plane, xPos, yPos, this);