实验12 java多线程(含答案)(3)

2018-12-22 22:12

int radius=30; Planet()

{ setSize(w,h); setLayout(null);

yellowBall=new Mycanvas();

yellowBall.setColor(Color.yellow); add(yellowBall);

yellowBall.setSize(12,12); yellowBall.setR(12/2);

pointX[0]=0; pointY[0]=-radius;

double angle=1*Math.PI/180; //刻度为1度 for(int i=0;i<359;i++) //计算出数组中各个元素的值

{ pointX[i+1]=pointX[i]*Math.cos(angle)-Math.sin(angle)*pointY[i];

pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle); }

for(int i=0;i<360;i++)

{ pointX[i]=pointX[i]+w/2; //坐标平移 pointY[i]=pointY[i]+h/2; }

yellowBall.setLocation((int)pointX[0]-yellowBall.getR(),

(int)pointY[0]-yellowBall.getR());

【代码2】 //创建 moon线程,当前面板做为该线程的目标对象 }

public void start()

{ try{ moon .start(); }

catch(Exception exe){} }

public void paint(Graphics g) { g.setColor(Color.blue);

g.fillOval(w/2-9,h/2-9,18,18); }

public void run() { int i=0; while(true)

{ i=(i+1)60;

yellowBall.setLocation((int)pointX[i]-yellowBall.getR(),

(int)pointY[i]-yellowBall.getR());

try{ 【代码3】 // Thread类调用类方法sleep使得线程中断10豪秒 }

catch(InterruptedException e){} } } }

HaveThreadFrame.java

import java.awt.*;

import java.awt.event.*;

public class HaveThreadFrame extends Frame implements Runnable { 【代码4】 //用Thread类声明一个rotate对象 Planet earth;

double pointX[]=new double[360], pointY[]=new double[360]; int width,height;

int radius=120; HaveThreadFrame()

{ rotate=new Thread(this); earth=new Planet();

setBounds(0,0,360,400); width=getBounds().width; height=getBounds().height; pointX[0]=0; pointY[0]=-radius;

double angle=1*Math.PI/180; for(int i=0;i<359;i++)

{ pointX[i+1]=pointX[i]*Math.cos(angle)-Math.sin(angle)*pointY[i];

pointY[i+1]=pointY[i]*Math.cos(angle)+pointX[i]*Math.sin(angle); }

for(int i=0;i<360;i++) { pointX[i]=pointX[i]+width/2; pointY[i]=pointY[i]+height/2; }

setLayout(null); setVisible(true); validate();

addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e)

{ System.exit(0); } } ); add(earth);

earth.setLocation((int)pointX[0]-earth.getSize().width/2,

(int)pointY[0]-earth.getSize().height/2); earth.start();

【代码5】 //用rotate调用start方法 }

public void run() { int i=0; while(true)

{ i=(i+1)60;

earth.setLocation((int)pointX[i]-earth.getSize().width/2,

(int)pointY[i]-earth.getSize().height/2); try{ Thread.sleep(100); }

catch(InterruptedException e){} } }

public void paint(Graphics g) { g.setColor(Color.red);

g.fillOval(width/2-15,height/2-15,30,30); } }

HaveThreadFrame.java

public class ThreadRotateMainClass

{ public static void main(String args[]) { new HaveThreadFrame(); } }

? 实验后的练习:

1. 在Planet类中再增加一个Mycanvas对象greenBall和一

个Thread对象Satellite,线程Satellite占有CPU资源期间可以让greenBall画布旋转。

3.双线程接力。 ? 实验要求:

编写一个应用程序,除了主线程外,还有两个线程:first和second。first负责模拟一个红色的按钮从坐标(10,60)运动到(100,60);second负责模拟一个绿色的按钮从坐标(100,60)运动到(200,60)。 ? 程序运行效果示例:

程序运行效果如下图所示:

? 程序模板:

MoveButton.java

import java.awt.*;

import java.awt.event.*;

public class MoveButton extends Frame implements Runnable,ActionListener

{ 【代码1】//用Thread类声明first,second两个线程对象

Button redButton,greenButton,startButton; int distance=10; MoveButton()

{ 【代码2】 //创建first线程,当前窗口做为该线程的目标对象

【代码3】 //创建first线程,当前窗口做为该线程的目标对象

redButton=new Button(); greenButton=new Button();

redButton.setBackground(Color.red); greenButton.setBackground(Color.green); startButton=new Button(\ startButton.addActionListener(this); setLayout(null);


实验12 java多线程(含答案)(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:湖北省融资租赁公司名录2018版118家

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: