java - 如何在另一个动态图像下添加背景图像?

标签 java image user-interface jframe

我正在从事空中交通管制系统项目,我想让飞机在我的 map 上移动。 到目前为止,我设法使图像移动(平面),现在我想在下面添加背景图像( map )。我怎么做?

这是我的代码:

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);

        }

    }

}

最佳答案

您不必实际将其绘制在“下方”。 您可以先绘制背景,然后绘制所有平面(它们的图像)。

喜欢:

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);

它基本上总是这样:

  1. 绘制背景。
  2. 绘制任何应该位于背景上(“上方”)的对象。

关于java - 如何在另一个动态图像下添加背景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23249114/

相关文章:

java - luabind 和 java

html - 如何将较小的图像发送到移动浏览器(消除大数据请​​求)

ios - 来自 for in 数组的变量不会快速显示在我的 Collection View 中

java - if 语句检查 Swing 组件是否为空

Java Swing GUI - 在底部添加单个边框

java - 抽屉布局中菜单项工具栏上的后退操作按钮不起作用

java - 如何使用替换/正则表达式替换字符串中的两个字符?

java - 使用 AsyncTask 在 ListView 中显示图像

user-interface - TextFormFields 之间的空间

ios - SwiftUI:将动画附加到过渡