北大计算机系java讲义--第二章java小程序(编辑修改稿)内容摘要:

try{ URL url=new URL(str)。 }catch(MalformedURLException( e) { DisplayErrorMessage()。 } • URL类的基本方法 String getProtocol(), String getHost(), ing getPort(), String getFile(), String getRef() 31 URL类 • 构造 URL的实例 import。 import。 public class Test { URL url1,url2,url3。 void test() { try { url1= new URL(“file:/D:/image/”)。 url2= new URL(“ url1= new URL(url2, “”)。 }catch (MalformedURLException e)。 //处理例外 } }} 32 载入现有图像文件 Image类 • java支持 gif和 jpg两种格式的图像 • 图像文件的 URL: URL picurl= new URL (“ • 取一幅图像构成图像对象 Image img1 = getImage(picurl)。 Image img2 = getImage(getCodeBase(), “”)。 33 载入现有图像文件 • 显示一幅图像 : (img1, x, y, this)。 (img1, x, y, this)。 (image1, x, y,x2,y2, this)。 规定背景 规定尺寸 34 载入现有图像文件 • 完整的过程 不要忘记 AWT包 定义 Image对象了吗 ? 指定图像的 URL了吗 ? 把图像取出来吧 . 还记得画图像用什么方法和命令吗 ? 在类中 在 init0中 在 paint0中 35 载入现有图像文件 import .*。 import .*。 public class image extends Applet { Image img。 public void init() { img=getImage(getCodeBase(),)。 } public void paint(Graphics g) { int width=(this)。 int height=(this)。 (52,52,width+30,height+30)。 (img,57,57,width+20,height+20,this)。 }} 36 动态效果 线程的应用 动态效果 线程的应用 • 什么是线程 ? 线程是执行中的程序中的单个顺序控制流 . • Java支持多线程 开始 显示进度 引出最后结果 数学运算 线程 1 线程 2 37 动态效果 线程的应用 • 静态的情况 import .*。 import。 public class maguee extends Applet { public void paint(Graphics g) { (Hello, Java!,0,0)。 } } 38 动态效果 线程的应用 • 动态的情况 (不是多线程 ) public void init() { x=size().width。 y=size().height/2。 width=x。 } public void paint(Graphics g) { while(true) { (Hello, Java!,x,y)。 x=10。 if(x0) x=width。 } } 39 动态效果 线程的应用 • 实现一个线程 让 Applet类去实现 Runable接口 ,创建一个线程类 改写方法 start,在其中产生一个新的线程来工作 改写 stop方法 ,在其中编写结束线程的程序代码 引入新的方法 ,将分给线程的工作写到 run中 40 动态效果 线程的应用 第一步:实现 Runable接口 public class xc extends implements Runnable { Thread smallthread=null。 … } Thread是一个类 ,只有是它的实例才能具有线程的功能 主函数中要定义一个线程变量 41 动态效果 线程的应用 第二步:改写方法 start public void start() { if( smallthread == null) { smallthread= new Thread( this); ()。 //从现在开始程序由两个线程在执行 }} 第三步:改写 stop方法 public void stop() { ()。 //停止线程 smallthread = null。 //释放线程对象 } 42 动态效果 线程的应用 第四步 :新的方法 run 将让线程要做的事放 run中 public void run() { while (true) { repaint()。 try {(1000)。 } catch(InterruptedException e){} } } 43 动态效果 线程的应用 import .*。 import。 public class MovingCharacter extends Applet implements Runnable { int x=200。 Thread my_thread=null。 // public void start() { my_thread=new Thread(this)。 ()。 } public void run() {。
阅读剩余 0%
本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。