多线程编程.docx
- 文档编号:6210179
- 上传时间:2023-01-04
- 格式:DOCX
- 页数:11
- 大小:21KB
多线程编程.docx
《多线程编程.docx》由会员分享,可在线阅读,更多相关《多线程编程.docx(11页珍藏版)》请在冰豆网上搜索。
多线程编程
实验报告
学号:
年月日
系别
专业
班级
姓名
课程名称
课程类型
学时数
实验名称
JAVA基本程序设计结构
实验目的和要求:
1.创建两个线程,每个线程按顺序输出5次“你好”,每个“你好”要标明来自哪个线程及其顺序号。
2.编写一个Applet程序ScreenProtect.java,模拟屏幕保护程序:
屏幕上自动出现由小到大变换的实心圆,每个圆出现的位置和颜色都是随机的,当圆扩大到200像素时将其擦除,重新出现一个新的圆。
程序源代码:
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
publicclassBounceThread
{
publicstaticvoidmain(String[]args)
{
JFrameframe=newBounceThreadFrame();
frame.show();
}
}
classBounceThreadFrameextendsJFrame
{
privateJPanelcanvas;
publicBounceThreadFrame()
{
setSize(300,200);
setTitle("Bounce");
addWindowListener(newWindowAdapter()
{
publicvoidwindowClosing(WindowEvente)
{
System.exit(0);
}
});
ContainercontentPane=getContentPane();
canvas=newJPanel();
contentPane.add(canvas,"Center");
JPanelp=newJPanel();
addButton(p,"Start",newActionListener()
{
publicvoidactionPerformed(ActionEventevt)
{
Ballb=newBall(canvas);
b.start();
}
});
addButton(p,"Close",newActionListener()
{
publicvoidactionPerformed(ActionEventevt)
{
canvas.setVisible(false);
System.exit(0);
}
});
contentPane.add(p,"South");
}
publicvoidaddButton(Containerc,Stringtitle,ActionListenera)
{
JButtonb=newJButton(title);
c.add(b);
b.addActionListener(a);
}
}
classBallextendsThread
{
publicBall(JPanelb)
{
box=b;
}
publicvoiddraw()
{
Graphicsg=box.getGraphics();
g.fillOval(x,y,XSIZE,YSIZE);
g.dispose();
}
publicvoidmove()
{
if(!
box.isVisible())
return;
Graphicsg=box.getGraphics();
g.setXORMode(box.getBackground());
g.fillOval(x,y,XSIZE,YSIZE);
x+=dx;
y+=dy;
Dimensiond=box.getSize();
if(x<0)
{
x=0;
dx=-dx;
}
if(x+XSIZE>=d.width)
{
x=d.width-XSIZE;
dx=-dx;
}
if(y<0)
{
y=0;
dy=-dy;
}
if(y+YSIZE>=d.height)
{
y=d.height-YSIZE;
dy=-dy;
}
g.fillOval(x,y,XSIZE,YSIZE);
g.dispose();}
publicvoidrun()
{try
{
draw();
for(inti=1;i<=1000;i++)
{
move();
sleep(30);
}
}
catch(InterruptedExceptione)
{}
}
privateJPanelbox;
privatestaticfinalintXSIZE=10;
privatestaticfinalintYSIZE=10;
privateintx=0;
privateinty=0;
privateintdx=2;
privateintdy=2;
}
importjava.applet.Applet;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.Panel;
importjava.awt.Shape;
importjava.awt.geom.*;
importjava.awt.*;
importjavax.swing.JFrame;
publicclassscreen_protectextendsAppletimplementsRunnable{
privatestaticfinalPanelPanel=null;
Buttonfirst;
Panelp1;
intcurrentX=10,currentY=10,x=200,y=200;
Threadt=null;
intnewColor;
publicvoidinit1()
{
p1=Panel;
first=newButton("确定");
}
publicvoidinit()
{
t=newThread(this);
}
publicvoidpaint(Graphicsg)
{
g.setColor(newColor(newColor));
g.fillOval(x,y,currentX,currentY);
}
publicvoidstart()
{
t.start();
try
{
Thread.sleep(10);
}
catch(InterruptedExceptione){}
}
publicvoidstop()
{
if(t.isAlive());
//t.stop
}
publicvoidrun()
{
try{
while(true)
{
currentX++;
currentY++;
if((currentX-x)>150||(currentY-y)>100)
{
x=(int)(Math.random()*300);
y=(int)(Math.random()*200);
newColor=(int)(Math.random()*Integer.MAX_VALUE);
currentX=0;
currentY=0;
}
showStatus("("+x+","+y+")"+":
"+newColor+"圆圈在活动");
repaint();
Thread.sleep(10);
}
}
catch(InterruptedExceptione){}
}
}
importjava.awt.*;
importjava.applet.*;
publicclassUseDoubleBufferingextendsAppletimplementsRunnable{
intX,Y,moveX,moveY,width,height;
ThreadnewThread;
ImageOffScreen;
GraphicsdrawOffScreen;
publicvoidinit(){
X=0;//X坐标
Y=0;//Y坐标
moveX=2;//X轴位移
moveY=3;//Y轴位移
width=getSize().width;//Applet的宽度
height=getSize().height;//Applet的高度
OffScreen=createImage(width,height);//建立次画面
drawOffScreen=OffScreen.getGraphics();//取得次画面的绘图类
}
publicvoidstart(){
newThread=newThread(this);//建立新线程
newThread.start();
}
publicvoidstop(){
newThread=null;//将线程设为空
}
publicvoidpaint(Graphicsg){
if(width!
=getSize().width||height!
=getSize().height)
{
//如果Applet大小变化就重新生成次画面
width=getSize().width;
height=getSize().height;
OffScreen=createImage(width,height);
drawOffScreen=OffScreen.getGraphics();
}
//清除次画面(即在次画面上绘制一个与与Applet相同大小的矩形)
drawOffScreen.setColor(Color.black);
drawOffScreen.fillRect(0,0,width,height);
//在次画面上绘制实心园(代表小球)
drawOffScreen.setColor(Color.white);
drawOffScreen.fillOval(X,Y,30,30);
//将次画面贴到主画面上
g.drawImage(OffScreen,0,0,this);
//g.setColor(Color.white);
//g.fillOval(X,Y,30,30);
}
publicvoidupdate(Graphicsg)
{
//update函数只调用paint()函数而不是清除后再调用
paint(g);
}
publicvoidrun(){
while(newThread!
=null){
repaint();//重新绘制图像
try{
Thread.sleep(50);//线程暂停50毫秒
}
catch(InterruptedExceptionE){}
X=X+moveX;//重新计算X、Y坐标
Y=Y+moveY;
//碰到边界时改变递增值造成反弹效果
if(X>=(width-30)){
X=width-30;
moveX=-moveX;
}
if(X<=0){
X=0;
moveX=-moveX;
}
if(Y>=(height-30)){
Y=height-30;
moveY=-moveY;
}
if(Y<=0){
Y=0;
moveY=-moveY;
}
}
}
}
运行结果:
实验总结:
通过本次实验,我对整个多线程这一章节的知识点有了更进一步的认识,把原来比较含糊的概念,现在理的比较清晰,除此之外,通过联系这个题目,我知道了什么是多线程的同步调用,总而言之,在实验课上我收获很大,在以后的实验课当中,我一定会再接再厉,争取做到更好,提高自己的编程能力与思维能力。
成绩
批阅教师
批阅日期
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 多线程 编程