2015-05-01 68 views
0

我搜索了整個網絡,試圖找到一個解決方案,但最終我只是變得更加困惑和沮喪。我試圖讓我的槳移動(paddle_y)。槳不需要x座標,它只需在垂直線上上下移動即可。我不知道什麼語法我需要mouseListener來讓我的槳移動。請儘可能提供一個示例或解決方案。非常感謝,任何幫助表示讚賞。將MouseMotionListener添加到槳(Java Pong Game)

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 


public class PongGame extends JFrame implements Runnable, MouseMotionListener { 


    int ball_x, ball_y, ball_dx, ball_dy; 
    int ball_r; 

    int x_left, x_right, y_top, y_bottom; 

    int paddle_y = 30; 


    /** 
    * Constructor 
    */ 
    public PongGame(){ 
    init(); 
    } 

    /** 
    * UI 
    */ 
    protected void init(){ 
    this.setSize(300,300); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  


    int curX = -1, curY = -1; 

    ball_x = this.getWidth()/2; 
    ball_y = this.getHeight()/2; 

    ball_dx = ball_dy = 2; 

    ball_r = 20; 

    this.setVisible(true); 
    getFocus(this); 

    x_left = this.getInsets().left; 
    x_right = this.getWidth() - this.getInsets().right - ball_r; 
    y_top = this.getHeight() - this.getInsets().top + ball_r/3; 
    y_bottom = this.getInsets().bottom + ball_r; 

    addMouseMotionListener(this); 

    } 

    public void mouseMoved(MouseEvent e) { 

     //help 
    } 


    public void mouseDragged(MouseEvent e) { 
     //help 
    } 


    /** 
    * helper method which we use to get the focus 
    */ 
    public void getFocus(final JFrame frame) 
    { 
     EventQueue.invokeLater(new Runnable() { 
       public void run() { 
        frame.requestFocus(); 
       } 
     }); 
    } 

    /** 
    * implementation of the Runnable interface to be able to move the ball, etc. 
    */ 
    public void run(){ 

    while(true){ 

     ball_x += ball_dx; 
     if(ball_x <= x_left || ball_x >= x_right){ 
     ball_dx *=-1; 
     ball_x += (2*ball_dx); 
     } 

     ball_y += ball_dy; 
     if(ball_y <= y_bottom || ball_y >= y_top){ 
     ball_dy *=-1; 
     ball_y += (2*ball_dy); 
     } 

     repaint(); 

     try{ 
     Thread.sleep(50); 
     }catch(InterruptedException ex){ 
     System.out.println(ex); 
     } 
    } 
    } 

    /** 
    * all rendering occurs here 
    */ 
    public void paint(Graphics g){ 

    //Color paddleOne; 
    //paddleOne = new Color(0); 

    g.setColor(Color.white); 
    g.fillRect(0,0,this.getWidth(),this.getHeight()); 

    g.setColor(Color.black); 
    g.fillOval(ball_x,ball_y, ball_r, ball_r); 

    g.setColor(Color.black); 
    g.fillRect(0,paddle_y,20,70); 
    } 

    /** 
    * entry point into the program 
    */ 
    public static void main(String[] args){ 
    // create the class 
    PongGame application = new PongGame(); 
    new Thread(application).start(); 

    } 

} 

槳:

int paddle_y = 30; 



g.setColor(Color.black); 
g.fillRect(0,paddle_y,20,70); 




public void mouseMoved(MouseEvent e) { 

     //help 
    } 


    public void mouseDragged(MouseEvent e) { 
     //help 
    } 
+1

嘿,你可以去這裏,得到一切。 https://github.com/Jaryt23/PongTutorial/tree/master/pong –

+1

看看[MouseEvent的文檔](https://docs.oracle.com/javase/8/docs/api/java/awt /event/MouseEvent.html)。這些方法中的任何一個看起來好像可能會提供可用於修改paddle_y字段值的數據? – VGR

+0

我會看看這些文件。非常感謝。非常感激。 – reckless

回答

1

Paddle類需要爲了添加一個鼠標監聽的JFrame類的一個實例。如果你傳遞一個JFrame對象到構造函數,然後你可以使用下面的代碼:

public class Paddle { 
    private JFrame frame; 
    private int paddle_y; 
    public Paddle(JFrame frame) { 
     this.frame = frame; 
     this.paddle_y = 30; //Or whatever number you want 
     frame.addMouseMotionListener(new MouseMotionListener() { 
      public void mouseMoved(MouseEvent e) { 
       Paddle.this.paddle_y = e.getY(); //When the mouse is moved, it will call on this function to change the Paddle.paddle_y variable. 
      } 

      public void mouseDragged(MouseEvent e){} //This reacts when the mouse is clicked, moved, then released. 
    } 
} 

這應該更換槳類,而當鼠標移動時它應該改變paddle_y變量。

編輯:如果你不希望創建一個新的類,要把它放到JFrame類:

int paddle_y; 
this.addMouseMotionListener(new MouseMotionListener() { 
       public void mouseMoved(MouseEvent e) { 
        paddle_y = e.getY(); //When the mouse is moved, it will call on this function to change the paddle_y variable (Within the JFrame class). 
       } 

然後你就可以在渲染時對paddle_y變量調用。

+0

即時嘗試添加這個沒有類,但即時通訊面臨着很多錯誤。任何想法如何在沒有新班的情況下做到這一點? – reckless

+1

@受騙使用上面的編輯 – HyperNeutrino