在游戏中将精灵放在背景上

时间:2014-04-27 21:10:11

标签: java swing jframe jpanel

我在将精灵加载到游戏中时遇到问题。我已完成背景,但精灵似乎不在后台。我想假设你把它放在主构造函数中,但我不认为它会起作用。

package MyGamePKG;

import java.awt.BorderLayout;

public class GameJFrame extends JFrame {

private JPanel contentPane;
private int x,y,x_vel,y_vel;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                GameJFrame frame = new GameJFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public GameJFrame() {


    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 720, 520);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    GameJPanel Jpanel = new GameJPanel();
    Jpanel.setBounds(0, 0, 720, 480);
    contentPane.add(Jpanel);
    Jpanel.setLayout(null);

    JLabel lblNewLabel = new JLabel("New label");
    lblNewLabel.setIcon(new ImageIcon(GameJFrame.class.getResource("/Resources/gamemap.png")));
    lblNewLabel.setBounds(0, 0, 720, 480);
    contentPane.add(lblNewLabel);




}
}

JPanel类

package MyGamePKG;

import java.awt.BasicStroke;

public class GameJPanel extends JPanel implements ActionListener, KeyListener {

private int frameRate = 30;
private int x=0;
private int y=460;
private int x_vel, y_vel;
private Image map;
private Image character;

/**
 * Create the panel.
 */
public GameJPanel() {
    character = new ImageIcon(getClass().getResource("/resources/sprite-concepts.png")).getImage();
    this.addKeyListener(this);
    this.setFocusable(true);
    Timer timer = new Timer(30, this);      
    timer.start();


}
@Override
public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;

    RenderingHints rh = new RenderingHints( RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);

    g2d.setRenderingHints(rh);
    this.setOpaque(false);
    myDrawBoxOvalsandRect(x,y,100,g2d);
    //myDrawArc(x+25,y+40,50,50,550,170,g2d);
} // paintComponent
public void myDrawBoxOvalsandRect( int x, int y, int scale, Graphics my_g)
{
    my_g.setColor(Color.cyan);
    my_g.fillOval(x, y, 15, 15); //face


}
public void myDrawArc(int x, int y, int height, int width, int angle1, int angle2, Graphics my_g)
{
    my_g.setColor(Color.red);
    my_g.drawArc(x, y, 50, 50, 550, 170); //happy face      
}
@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    int c = e.getKeyCode();

    if(c == KeyEvent.VK_LEFT)
    {
        x_vel = -2;
        y_vel = 0;
    }
    if(c == KeyEvent.VK_UP)
    {
        x_vel = 0;
        y_vel = -2;
    }
    if( c == KeyEvent.VK_RIGHT)
    {
        x_vel = 2;
        y_vel = 0;
    }
    if( c == KeyEvent.VK_DOWN)
    {
        x_vel = 0;
        y_vel = 2;
    }
    repaint();

}
@Override
public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub
    x_vel = 0;
    y_vel = 0;
    repaint();
}
@Override
public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

}
@Override
public void actionPerformed(ActionEvent e) {
    // TODO Auto-generated method stub
    if(x < 0)
    {
        x_vel = 0;
        x = 0;
    }
    if(x > 720)
    {
        x_vel = 0;
        x = 720;
    }
    if(y < 0)
    {
        y_vel = 0;
        y = 0;
    }
    if(y > 480)
    {
        y_vel = 0;
        y = 480;
    }
    x = x + x_vel;
    y = y + y_vel;
    repaint();

}


}

1 个答案:

答案 0 :(得分:0)

我怀疑有很多事情......

  1. 组件的z排序可能是在前景组件上呈现背景
  2. 依赖null布局意味着窗口边框可能占用更多空间,然后覆盖部分内容。
  3. 不要依赖空布局并猜测工作。每个平台/外观在框架边框(以及其他内容)方面都有不同的要求。相反,你应该考虑......

    • 覆盖getPreferredSize以提供更好的尺寸提示
    • 使用OverlayLayout经理
    • 使用JFrame#pack打包内容周围的框架,而不是使用setBounds

    考虑使用单个paintComponent方法绘制整个游戏状态,这意味着在GameJPanel的{​​{1}}内渲染背景,中间地带和前景。

    这样做的好处是,您可以完全控制z排序过程。

    您应该考虑使用key bindings而不是使用paintComponent,因为它不会遇到相同的焦点相关问题,并且可以更好地控制确定组件引发关键事件的焦点水平< / p>