当前位置: 代码迷 >> Java相关 >> java随即跳动的小球求助。。。
  详细解决方案

java随即跳动的小球求助。。。

热度:300   发布时间:2012-05-10 16:17:24.0
java随即跳动的小球求助。。。
在JAVA实现跳动的小球。。代码如下:
package cn;

import java.awt.Graphics;
import java.awt.*;

import javax.swing.JPanel;

public class BallOnPanel extends JPanel implements Runnable{
   
    int x=110;
    int y=110;
   
    public void paint(Graphics g){
        super.paint(g);
        g.fillOval(x, y, 30, 30);
    }
   
    public void run() {
        while(true){
            x=(int)(Math.random()*450);
            y=(int)(Math.random()*350);
            try{
                Thread.sleep(1000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            this.repaint();
        }
        
    }


}

2:
package cn;

import javax.swing.JFrame;

public class BallTest {

    JFrame jframe=null;
    BallOnPanel ball=null;
    public BallTest(){
        jframe=new JFrame();
        ball=new BallOnPanel();
        ball.setSize(450,350);
        jframe.setSize(500, 400);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.setVisible(true);
        jframe.add(ball);
        new Thread(ball).start();
        
    }
   
    public static void main(String[] args) {
        BallTest bt=new BallTest();
   

    }

}
请问怎么利用MouseListener。让我鼠标点击到小球,小球就变大,速度变快。。
搜索更多相关的解决方案: void  java  package  public  import  

----------------解决方案--------------------------------------------------------
求助
----------------解决方案--------------------------------------------------------
你不如再加个按钮,点一下按钮 小球就变大 速度变快
----------------解决方案--------------------------------------------------------
回复 3楼 ab1034982749
那具体怎么弄?
----------------解决方案--------------------------------------------------------
为了简单一些,我把两个包合在了一起,
代码如下:
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BallOnPanel extends JPanel implements Runnable{
   
    int x=110;
    int y=110;
       int width=30,high=30,time=1000;
    public void paint(Graphics g){
        super.paint(g);
        g.fillOval(x, y, width, high);
    }
   
    public void run() {
        while(true){
            x=(int)(Math.random()*450);
            y=(int)(Math.random()*350);
            try{
                Thread.sleep(time);
            }catch(InterruptedException e){
                e.printStackTrace();
            }
            this.repaint();
        }
        
    }


}
public class BallTest {

    JFrame jframe=null;
    BallOnPanel ball=null;
    public BallTest()
    {
            JButton button1=new JButton("变大变快");
            JButton button2=new JButton("变小变慢");
        jframe=new JFrame();
        ball=new BallOnPanel();
        button1.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e)
            {
                ball.width+=5;
                ball.high+=5;
                ball.time-=100;
            }
        });
        button2.addMouseListener(new MouseAdapter(){
            public void mouseClicked(MouseEvent e)
            {
                ball.width-=5;
                ball.high-=5;
                ball.time+=100;
            }
        });
        ball.setSize(450,350);
        jframe.setSize(500, 400);
        jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jframe.add(ball);
        ball.add(button1,"NORTH");
        ball.add(button2,"NORTH");
        new Thread(ball).start();
        jframe.setVisible(true);
      
        
    }
   
   
    public static void main(String[] args) {
        BallTest bt=new BallTest();
   

    }

}

----------------解决方案--------------------------------------------------------
  相关解决方案