标题:java随即跳动的小球求助。。。
只看楼主
sw3458495
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-5-10
结帖率:0
已结贴  问题点数:20 回复次数:4 
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 
2012-05-10 16:17
sw3458495
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-5-10
得分:0 
求助
2012-05-10 16:44
ab1034982749
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:215
专家分:1185
注 册:2012-4-14
得分:20 
你不如再加个按钮,点一下按钮 小球就变大 速度变快
2012-05-10 23:02
sw3458495
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-5-10
得分:0 
回复 3楼 ab1034982749
那具体怎么弄?
2012-05-12 23:48
ab1034982749
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:215
专家分:1185
注 册:2012-4-14
得分:0 
为了简单一些,我把两个包合在了一起,
代码如下:
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();
   

    }

}
2012-05-16 11:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-367294-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.100782 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved