《计算机知识.ppt》由会员分享,可在线阅读,更多相关《计算机知识.ppt(12页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、实验九实验九实验目的实验目的:1掌握掌握Java Applet程序结构和开发过程程序结构和开发过程2了解了解Applet运行机制运行机制3学会编写学会编写Applet对应的对应的HTML文件,掌文件,掌握从握从HTML文件向文件向Applet传递参数的方法。传递参数的方法。实验内容1Java Applet程序开发过程1)编辑源文件import java.awt.Graphics;import java.applet.Applet;public class HelloWorld2 extends Applet public void paint(Graphics g)g.setColor(Col
2、or.green);g.drawString(Hello World!,25,25);g.drawLine(20,200,20,20);2)检查无误后(注意大小写)保存文件,注意文件名为HelloWorld2.java。3)进入命令行(MS-DOS)方式,运行Java编译器:javac HelloWorld2.java4)如果输出错误信息,则根据错误信息提示的错误所在行返回编辑器进行修改。如果没有输出任何信息此时会在当前目录中生成HelloWorld2.class文件。5)键入如下HTML代码:HelloWorld2 6)检查后保存文件。文件名HelloWorld2.html。7)直接双击这个
3、HTML文件,查看Applet在浏览器中的运行结果。或者是用Appletviewer查看Applet的运行结果:Appletviewer HelloWorld2.html public void paint(Graphics g)/重写 paint方法在窗体上输出图形super.paint(g);g.drawImage(image1,x,y,this);public void run()/改写run方法while(true)x+=10;y+=10;tryThread.sleep(100);/线程挂起100毫秒catch(Exception e)repaint();if(x800|y800)x=
4、0;y=0;3输入运行下面的两个程序,分析运行结果,进一步理解线程import java.awt.*;import javax.swing.*;import java.awt.event.*;public class Animation1 extends JFrameImage girl=getToolkit().getImage(10.gif);Image shower=getToolkit().getImage(pic4.jpg);public int x=500,y=10;public int x1=200,y1=10;public static void main(String arg
5、s)Animation1 ani=new Animation1(Animation);ani.draw();ani.draw1();public Animation1(String s)super(s);setBounds(200,200,600,600);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););repaint();setVisible(true);public void paint(Graphics g)g.drawImage(shower,0,
6、0,600,600,this);g.drawImage(girl,x,y,this);g.drawImage(girl,x1,y1,this);void draw()while(true)y+=10;try Thread.sleep(100);catch(Exception e)repaint();if(y=600)break;void draw1()while(true)y1+=10;tryThread.sleep(100);catch(Exception e)repaint();if(y1=600)break;程序2:import java.awt.*;import javax.swing
7、.*;import java.awt.event.*;public class Animation2 extends JFrameImage girl=getToolkit().getImage(10.gif);Image shower=getToolkit().getImage(pic4.jpg);public int x=500,y=10;public int x1=200,y1=10;JPanel p=new JPanel();public static void main(String args)Animation2 ani=new Animation2(Animation);ani.
8、new XX().start();ani.new YY().start();public Animation(String s)super(s);setBounds(200,200,600,600);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););repaint();add(p,Center);setVisible(true);public void paint(Graphics g)g.drawImage(shower,0,0,600,600,this)
9、;g.drawImage(girl,x,y,this);g.drawImage(girl,x1,y1,this);class XX extends Threadpublic void run()while(true)y+=10;try Thread.sleep(100);catch(Exception e)repaint();if(y=600)break;class YY extends Threadpublic void run()while(true)y1+=10;try Thread.sleep(100);catch(Exception e)repaint();if(y1=600)break;