鼠标单击后使球平稳运动

时间:2019-05-04 14:48:48

标签: java swing

我要做的是使鼠标跟随鼠标移动 我可以做到,但是球正以非常原始的方式移动

我试图获得鼠标位置(x,y),并且如果球的位置较小 我使用ballX ++或BallX--对于Y也是一样,这取决于在Game Method中单击鼠标

public class Controller
{
ArrayList<MyBall> myball = new ArrayList<MyBall>();
Random rand = new Random();
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();

MyBall ball = new MyBall (50,50,40,Color.getHSBColor(r, g, b));  //random 
colored ball whenever we execute

MyFrame mf = new MyFrame("Our Java Project") ;  //i have changed the 
name.this is title
//JLabel jlabel = new JLabel("GAME OVER");

Label l = new Label("10");
int MposX  ;
int MposY  ;

static int xDis = 0 ;
static int yDis = 0 ;
int  Bspeed = 50 ;

public static void main(String[] args)
{
    new Controller().Game();
}

public void Game()
{

    mf.setBackground(Color.WHITE);   //sets background color of the game
    mf.addMouseListener(new MyMouseMoveListener());
    mf.add(l,BorderLayout.NORTH);
    Refresh RF = new Refresh();
    Thread T = new Thread(RF);

    T.start();

    while(true)
    {
        try
        {
            Random Rand = new Random();
            Thread.sleep(30);
           /* ball.x = ball.x + xDis / Bspeed ;
            ball.y = ball.y + yDis / Bspeed ;*/

            if(ball.x <= MposX || ball.y <= MposY){
                ball.x++;
                ball.y++;
            }else if(ball.x >= MposX || ball.y <= MposY ){
                ball.x--;
                ball.y++;
            }else if (ball.x <= MposX || ball.y >= MposY){
                ball.x++;
                ball.y--;

            }else{
                ball.x--;
                ball.y--;
            }


        }catch(Exception e)
        {

        }

    }

}

class Refresh implements Runnable
{
    public void run()
    {
        while(true)
        {
            try
            {
                Thread.sleep(40);
            }catch(Exception e)
            {
                System.out.println("something happend");
            }


            mf.repaint();
            }



        }
    }



class MyMouseMoveListener implements MouseListener {
    public void mouseMoved(MouseEvent m){


    }

    @Override
    public void mouseClicked(MouseEvent e) {
        MposX = e.getX();
        MposY = e.getY();



    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}


class MyFrame extends Frame{
    MyFrame(String s){
        super(s);
        setBounds(0,0,640,480);
        add(ball);
        myball.add(ball);
        setVisible(true);
    }
    public void paint(Graphics g){
        for(MyBall b : myball)
            b.paint(g);

        }

    }
}

当BallX不小于或大于鼠标单击&&时,Y轴仍然运动,您将看到球沿x直线移动并沿y向下移动

////////////球类///////////

         public class MyBall extends Component
        {


public int x ;
public int y ;
public int size ;
public Color color ;


  MyBall(int x , int y , int size , Color c)
  {
    this.x = x ;
    this.y = y ;
    this.size = size ;
    this.color = c ;
  }

  public void paint(Graphics g)
  {
    g.setColor(color);
    g.fillOval(x, y, size, size);
  }

 }

0 个答案:

没有答案