import java.awt.*;
import java.awt.event.*;
/* 定义弹子类 */
class Marble extends Thread
{
Table table=null;
int x,y,xdir,ydir,num1;
public Marble(Table _table,int _x,int _y,int _xdir,int _ydir,int num)
{
table=_table; //使用该参数,是为了能获取到窗口的大小
x=_x; //x坐标
y=_y; //y坐标
xdir= _xdir; //x方向速度
ydir= _ydir; //y方向速度
} public void run()
{
while(true)
{
if((x>(table.getSize().width)-25)||(x<0))
xdir*=(-1); //超过台子x方向边界后,反方向运行
if((y>(table.getSize().width)-25)||(y<0))
ydir*=(-1); //超过台子y方向边界后,反方向运行
x+=xdir; //坐标递增 以实现移动
y+=ydir;
try{ sleep(30); //延时时间(1/刷新率)
} catch(InterruptedException e)
{System.err.println("Thread interrupted");}
table.repaint(); //重绘图形
}
} public void draw(Graphics g)
{
g.setColor(Color.black); //弹子为黑色
g.fillOval(x,y,30,30); //画圆
g.setColor(Color.white); //弹子上的亮点为白色
g.fillOval(x+5,y+5,8,6);
}
}/* 定义弹子球台类 */
class Table extends Frame implements ActionListener,ItemListener
{
Button start=new Button("开始");
int num,spee;
Marble marbles[]=new Marble[num]; //建立弹子线程类对象数组
int v=2; //速度最小值
TextField amount = new TextField(3);
Choice color=new Choice();
TextField speed = new TextField(3);
public Table()
{
super("弹子台球");
setSize(400,400);
setBackground(Color.cyan); //背景
setVisible(true);
setLayout(new FlowLayout());
add(new Label("数量:"));
add(amount);
add(new Label("颜色:"));
color.add("blue");
color.add("green");
color.add("red");
add(color);
add(new Label("速度:"));
add(speed);
add(start);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{System.exit(0);}
} );
} public void actionPerformed(ActionEvent ae)
{ num=Integer.parseInt(amount.getText());
spee=Integer.parseInt(speed.getText());
for(int i=0;i<num;i++)
{ //随机产生弹子的速度和坐标
int xdir=i*(1-i*(int)Math.round(Math.random()))+v;
int ydir=i*(1-i*(int)Math.round(Math.random()))+v;
int x=(int)(getSize().width*Math.random());
int y=(int)(getSize().height*Math.random());
//实例化弹子线程对象
marbles[i]=new Marble(this,x,y,xdir,ydir,num);
marbles[i].start();
}
}
public void paint(Graphics g)
{
for(int i=0;i<num;i++)
if(marbles[i]!=null)
marbles[i].draw(g);
}
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
}
}
/* 定义主类 */
public class Ball
{
public static void main(String args[])
{
Table table=new Table();
}
}怎么把num传递到Marble对象中,使得点击按钮后球体数量为输入数量
import java.awt.event.*;
/* 定义弹子类 */
class Marble extends Thread
{
Table table=null;
int x,y,xdir,ydir,num1;
public Marble(Table _table,int _x,int _y,int _xdir,int _ydir,int num)
{
table=_table; //使用该参数,是为了能获取到窗口的大小
x=_x; //x坐标
y=_y; //y坐标
xdir= _xdir; //x方向速度
ydir= _ydir; //y方向速度
} public void run()
{
while(true)
{
if((x>(table.getSize().width)-25)||(x<0))
xdir*=(-1); //超过台子x方向边界后,反方向运行
if((y>(table.getSize().width)-25)||(y<0))
ydir*=(-1); //超过台子y方向边界后,反方向运行
x+=xdir; //坐标递增 以实现移动
y+=ydir;
try{ sleep(30); //延时时间(1/刷新率)
} catch(InterruptedException e)
{System.err.println("Thread interrupted");}
table.repaint(); //重绘图形
}
} public void draw(Graphics g)
{
g.setColor(Color.black); //弹子为黑色
g.fillOval(x,y,30,30); //画圆
g.setColor(Color.white); //弹子上的亮点为白色
g.fillOval(x+5,y+5,8,6);
}
}/* 定义弹子球台类 */
class Table extends Frame implements ActionListener,ItemListener
{
Button start=new Button("开始");
int num,spee;
Marble marbles[]=new Marble[num]; //建立弹子线程类对象数组
int v=2; //速度最小值
TextField amount = new TextField(3);
Choice color=new Choice();
TextField speed = new TextField(3);
public Table()
{
super("弹子台球");
setSize(400,400);
setBackground(Color.cyan); //背景
setVisible(true);
setLayout(new FlowLayout());
add(new Label("数量:"));
add(amount);
add(new Label("颜色:"));
color.add("blue");
color.add("green");
color.add("red");
add(color);
add(new Label("速度:"));
add(speed);
add(start);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing (WindowEvent e)
{System.exit(0);}
} );
} public void actionPerformed(ActionEvent ae)
{ num=Integer.parseInt(amount.getText());
spee=Integer.parseInt(speed.getText());
for(int i=0;i<num;i++)
{ //随机产生弹子的速度和坐标
int xdir=i*(1-i*(int)Math.round(Math.random()))+v;
int ydir=i*(1-i*(int)Math.round(Math.random()))+v;
int x=(int)(getSize().width*Math.random());
int y=(int)(getSize().height*Math.random());
//实例化弹子线程对象
marbles[i]=new Marble(this,x,y,xdir,ydir,num);
marbles[i].start();
}
}
public void paint(Graphics g)
{
for(int i=0;i<num;i++)
if(marbles[i]!=null)
marbles[i].draw(g);
}
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
}
}
/* 定义主类 */
public class Ball
{
public static void main(String args[])
{
Table table=new Table();
}
}怎么把num传递到Marble对象中,使得点击按钮后球体数量为输入数量
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货