2016-04-20 27 views
-5

嗨,這可能很容易爲你們大多數人,但我被授予此代碼今天在課堂上分析,我剛剛開始與Java我想知道如果有人可以填補我其他代碼手段,我真的不明白。不過,我確實瞭解我在某些地方放置了//標記。這段代碼是什麼意思,需要保證... Java基礎知識

**I have no clue about this following statement** 

public class PONG extends JComponent implements ActionListener, MouseMotionListener, KeyListener { 

    **are these the initial values?** 


    private static final long serialVersionUID = 1L; 
      private int scorefinal; 
      private int ballx = 150; 
      private int bally = 30; 
      private int ballx1 = 100; 
      private int bally1 = 10; 
      private int paddlex = 0; 
      private int ballySpeed = 7; 
      private int ballxSpeed = 5; 
      private int bally1Speed = 14; 
      private int ballx1Speed = 10; 

      public int score = 0; 
      public int score1 = 0; 
      public int bestscore; 
      public int bestscore1; 
      public boolean gameOver, started; 
     **Is this saying how fast the ball moves?** 


Timer tt = new Timer(10, g); 
     tt.start(); 

     } 
    **I do not know the following block of code** 
     public void newball(int ballx, int bally, int ballxspeed, int  ballyspeed) { 

      ballx = 150; 
      bally = 30; 
      ballxspeed = 5; 
      ballyspeed = 7; 
      return; 
     } 
      **So how is the scoring calculated?** 
      if (score >= 5) { 
       g.setColor(Color.red); 
       g.setFont(new Font("Arial", 8, 50)); 
       g.drawString(String.valueOf(score + score1), 30/1 - 15, 80); 
      } else { 
       g.setColor(Color.white); 
       g.setFont(new Font("Arial", 8, 50)); 
       g.drawString(String.valueOf(score), 30/1 - 15, 80); 
      } 
      // start && gameOver 
      g.setColor(Color.white); 
      g.setFont(new Font("Arial", 8, 50)); 

      if (gameOver) { 
    **I do not understand the 50/ 1-15, 200** 
       g.drawString(String.valueOf(" Best Score :" + scorefinal), 50/1 - 15, 200); 

      } 
     } 


     public void actionPerformed(ActionEvent e) { 

      ballx = ballx + ballxSpeed; 
      bally = bally + ballySpeed; 


**I think these are the boundaries but i am not entirely sure 
      // Window Down 
      if (ballx >= paddlex && ballx <= paddlex + 100 && bally >= 475) { 

       ballySpeed = -7; 
       score++; 

      } 

      if (bally >= 700) { 

       score = 0; 
       bally = 30; 
       gameOver = true; 

      } 

      // Window up 
      if (bally <= 0) { 

       ballySpeed = 7; 

      } 

      // Window right 
      if (ballx >= 775) { 

       ballxSpeed = -5; 

      } 

      // Window left 
      if (ballx <= 0) { 

       ballxSpeed = 5; 

      } 

      //i do not know what this means 
      ballx1 = ballx1 + ballx1Speed; 
      bally1 = bally1 + bally1Speed; 

      // Window down 
      if (ballx1 >= paddlex && ballx1 <= paddlex + 100 && bally1 >= 475) { 

       bally1Speed = -14; 
       score1++; 

      } 

      if (bally1 >= 700) { 

       score1 = 0; 
       bally1 = 10; 


      } 

      // Window up 
      if (bally1 <= 0) { 

       bally1Speed = 14; 

      } 

      // Window right 
      if (ballx1 >= 775) { 

       ballx1Speed = -10; 

      } 

      // Window left 
      if (ballx1 <= 0) { 

       ballx1Speed = 10; 

      } 


     **/i understand this has something to do with mouse events but i dont get the -50 part, and what is the overrides mean** 
     public void mouseMoved(MouseEvent e) { 

      paddlex = e.getX() - 50; 
      repaint(); 
     } 

     @Override 
     public void mouseDragged(MouseEvent e) { 
     } 

     @Override 
     public void keyTyped(KeyEvent e) { 

     } 

     @Override 
     public void keyPressed(KeyEvent e) { 

     } 

     @Override 
     public void keyReleased(KeyEvent e) { 
+4

如果只有少數你不明白的東西,不要發佈200行無關的代碼。改變你的頭銜以反映你不明白的以及你不瞭解他們的內容。在你的問題身上做同樣的事情。投票結束目前不清楚。 – Savior

+0

你不瞭解哪些部分? –

+0

如果您想要具體的答案和幫助,請**提出具體問題**。 –

回答

-2

@覆蓋意味着,從你的情況「的JComponent」的實現類或「超一流」的代碼將通過實現它的方法子類覆蓋。你的PONG類將方法引入並更改需要從超類中改變的方法以實現其目標。

無論是否被更改,所有方法都需要覆蓋。

導入是項目依賴關係。

主要方法是你程序的開始;代碼由計算機連續執行,從JFrame的實例化開始,逐行執行,直到定時器到期並結束程序。

+0

非常感謝你有助於設置一些事情直! –

+0

沒問題。只是一個小費;如果您的問題具體,您收到的答案將會更有幫助。 –