2016-06-18 7 views
0

学校のプロジェクトでは、私はトロンゲームを作っています。私は現在、自転車の写真を使っています。バイクは画面を横切って動きますので、キーをクリックして自転車を回すと、自転車はその時点で新しい画像を表示します。今は、画像を表示して、キーをクリックしたときにその画像を表示する最善の方法を見つけようとしています。 4つの異なる画像を持つか、画像を回転させる方が良いでしょうか。いずれの状況でも助けてください。ゲームの画像を回す

public class contents extends JPanel implements ActionListener, KeyListener 
{ 

    private int x = 0, y = 0; 
    private Timer t; 
    private ImageIcon d1 = new ImageIcon(this.getClass().getResource("BlueBike.jpg")); 
    private Image bike1 = d1.getImage();; 
    private ImageIcon d2 = new ImageIcon(this.getClass().getResource("BlueBike2.jpg")); 
    private Image bike2 = d2.getImage();; 
    private ImageIcon d3 = new ImageIcon(this.getClass().getResource("BlueBike3.jpg")); 
    private Image bike3 = d3.getImage();; 
    private ImageIcon d4 = new ImageIcon(this.getClass().getResource("BlueBike4.jpg")); 
    private Image bike4 = d4.getImage();; 
    private char direction = 'd'; 
    private char newdirection = 'd'; 
    public contents() 

    { 
     super.setDoubleBuffered(true); 
     t = new Timer(7, this); 
     t.start(); 

    } 


    @Override 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
     Graphics2D g2D = (Graphics2D)g; 

     if (direction == 'r') 
     g2D.drawImage(bike1, x, y, this); 
     if (direction == 'l') 
      g2D.drawImage(bike2, x, y, this); 
     if (direction == 'u') 
      g2D.drawImage(bike3, x, y, this); 
     if (direction == 'd') 
      g2D.drawImage(bike4, x, y, this); 

    } 

    @Override 
    public void actionPerformed(ActionEvent e) 
    { 
     //x=x+1; 

     direction = newdirection; 
     repaint(); 

    } 



    public void keyTyped(KeyEvent e) { 
     int code = e.getKeyCode(); 
     if (code == KeyEvent.VK_RIGHT) 
     { 
      newdirection = 'r'; 
     } 
     if (code == KeyEvent.VK_LEFT) 
     { 
      newdirection = 'l'; 
     } 
     if (code == KeyEvent.VK_UP) 
     { 
      newdirection = 'u'; 
     } 
     if (code == KeyEvent.VK_DOWN) 
     { 
      newdirection = 'd'; 
     } 

    } 


    public void keyPressed(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 



    public void keyReleased(KeyEvent e) { 
     // TODO Auto-generated method stub 

    } 
} 
+1

新しい画像を保存できますが、読み込んだ1つの画像から3つの画像を作成するには、新しいBufferedImageを作成し、BufferedImageから取得したGraphics2Dオブジェクトを使用して既存の画像を描画し、 AffineTransformを使用した画像。最初にそれを使用する方法を知るには、これを検索したいでしょう。 –

+0

アフィン変換を使用しようとしましたが、タイマーを使用して画像を画面全体に移動しようとしていますが、AffineTransformを使用すると画像は移動しません – javaprojecthelp

+0

2つの非常に異なる問題を混同しないでください:1)回転または反転した画像 - これにAffineTransformsを使用し、2)画面全体で画像を翻訳する - これに対してAffineTransformsを使用する可能性がありますが、おそらくそうしないでしょう。 –

答えて

-1

理想的には、あなたはあなたのゲームが必要とするすべての画像と画像Spriteを持っているでしょう。何らかの理由でSpriteを生成できない場合は、画像を回転するよりも4つの異なる画像を作成するほうがよいでしょう。あなたは自分でなぜそれが簡単か分かります。