如何在Java中使用两个图像?

时间:2013-11-23 18:29:48

标签: java swing

我正在尝试用背景制作游戏。我使用了一个可移动的图像,它的工作原理。这是我的源代码:

Class 1(RType.java):

package sprites;

import javax.swing.JFrame;

public class RType extends JFrame {
     /**
      * 
      */
     private static final long serialVersionUID = 1L;
     public RType() {
         add(new Background());
         add(new Board());

         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         setSize(800, 600);
         setLocationRelativeTo(null);
         setTitle("Game");
         setResizable(true);
         setVisible(true);
     }

     public static void main(String[] args) {
         new RType();
     }
}

第2类(Board.java):

package sprites;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener {
     /**
      *
      */
     private static final long serialVersionUID = 1L;
     private Timer timer;
     private Sprite sprite;

     public Board() {
         addKeyListener(new TAdapter());
         setFocusable(true);
         setDoubleBuffered(true);

         sprite = new Sprite();

         timer = new Timer(5, this);
         timer.start();
     }

     public void paint(Graphics g) {
         super.paint(g);

         Graphics2D g2d = (Graphics2D) g;
         g2d.drawImage(sprite.getImage(), sprite.getX(), sprite.getY(), this);

         Toolkit.getDefaultToolkit().sync();
         g.dispose();
     }

     public void actionPerformed(ActionEvent e) {
         sprite.move();
         repaint();
     }

     private class TAdapter extends KeyAdapter {
         public void keyPressed(KeyEvent e) {
             sprite.keyPressed(e);
         }

         public void keyReleased(KeyEvent e) {
             sprite.keyReleased(e);
         }
     }
}

Class 3(Sprite.java):

package sprites;

import java.awt.Image;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;

public class Sprite {
     private String craft = "Sprite.jpg";

     private int dx;
     private int dy;
     private int x;
     private int y;
     private Image image;

     public Sprite() {
          ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
          image = ii.getImage();
          x = 40;
          y = 60;
     }

     public void move() {
          x += dx;
          y += dy;
     }

     public int getX() {
          return x;
     }

     public int getY() {
          return y;
     }

     public Image getImage() {
          return image;
     }

     public void keyPressed(KeyEvent e) {
          int key = e.getKeyCode();
          if(key == KeyEvent.VK_LEFT) {
               dx = -3;
          }

          if(key == KeyEvent.VK_RIGHT) {
               dx = 3;
          }

          if(key == KeyEvent.VK_UP) {
               dy = -3;
          }

          if(key == KeyEvent.VK_DOWN) {
               dy = 3;
          }
     }

     public void keyReleased(KeyEvent e) {
          int key = e.getKeyCode();
          if(key == KeyEvent.VK_LEFT) {
               dx = 0;
          }

          if(key == KeyEvent.VK_RIGHT) {
               dx = 0;
          }

          if(key == KeyEvent.VK_UP) {
               dy = 0;
          }

          if(key == KeyEvent.VK_DOWN) {
               dy = 0;
          }
     }
}

第4类(Background.java):

package sprites;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class Background extends JPanel {
     /**
      *
      */
     private static final long serialVersionUID = 1L;

     Image nook;

     public Background() {
          ImageIcon ii = new ImageIcon(this.getClass().getResource("nook 013.jpg"));
          nook = ii.getImage();
     }

     public void paint(Graphics g) {
          Graphics2D g2d = (Grahpics2D) g;
          g2d.drawImage(nook, 10, 10, null);
     }
}

后台课似乎无法正常工作。我已经通过执行JFrame将该类添加到add(new Background());中,但它无效。救命?!!?可移动图像类工作得很好。

2 个答案:

答案 0 :(得分:1)

  1. 您没有提供任何约束来将组件定位在不同的位置:

      add(new Background());
      add(new Board());
    

    JFrame默认使用BorderLayout。它有FIRST, LAST, CENTER等类型的约束来定位它的组件。您可以使用它们,例如:add(new Background(), BorderLayout.CENTER);

  2. 请勿使用paint(Graphics g)进行自定义绘画。覆盖paintComponent(Graphics g)功能,不要忘记在其中调用super.paintComponent(g)

答案 1 :(得分:0)

  

BorderLayout(我知道这是默认设置)也会将背景放在角色后面吗?

没有。大多数布局管理器都是二维的,因此您无法使用相同的父面板在彼此之上添加组件。所以以下内容不起作用:

     add(new Background());
     add(new Board());

相反,您需要以下代码:

Board board = new Board();
Background background = new Background();
background.setLayout( new BorderLayout() );
background.add(board);
add(background, BorderLayout.CENTER);

这仍然无效,因为电路板是不透明的,并且会在背景上方绘画。您可以通过使Board不透明来解决此问题。

实际上,背景/委员会应该是一个单一的组成部分。它的工作是绘制背景,然后绘制精灵。

  

用箭头键移动精灵?

不要使用KeyListener。有关原因以及使用Key Bindings的解决方案,请参阅Motion Using the Keyboard

此外,默认情况下,Swing是双缓冲的,因此不需要对组件进行双缓冲。