博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
弹珠游戏
阅读量:7226 次
发布时间:2019-06-29

本文共 1702 字,大约阅读时间需要 5 分钟。

import java.awt.*;

import java.awt.event.*;
class Marble extends Thread
{
Table table=null;
int x,y,xdir,ydir;
public Marble (Table _table,int _x,int _y,int _xdir,int _ydir)
{
table=_table;
x=_x;
y=_y;
xdir=_xdir;
ydir=_ydir;
}
public void run()
{
while(true)
{
if((x>(table.getSize().width)-25)||(x<0))
{
xdir*=(-1);
}
if((y>(table.getSize().width)-25)||(y<0))
{
ydir*=(-1);
}
x+=xdir;
y+=ydir;
try
{
sleep(30);
}
catch(InterruptedException e)
{
System.err.println("Thread iterrupted");
}
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
{

Button start=new Button("start");

Marble marbles[]=new Marble[5];
int v=2;
public Table()
{
super("弹子弹球");
setSize(300,300);
setBackground(Color.cyan);
setVisible(true);
setLayout(new FlowLayout());
add(start);
start.addActionListener(this);
validate();
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
for(int i=0;i<marbles.length;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().width*Math.random());
marbles[i]=new Marble(this,x,y,xdir,ydir);
marbles[i].start();
}
}
public void paint(Graphics g)
{
for(int i=0;i<marbles.length;i++)
{
if(marbles[i]!=null)
{
marbles[i].draw(g);
}
}
}
}
public class Main {
public static void main(String args[])
{
Table table=new Table();
}
}

转载于:https://www.cnblogs.com/pengpenggege/p/8675276.html

你可能感兴趣的文章
Pdf Convert Image 的解决方案
查看>>
[笔记]使用clearfix清除浮动
查看>>
数据强转
查看>>
Latest crack software ftp download
查看>>
OpenStack 的防火墙规则流程
查看>>
Overloading Django Form Fields
查看>>
03.MyBatis的核心配置文件SqlMapConfig.xml
查看>>
python学习笔记(9)-python编程风格
查看>>
Apache HTTP Server搭建虚拟主机
查看>>
(译).NET4.X 并行任务中Task.Start()的FAQ
查看>>
git log显示
查看>>
java中相同名字不同返回类型的方法
查看>>
Rails NameError uninitialized constant class solution
查看>>
Android 获取SDCard中某个目录下图片
查看>>
设置cookies第二天0点过期
查看>>
【转载】NIO客户端序列图
查看>>
poj_2709 贪心算法
查看>>
【程序员眼中的统计学(11)】卡方分布的应用
查看>>
文件夹工具类 - FolderUtils
查看>>
http://blog.csdn.net/huang_xw/article/details/7090173
查看>>