《Java程序设计》实 验 报 告(六)
实验名称:java编程基础 |
实验地点:10-306 |
所使用的工具软件及环境: JDK1.7或1.8与Eclipse |
|
一、实验目的:
中使用的动画。 |
|
二、实验内容: 1.实现一个红色反弹球的程序,当球撞击边框时,球从边框弹回并以相反方向运动。 编辑 |
|
三、源代码 //Ball.java package实验6; import java.awt.*; import java.applet.*; publicclassBallextends Applet implements Runnable{ intX,Y; public Ball() { } Thread draw=null; publicvoid init() { draw=new Thread(this); setBackground(Color.green); } publicvoid paint(Graphics g) { g.setColor(Color.red); g.fillOval(X, Y, 20, 20); } publicvoid start() { draw.start(); try { Thread.sleep(50); }catch(InterruptedExceptione) {} } publicvoid stop() { draw=null; } publicvoid run() { intx=(int)(Math.random()*10)%2+3; inty=(int)(Math.random()*10)%2+3; try { while(true) { X=X+x; Y=Y+y; if(Y>getHeight()-40||Y<0) y=-y; if(X>getHeight()-40||X<0) x=-x; repaint(); Thread.sleep(100); } }catch(InterruptedException e) {} } } 四、实验结果测试、收获与体会: 编辑编辑 能够运行小程序,能够完成初步线程的实现,对线程编辑有了进一步的学习。 |
实验名称:java编程基础 |
实验地点:10-306 |
所使用的工具软件及环境: JDK1.7或1.8与Eclipse |
|
一、实验目的:
中使用的动画。 |
|
二、实验内容: 2 实现一个时钟, 效果如图所示: 编辑 参考代码: import java.util.*; import java.awt.*; import java.applet.*; import java.text.*; public class Clock extends Applet implements Runnable { private volatile Thread timer; // 显示时钟的线程 private int lastxs, lastys, lastxm, lastym, lastxh, lastyh; // 用于绘制指针 private SimpleDateFormat formatter; // 用于日期格式化 private String lastdate; // 显示日期的字符串 private Font clockFaceFont; // 钟面字体 private Date currentDate; // 当前日期 private Color handColor; // 指针颜色 private Color numberColor; // 秒针和数字的颜色 private int xcenter = 80, ycenter = 55; // 中心位置 public void init() { int x,y; lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0; formatter = new SimpleDateFormat ("yyyy MMM dd hh:mm:ss ", Locale.getDefault()); currentDate = new Date(); lastdate = formatter.format(currentDate); clockFaceFont = new Font("Serif", Font.PLAIN, 14); handColor = Color.blue; numberColor = Color.darkGray; try { setBackground(new Color(Integer.parseInt(getParameter("bgcolor"), 16))); } catch (NullPointerException e) { } catch (NumberFormatException e) { } try { handColor = new Color(Integer.parseInt(getParameter("fgcolor1"), 16)); } catch (NullPointerException e) { } catch (NumberFormatException e) { } try { numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"), 16)); } catch (NullPointerException e) { } catch (NumberFormatException e) { } resize(400,400); // 设置窗体大小 } // 绘制时钟 public void update(Graphics g) { int xh, yh, xm, ym, xs, ys; int s = 0, m = 10, h = 10; String today; currentDate = new Date(); formatter.applyPattern("s"); try { s = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { s = 0; } formatter.applyPattern("m"); try { m = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { m = 10; } formatter.applyPattern("h"); try { h = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { h = 10; } // 设置指针位置 xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter); ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter); xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter); ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter); xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + xcenter); yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + ycenter); // 时钟下面显示的日期时间字符串 formatter.applyPattern("yyyy MMM dd HH:mm:ss "); today = formatter.format(currentDate); g.setFont(clockFaceFont); g.setColor(getBackground()); if (xs != lastxs || ys != lastys) { g.drawLine(xcenter, ycenter, lastxs, lastys); g.drawString(lastdate, 5, 125); } if (xm != lastxm || ym != lastym) { g.drawLine(xcenter, ycenter-1, lastxm, lastym); g.drawLine(xcenter-1, ycenter, lastxm, lastym); } if (xh != lastxh || yh != lastyh) { g.drawLine(xcenter, ycenter-1, lastxh, lastyh); g.drawLine(xcenter-1, ycenter, lastxh, lastyh); } g.setColor(numberColor); g.drawString(today, 5, 125); // 绘制指针 g.drawLine(xcenter, ycenter, xs, ys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, xm, ym); g.drawLine(xcenter-1, ycenter, xm, ym); g.drawLine(xcenter, ycenter-1, xh, yh); g.drawLine(xcenter-1, ycenter, xh, yh); lastxs = xs; lastys = ys; lastxm = xm; lastym = ym; lastxh = xh; lastyh = yh; lastdate = today; currentDate = null; } public void paint(Graphics g) { g.setFont(clockFaceFont); //绘制圆形钟面 g.setColor(handColor); g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360); g.setColor(numberColor); g.drawString("9", xcenter-45, ycenter+3); g.drawString("3", xcenter+40, ycenter+3); g.drawString("12", xcenter-5, ycenter-37); g.drawString("6", xcenter-3, ycenter+45); // 绘制指针 g.setColor(numberColor); g.drawString(lastdate, 5, 125); g.drawLine(xcenter, ycenter, lastxs, lastys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, lastxm, lastym); g.drawLine(xcenter-1, ycenter, lastxm, lastym); g.drawLine(xcenter, ycenter-1, lastxh, lastyh); g.drawLine(xcenter-1, ycenter, lastxh, lastyh); } //启动线程 public void start() { //补充完整 } public void stop() { // //补充完整 } public void run() { //补充完整 } } |
|
三、源代码 //Clock.java package实验6; import java.util.*; import java.awt.*; import java.applet.*; import java.text.*; publicclassClockextends Applet implements Runnable { privatevolatile Thread timer; // 显示时钟的线程 privateintlastxs, lastys, lastxm, lastym, lastxh, lastyh; // 用于绘制指针 private SimpleDateFormat formatter; // 用于日期格式化 private String lastdate; // 显示日期的字符串 private Font clockFaceFont; // 钟面字体 private Date currentDate; // 当前日期 private Color handColor; // 指针颜色 private Color numberColor; // 秒针和数字的颜色 privateintxcenter = 80, ycenter = 55; // 中心位置 publicvoid init() { intx,y; lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0; formatter = new SimpleDateFormat ("yyyy MMM dd hh:mm:ss ", Locale.getDefault()); currentDate = new Date(); lastdate = formatter.format(currentDate); clockFaceFont = new Font("Serif", Font.PLAIN, 14); handColor = Color.blue; numberColor = Color.darkGray; try { setBackground(new Color(Integer.parseInt(getParameter("bgcolor"), 16))); } catch (NullPointerException e) { } catch (NumberFormatException e) { } try { handColor = new Color(Integer.parseInt(getParameter("fgcolor1"), 16)); } catch (NullPointerException e) { } catch (NumberFormatException e) { } try { numberColor = new Color(Integer.parseInt(getParameter("fgcolor2"), 16)); } catch (NullPointerException e) { } catch (NumberFormatException e) { } resize(400,400); // 设置窗体大小 } // 绘制时钟 publicvoid update(Graphics g) { intxh, yh, xm, ym, xs, ys; ints = 0, m = 10, h = 10; String today; currentDate = new Date(); formatter.applyPattern("s"); try { s = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { s = 0; } formatter.applyPattern("m"); try { m = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { m = 10; } formatter.applyPattern("h"); try { h = Integer.parseInt(formatter.format(currentDate)); } catch (NumberFormatException n) { h = 10; } // 设置指针位置 xs = (int) (Math.cos(s * Math.PI / 30 - Math.PI / 2) * 45 + xcenter); ys = (int) (Math.sin(s * Math.PI / 30 - Math.PI / 2) * 45 + ycenter); xm = (int) (Math.cos(m * Math.PI / 30 - Math.PI / 2) * 40 + xcenter); ym = (int) (Math.sin(m * Math.PI / 30 - Math.PI / 2) * 40 + ycenter); xh = (int) (Math.cos((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + xcenter); yh = (int) (Math.sin((h*30 + m / 2) * Math.PI / 180 - Math.PI / 2) * 30 + ycenter); // 时钟下面显示的日期时间字符串 formatter.applyPattern("yyyy MMM dd HH:mm:ss "); today = formatter.format(currentDate); g.setFont(clockFaceFont); g.setColor(getBackground()); if (xs != lastxs || ys != lastys) { g.drawLine(xcenter, ycenter, lastxs, lastys); g.drawString(lastdate, 5, 125); } if (xm != lastxm || ym != lastym) { g.drawLine(xcenter, ycenter-1, lastxm, lastym); g.drawLine(xcenter-1, ycenter, lastxm, lastym); } if (xh != lastxh || yh != lastyh) { g.drawLine(xcenter, ycenter-1, lastxh, lastyh); g.drawLine(xcenter-1, ycenter, lastxh, lastyh); } g.setColor(numberColor); g.drawString(today, 5, 125); // 绘制指针 g.drawLine(xcenter, ycenter, xs, ys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, xm, ym); g.drawLine(xcenter-1, ycenter, xm, ym); g.drawLine(xcenter, ycenter-1, xh, yh); g.drawLine(xcenter-1, ycenter, xh, yh); lastxs = xs; lastys = ys; lastxm = xm; lastym = ym; lastxh = xh; lastyh = yh; lastdate = today; currentDate = null; } publicvoid paint(Graphics g) { g.setFont(clockFaceFont); //绘制圆形钟面 g.setColor(handColor); g.drawArc(xcenter-50, ycenter-50, 100, 100, 0, 360); g.setColor(numberColor); g.drawString("9", xcenter-45, ycenter+3); g.drawString("3", xcenter+40, ycenter+3); g.drawString("12", xcenter-5, ycenter-37); g.drawString("6", xcenter-3, ycenter+45); // 绘制指针 g.setColor(numberColor); g.drawString(lastdate, 5, 125); g.drawLine(xcenter, ycenter, lastxs, lastys); g.setColor(handColor); g.drawLine(xcenter, ycenter-1, lastxm, lastym); g.drawLine(xcenter-1, ycenter, lastxm, lastym); g.drawLine(xcenter, ycenter-1, lastxh, lastyh); g.drawLine(xcenter-1, ycenter, lastxh, lastyh); } //启动线程 publicvoid start() { //补充完整 timer = new Thread(this); //用当前对象为参数创建线程 timer.start(); } publicvoid stop() { // //补充完整 timer = null; } publicvoid run() { //补充完整 Thread me = Thread.currentThread(); while (timer == me) { try { Thread.currentThread().sleep(100); } catch (InterruptedException e) { } repaint(); //休眠一段时间后重绘窗体 } } } 四、实验结果测试、收获与体会: 编辑 补充实验内容中的代码,覆写了Runnable接口中的run()方法,并且实例化Thread类对象,也运用sleep()使线程睡眠等。通过该实验更加对线程的程序设计有了更深入的学习,对今后的学习有了较大的帮助。 |