Assignment4生成随机图形

上传人:飞*** 文档编号:4828409 上传时间:2017-08-26 格式:DOC 页数:6 大小:64.50KB
返回 下载 相关 举报
Assignment4生成随机图形_第1页
第1页 / 共6页
Assignment4生成随机图形_第2页
第2页 / 共6页
Assignment4生成随机图形_第3页
第3页 / 共6页
Assignment4生成随机图形_第4页
第4页 / 共6页
Assignment4生成随机图形_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《Assignment4生成随机图形》由会员分享,可在线阅读,更多相关《Assignment4生成随机图形(6页珍藏版)》请在金锄头文库上搜索。

1、Assignment 3 生成随机图形作业一在屏幕上随机图形,要求图形要随机,位置要随机。生成图形时,图形必须是三种以上。一、 题目需要知识(1) 图形的生成;(2) 随机数的产生;二、可能会用到的类和方法:(1) Random rgen = new Random(); /产生随机数(2) Color col=new Color(int r, int g, int b); / 创建一个颜色对象(3) fillOval(int x, int y, int width, int height) /创建一个椭圆,其参考点为(x,y)(4) fillRect(int x, int y, int wid

2、th, int height) /创建一个矩形,其参考点为(x,y)(5) fillPolygon(int xPoints, int yPoints, int nPoints) / 创建一个 x 和 y 数组坐标定义的闭合多边形,如生成一个三角形:int a = rgen.nextInt(100), rgen.nextInt(100), rgen.nextInt(100) ,;int b = rgen.nextInt(100), rgen.nextInt(100), rgen.nextInt(100) ;g.fillPolygon(a, b, 3);(6) Thread.sleep(doubl

3、e milliseconds) /暂停毫秒import java.awt.*;import java.util.*;import java.applet.*;public class RandomShapes extends Applet private static final int getWidth = 300; / 屏幕宽度private static final int getHeight = 300; / 屏幕高度private static final int Max_LEN = 100; / 最大宽度或高度static Random rgen = new Random();pu

4、blic void init() this.setSize( );this.setVisible( );/* 使用 paint 方法将随机图形添加到画布中 */public void paint(Graphics g) /* 添加随机数个图形 */for (int i = 0; i = rgen.nextInt(100); i+) /* 图形显示的位置 */int x = rgen.nextInt( );int y = rgen.nextInt( );/* 图形宽度和高度 */int w = rgen.nextInt(Max_LEN);int h = rgen.nextInt(Max_LEN)

5、;/* 随机产生颜色 */Color col = new Color(rgen.nextInt(255), rgen.nextInt(255), rgen.nextInt(255);int randShape = rgen.nextInt( ); / 五种图形/* 创建随机图形 */if (randShape = 1) / 生成圆if (randShape = 2) / 生成椭圆if (randShape = 3) / 生成矩形if (randShape = 4) / 生成三角形if (randShape = 5) / / 生成正方形/*暂停毫秒*/try Thread.sleep(1000)

6、; catch (InterruptedException e) 作业二一设计一个简易计算器可进行加、减、乘、除等简单的四则运算二参考文件:import java.awt.*;import java.awt.event.*;public class zy2 extends Frame implements ActionListenerstatic zy2 frm = new zy2();static TextField tf1 = new TextField(10);static TextField tf2 = new TextField(10);static TextField tf3 =

7、new TextField(10);static Panel pan1 = new Panel();static Button b1 = new Button(+);static Button b2 = new Button(-);static Button b3 = new Button(*);static Button b4 = new Button(/);static Panel pan2 = new Panel();public static void main(String args) frm.setLayout(new FlowLayout();frm.setSize(250, 2

8、00);pan1.setBackground(Color.lightGray);pan1.setLayout(new GridLayout(3,1);pan1.add(new Label(操作数);pan1.add(tf1);pan1.add(new Label(操作数);pan1.add(tf2);pan1.add(new Label(结果);pan1.add(tf3);frm.add(pan1);pan2.setBackground(Color.lightGray);pan2.setLayout(new GridLayout(4,1);pan2.add(b1);pan2.add(b2);p

9、an2.add(b3);pan2.add(b4);frm.add(pan2);b1.addActionListener(frm);b2.addActionListener(frm);b3.addActionListener(frm);b4.addActionListener(frm);frm.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0););frm.setVisible(true);public void actionPerformed(ActionEv

10、ent e) Button bb = (Button) e.getSource();int s1,s2;s1=Integer.parseInt(tf1.getText();s2=Integer.parseInt(tf2.getText();if (bb = b1)tf3.setText(Integer.toString(s1+s2);if (bb = b2)tf3.setText(Integer.toString(s1-s2);if (bb = b3)tf3.setText(Integer.toString(s1*s2);if (bb = b4)double p1,p2;p1=Double.parseDouble(tf1.getText();p2=Double.parseDouble(tf2.getText();if(p2!=0)tf3.setText(Double.toString(p1/p2);elsetf3.setText(除数不能为零);类 zy2 的运行结果:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 研究报告 > 综合/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号