求大神帮忙做一下

实验名称:图形界面多线程编程实验要求:1、在桌面中设计一个窗体(大小根据自己的计算机进行),在窗体中有若干个个小球(使用绘图工具完成)2、其中小球可以在窗体中上、下、左、... 实验名称:图形界面多线程编程实验要求:1、在桌面中设计一个窗体(大小根据自己的计算机进行),在窗体中有若干个个小球(使用绘图工具完成)2、其中小球可以在窗体中上、下、左、右自动移动。考虑使用多线程方式。其中1从上向下,移动到窗体底部后从上面随机位置进入窗体;2从下向上移动到顶部后返回向下移动;3从左向右移动,离开窗体后,从左面再次进入;4水平移动到中间后反方向运动,到窗体边缘重新反方向移动。3、当小球移动离开窗体时,能够实现反弹效果与从反方向进入的效果。考虑如何实验控制结构实现方向的控制。4、不同小球的移动速度可以不同。考虑使用随机函数控制移动速度 展开
 我来答
yugi111
2017-11-15 · TA获得超过8.1万个赞
知道大有可为答主
回答量:5.1万
采纳率:70%
帮助的人:1.2亿
展开全部
package com;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Ball extends JFrame
{
private static final long serialVersionUID = 1L;
private static final int COUNT = 4;
Thread thread;
UFO[] ufos = new UFO[COUNT];
public Ball()
{
setTitle("移动的小球");
setSize(800, 600);
setLayout(new BorderLayout());
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Panel panel = new Panel();
add(panel, BorderLayout.CENTER);
addComponentListener(new ComponentAdapter()
{
@Override
public void componentResized(ComponentEvent e)
{
panel.repaint();
}
});
setVisible(true);
}
class Panel extends JPanel implements Runnable
{
private static final long serialVersionUID = 1L;
public Panel()
{
produce(COUNT);
start();
}
@Override
public void paint(Graphics g)
{
super.paint(g);
Font font = new Font(Font.DIALOG, Font.BOLD, 15);
FontMetrics fm=sun.font.FontDesignMetrics.getMetrics(font);
for(int i = 0; i < ufos.length; i++)
{
UFO u = ufos[i];
g.setColor(u.color);
if(i == 0)
{
u.y += u.j;
if(u.y > getHeight())
{
u.y = 0;
u.x = (int) (Math.random() * getWidth());
}
g.fillOval(u.x, u.y, u.width, u.height);
}
else if(i == 1)
{
int T = u.y + u.j;
int ST = getHeight() - u.height;
if(T > ST)
{
u.j = -u.j;
u.y = ST - u.j;
}
else if(T < 0)
{
u.j = -u.j;
u.y = 0 - u.j;
}
g.fillOval(u.x, u.y += u.j, u.width, u.height);
}
else if(i == 2)
{
u.x += u.i;
if(u.x > getWidth())
{
u.x = 0;
}
g.fillOval(u.x, u.y, u.width, u.height);
}
else if(i == 3)
{
int L = u.x + u.i;
int SL = getWidth() - u.width;
if(L > SL)
{
u.i = -u.i;
u.x = SL - u.i;
}
else if(L < getWidth() / 2)
{
u.i = -u.i;
u.x = getWidth() / 2 - u.i;
}
g.fillOval(u.x += u.i, u.y, u.width, u.height);
}
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString(u.name, u.x + u.width / 2-fm.getWidths()[0]/4, u.y + u.height / 2+fm.getHeight()/4);
}
g.dispose();
}
public void start()
{
if(null == thread)
{
thread = new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
public synchronized void stop()
{
if(null != thread)
{
thread.interrupt();
}
thread = null;
notifyAll();
}
@Override
public void run()
{
Thread me = Thread.currentThread();
while(thread == me && !isShowing() || getSize().width == 0)
{
try
{
Thread.sleep(200);
}
catch(InterruptedException e)
{}
}
while(me == thread)
{
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
break;
}
}
stop();
}
}
private void produce(int n)
{
int[][] pos = {
{ 100, 0, 80, 80 }, { 400, getHeight() - 100, 80, 80 },
{ 0, 100, 80, 80 }, { getWidth() - 100, 300, 80, 80 }
};
for(int i = 0; i < n; i++)
{
int[] arr = new int[3];
for(int j = 0; j < arr.length; j++)
{
arr[j] = (int) (Math.random() * 255);
}
int x = (int) (Math.random() * 10) + 1;
int y = (int) (Math.random() * 10) + 1;
ufos[i] =
new UFO(pos[i][0], pos[i][1], pos[i][2], pos[i][3], i + 1 + "", new Color(arr[0], arr[1], arr[2]),
x, y);
}
}
class UFO
{
int x;
int y;
int width;
int height;
String name;
Color color;
int i;
int j;
public UFO(int x, int y, int width, int height, String name, Color color, int i, int j)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.name = name;
this.color = color;
this.i = i;
this.j = j;
}
}
public static void main(String[] args)
{
new Ball();
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式