简体   繁体   English

我的游戏(JFrame)上的背景图片

[英]Background image on my game (JFrame)

I recently started developing a Java mini-game and I'm stuck at managing to put a background image on my frame. 我最近开始开发Java小型游戏,但我一直坚持设法将背景图像放在我的框架上。 I searched on StackOverflow for this and I found a couple of threads with good suggestions but I simply couldn't implement them on my already done Frame (the things is I found ways to make a new frame and add a background image on the newly created frame with a class, but I already created my frame and I just can't make that code work on my project). 我在StackOverflow上搜索了此内容,发现了几个具有很好建议的线程,但我根本无法在已经完成的Frame上实现它们(事情是我找到了制作新框架并在新创建的框架上添加背景图像的方法)框架,但是我已经创建了框架,而我的代码无法在我的项目中运行)。

Here is what I found: http://java-demos.blogspot.ro/2012/09/setting-background-image-in-jframe.html 这是我发现的内容: http : //java-demos.blogspot.ro/2012/09/setting-background-image-in-jframe.html

I tried everything but couldn't make it work. 我尝试了所有内容,但无法正常工作。 The best I could do was putting this line: **frame**.setContentPane(new JLabel(new ImageIcon("C:\\\\Users\\\\Computer\\\\Downloads\\\\colorful design.png"))); 我所能做的最好的事情是: **frame**.setContentPane(new JLabel(new ImageIcon("C:\\\\Users\\\\Computer\\\\Downloads\\\\colorful design.png")));

This line succesfully set my background image but I couldn't control the square with my keys anymore and I had some errors. 这条线成功设置了我的背景图像,但是我再也无法用键控制正方形了,并且出现了一些错误。

This is my relevant code on which I need help to apply a background image to my Frame (with frame, canvas or panel, any option is welcomed). 这是我的相关代码,需要我帮忙将背景图像应用于框架(带有框架,画布或面板,欢迎使用任何选项)。

    public class Game implements Runnable{ 

   final int WIDTH = 640;
   final int HEIGHT = 480;

   JFrame frame;
   Canvas canvas;
   BufferStrategy bufferStrategy;

   public Game(){

      frame = new JFrame("Basic Game");




      JPanel panel = (JPanel) frame.getContentPane();
      panel.setPreferredSize(new Dimension(WIDTH, HEIGHT));
      panel.setLayout(null);



      canvas = new Canvas();
      canvas.setBounds(0, 0, WIDTH, HEIGHT);
      canvas.setIgnoreRepaint(true);

      panel.add(canvas);
      canvas.addKeyListener(new KeyControl()); //adds the controller 





      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      frame.pack();
      frame.setResizable(false); //non resizable window
      frame.setVisible(true);



      canvas.createBufferStrategy(2);
      bufferStrategy = canvas.getBufferStrategy();

      canvas.requestFocus();

   }

Do not mix AWT components (Canvas) with SWING components (JFrame). 请勿将AWT组件(Canvas)与SWING组件(JFrame)混合使用。 It doesn't work. 没用 Do something like this. 做这样的事情。

public class Game extends JFrame {

   public Game() {

       initUI();
   }  

   private void initUI() {

       JLabel background = new JLabel("image.png");
       setContentPane(background);

       pack();
       repaint();

       setResizable(false);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       setLocationRelativeTo(null);
   }

   public static void main(String[] args) {
       EventQueue.invokeLater(new Runnable() {
           @Override
           public void run() {
               Game g = new Game();
               g.setVisible(true);
               running = true;
           }
       });

   }

Remember, java.awt.Canvas (and all AWT components) do not support transparency. 请记住, java.awt.Canvas (和所有AWT组件)不支持透明性。

Generally speaking, you shouldn't be mixing BufferStrategy with Swing, as they use different painting process which can cause issues. 一般来说,您不应该将BufferStrategy与Swing混合BufferStrategy ,因为它们使用不同的绘画过程,这可能会导致问题。

Instead of trying to paint the background via a different component, simply paint the background directly to your BufferStrategy and then paint the remaining game state in top of it... 无需尝试通过其他组件绘制背景,只需直接将背景绘制到您的BufferStrategy ,然后在其顶部绘制剩余的游戏状态...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM