2017-06-07 10 views
-1

基本的に数日間、私は矢印キーで自分の「スーパーマン」オブジェクトを移動させたキーリスナーを見つけようとしています。これは、「動く」道を描くときの私のwhileループの間にI/* */outが働いているときだけ機能します。コメントを外してプログラムを実行すると、スーパーマンは動かず、ループが画像よりも優先順位が高いためと考えています。私は、画像の優先順位をスレッディングするのに助けが必要だと思っています。ヘルプは非常に感謝しています。イメージを優先的にループに描く方法描画の背景

// The "CPT" class. 
import java.applet.*; 
import java.awt.*; 
import java.awt.Image; 
import java.awt.event.*; 
import javax.swing.*; 
import java.lang.Runnable; 
import java.lang.Thread; 



public class CPT extends Applet implements KeyListener 
{ 
    // Place instance variables here 
    Image myImage; 
    int x = 100, y = 200; 

    public void init() 
    { 

     myImage = getImage (getCodeBase(), "SuperMan.png"); 
     System.out.println (getCodeBase()); 
     addKeyListener (this); 
     // Place the body of the initialization method here 
    } // init method 





    public void paint (Graphics g) 
    { 




     //Drawing sky 
     g.setColor (new Color (150, 150, 254)); 
     g.fillRect (0, 0, 1000, 500); 
     //Drawing floor 
     g.setColor (Color.black); 
     g.fillRect (0, 500, 1000, 150); 
     //Drawing road lines 





     //g.fillRect (
     //Drawing Sun 
     g.setColor (Color.yellow); 
     g.fillOval (800, 50, 75, 75); 

     //Drawing Birds 
     g.setColor (Color.black); 
     g.drawArc (100, 75, 25, 25, 0, 180); 
     g.drawArc (125, 75, 25, 25, 0, 180); 

     g.drawArc (175, 85, 25, 25, 0, 180); 
     g.drawArc (200, 85, 25, 25, 0, 180); 

     g.drawArc (250, 65, 25, 25, 0, 180); 
     g.drawArc (275, 65, 25, 25, 0, 180); 

     //Drawing Smaller Birds 

     g.drawArc (125, 55, 15, 15, 0, 180); 
     g.drawArc (140, 55, 15, 15, 0, 180); 
     while (true) 
     { 

      g.setColor (Color.black); 
      g.fillRect (-30, 575, 80, 25); 
      g.fillRect (120, 575, 80, 25); 
      g.fillRect (270, 575, 80, 25); 
      g.fillRect (420, 575, 80, 25); 
      g.fillRect (570, 575, 80, 25); 
      g.fillRect (720, 575, 80, 25); 
      g.fillRect (870, 575, 80, 25); 
      //first blocks 
      g.setColor (Color.yellow); 
      g.fillRect (50, 575, 80, 25); 
      g.fillRect (200, 575, 80, 25); 
      g.fillRect (350, 575, 80, 25); 
      g.fillRect (500, 575, 80, 25); 
      g.fillRect (650, 575, 80, 25); 
      g.fillRect (800, 575, 80, 25); 
      g.fillRect (950, 575, 80, 25); 





      for (int k = 0 ; k < 100000000 ; k++) 
       ; 
      //second blocks 

      g.setColor (Color.black); 
      g.fillRect (50, 575, 80, 25); 
      g.fillRect (200, 575, 80, 25); 
      g.fillRect (350, 575, 80, 25); 
      g.fillRect (500, 575, 80, 25); 
      g.fillRect (650, 575, 80, 25); 
      g.fillRect (800, 575, 80, 25); 
      g.fillRect (950, 575, 80, 25); 

      g.setColor (Color.orange); 
      g.fillRect (10, 575, 80, 25); 
      g.fillRect (160, 575, 80, 25); 
      g.fillRect (310, 575, 80, 25); 
      g.fillRect (460, 575, 80, 25); 
      g.fillRect (610, 575, 80, 25); 
      g.fillRect (760, 575, 80, 25); 
      g.fillRect (910, 575, 80, 25); 

      for (int z = 0 ; z < 100000000 ; z++) 
       ; 


      //third blocks 

      g.setColor (Color.black); 
      g.fillRect (10, 575, 80, 25); 
      g.fillRect (160, 575, 80, 25); 
      g.fillRect (310, 575, 80, 25); 
      g.fillRect (460, 575, 80, 25); 
      g.fillRect (610, 575, 80, 25); 
      g.fillRect (760, 575, 80, 25); 
      g.fillRect (910, 575, 80, 25); 


      g.setColor (Color.yellow); 
      g.fillRect (-30, 575, 80, 25); 
      g.fillRect (120, 575, 80, 25); 
      g.fillRect (270, 575, 80, 25); 
      g.fillRect (420, 575, 80, 25); 
      g.fillRect (570, 575, 80, 25); 
      g.fillRect (720, 575, 80, 25); 
      g.fillRect (870, 575, 80, 25); 

      for (int t = 0 ; t < 100000000 ; t++) 
       ; 



      g.drawImage (myImage, x, y, 100, 40, this); 

     } 
    } // paint method 


    public void keyReleased (KeyEvent e) 
    { 


    } 


    public void keyPressed (KeyEvent e) 
    { 
     if (e.getKeyCode() == KeyEvent.VK_RIGHT) 
     { 
      // System.out.println ("Right Key Pressed"); 
      x += 10; 
     } 

     if (e.getKeyCode() == KeyEvent.VK_LEFT) 
     { 
      // System.out.println ("Left Key Pressed"); 
      x -= 10; 
     } 

     if (e.getKeyCode() == KeyEvent.VK_UP) 
     { 
     y -= 10; 
     } 

     if (e.getKeyCode() == KeyEvent.VK_DOWN) 
     { 
     y+=10; 
     } 
     repaint(); 
    } 



    public void keyTyped (KeyEvent e) 
    { 

    } 
} // CPT class 

答えて

1

そのwhile loopはアプリケーション全体をフリーズします。それはグラフィックス上のものを描画し続けるが、スイングイベントディスパッチスレッドがあなたのグラフィックスで画面をリフレッシュできるようにすることはできません。

ヒント:

1)paint方法の外に、独立したスレッドにwhileループを移動します。 Java Swingで適切なアニメーションを実行する方法をさらによく読んでください。

2)Swingコンポーネントpaintメソッドを正しく上書きする方法をお読みください。

3)Event Dispatch Threadとは何ですか、なぜそれをブロックしないでください。