2013-12-10 65 views
2

我试图创建一个简单的游戏的开始。我正在尝试做的第一件事是将图形导入我的代码并将其移动到屏幕上。我能够在屏幕上画出一个球并移动它,但是当我从文件导入图形时,我无法移动它。我错过了什么或做错了什么?如何通过箭头键在屏幕上移动图形?

import javax.swing.*; 
import java.awt.Graphics; 
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.ImageIcon; 
import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyListener; 
import javax.swing.JPanel; 
import javax.swing.Timer; 

public class Game extends JPanel implements ActionListener, KeyListener { 

    Timer t = new Timer(5, this); 
    double x = 0, y = 0, velX = 0, velY = 0; 
    private ImageIcon image; 


    public Game(){ 
     setBackground(Color.WHITE); 
     t.start(); 
     addKeyListener(this); 
     this.setFocusable(true); 
     setFocusTraversalKeysEnabled(false); 
     image = new ImageIcon ("ship.gif"); 

    } 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     ImageIcon i = new ImageIcon("C:\\Users\\Bryan\\Pictures\\ship.gif"); 
     i.paintIcon(this, g, 0, 0); 

    } 

    public void actionPerformed(ActionEvent e){ 
     repaint(); 
     x += velX; 
     y += velY; 

     if(x<0){ 
      velX = 0; 
      x = 0; 
     } 

     if(x>750){ 
      velX = 0; 
      x = 750; 
     } 

     if(y<0);{ 
      velY = 0; 
      y = 0; 
     } 

     if(y>550){ 
      velY = 0; 
      y = 550; 
     } 
    } 

    public void up(){ 
     velY = -1.5; 
     velX = 0; 
    } 

    public void down(){ 
     velY = 1.5; 
     velX = 0; 
    } 

    public void left(){ 
     velX = -1.5; 
     velY = 0; 
    } 

    public void right(){ 
     velX = 1.5; 
     velY = 0; 
    } 

    public void keyPressed(KeyEvent e){ 
     int code = e.getKeyCode(); 

     if (code == KeyEvent.VK_UP){ 
      up(); 
     } 

     if (code == KeyEvent.VK_DOWN){ 
      down(); 
     } 

     if (code == KeyEvent.VK_LEFT){ 
      left(); 
     } 

     if (code == KeyEvent.VK_RIGHT){ 
      right(); 
     } 
    } 

    public void keyTyped(KeyEvent e){} 

    public void keyReleased(KeyEvent e){ 

//  velX = 0; 
//  velY = 0; 
     int code = e.getKeyCode(); 

     if (code == KeyEvent.VK_UP){ 
      velY = 0; 
     } 
     if (code == KeyEvent.VK_DOWN){ 
      velY = 0; 
     } 
     if (code == KeyEvent.VK_LEFT){ 
      velX = 0; 
     } 
     if (code == KeyEvent.VK_RIGHT){ 
      velX = 0; 
     } 
    } 

} 

我的司机是另一个类,如下所示:

import java.awt.Color; 

import javax.swing.JFrame; 

public class GameDriver { 

    public static void main(String[] args) { 

     JFrame f = new JFrame(); 
     Game g = new Game(); 
     f.add(g); 
     f.setVisible(true); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     f.setSize(800,600); 

    } 
} 

回答

2

两个大问题在这里:

public void paintComponent(Graphics g){ 
    super.paintComponent(g); 
    ImageIcon i = new ImageIcon("C:\\Users\\Bryan\\Pictures\\ship.gif"); 
    i.paintIcon(this, g, 0, 0); 
} 
  1. 你从文件中读取从paintComponent(...)内。 从来没有这样做,因为这会不必要地拖慢你的绘图。读取图像一次,也许在构造函数中,然后在绘图中使用存储的图像变量。 paintComponent方法应该用于绘制只有,它应该是瘦,平均和快速
  2. 您正在绘制0,0 总是。如果您想要移动某些东西,请在可变位置绘制,然后更改变量所保留的值并重绘。

另外:您应该使用键绑定接受Swing应用程序中的按键,因为这有助于解决焦点问题。

例如,请查看我的代码this answer

+1

(咳嗽)键绑定(咳嗽);)+1 – MadProgrammer

+0

@MadProgrammer:你应该咳嗽了一下(谢谢)。 –

+0

好的,谢谢。那么再给你一个问题。而不是使用paintComponent方法,我应该使用什么以及如何导入图像?对不起,我是一个新手(: – Bfrank

相关问题