int getHeight(ImageObserver observer)
例1 drawImage
import java.awt.Image; import java.awt.Graphics; public class drawImage extends java.applet.Applet{//这里的public便不是可有可无了 Image img; public void init(){ img=getImage(getCodeBase(),"boy.gif");//getCodeBase()通过获得本类所在的文件的路径来获得图片的绝对URL } public void paint(Graphics g){ int width=img.getWidth(this); int height=img.getHeight(this); g.drawImage(img,20,10,this);//原图 g.drawImage(img,200,10,width/2,height/2,this);//缩小一半 g.drawImage(img,280,10,width*2,height,this);//宽扁图 g.drawImage(img,400,10,width,height*2,this);//瘦高图 } }其中,html文件为drawImage.htm
<html> <h1>this is an applet of java for Image</h1> <applet code="drawImage" width=750 height=180></applet> </html>编译:javac drawImage.java
运行:appletviewer drawImage.htm
运行结果为:
注意:上面示例中,类必须为public,否则,会出现如下异常:
示例2_对图像剪切:定了三个剪切点,clipExample
import java.awt.*; import java.awt.geom.*; import java.applet.*; public class clipExample extends Applet{ public void paint(Graphics g){ Image img=getImage(getCodeBase(),"boy.gif"); int w=img.getWidth(this); int h=img.getHeight(this); //int w=getSize().width; //int h=getSize().height;//大家可以把这两句换上面两句试试 g.fillRect(0,0,w,h); Polygon p=new Polygon(); p.addPoint(0,0); p.addPoint(w/2,h); p.addPoint(w,0); g.setClip(p); g.drawImage(img,0,0,this); } }
第六步,改写paint()方法,即此方法中需根据某些变量来进行绘图。 见例13。3
例13.2 AnimationImage.java,当然,要在源代码同下目录下准备有images[number].gif图片
import java.awt.*; import java.applet.*; public class AnimationImage extends Applet implements Runnable{ Image imageFrame[]; public Thread animationRunner; public int frame_i=0; public void init() { int i=0; imageFrame=new Image[5]; for(i=0;i<imageFrame.length;i++){ imageFrame[i]=getImage(getCodeBase(),"images"+i+".gif"); } } public void start(){ if(animationRunner==null){ animationRunner=new Thread(this); animationRunner.start(); } } public void stop(){ if(animationRunner!=null){ //animationRunner.stop();//已过时 animationRunner=null; } } public void run(){ while(Thread.currentThread()==animationRunner){ repaint();//No.1 statement try{ Thread.sleep(500);//wait } catch(InterruptedException e){ } frame_i=(frame_i+1)%imageFrame.length;//in order to repaint }//end of while } public void paint(Graphics g){ g.drawImage(imageFrame[frame_i],0,0,this); } }下面是:AnimationImage.htm
<html> <body> <h1>This is an applet for Animation with images</h1> <applet code="AnimationImage" width="180" height="80"></applet> <body> </html>
四、用JMF来播放视频
——关于这方面,据网上说是用JMF已过时了,Oracle已停止支持?不过,在这里将相应资料留下,点击打开链接
参考资料:点击打开链接
———————————————————————————————————http://www.oracle.com/technetwork/cn/java/javase/tech/index-jsp-140239-zhs.html
这种涉及到框架的,框架是另外下的,JMF 下载链接如下————————————————http://www.oracle.com/technetwork/java/javase/download-142937.html
1 什么是JMF(Java Media Frame,简称JMF)import java.awt.*; import java.awt.event.*; import javax.media.*; class player extends Frame implements ControllerListener { Player p; Component vc; player(String ss,String mediaurl) { super(ss); try { p = Manager.createPlayer(new MediaLocator( mediaurl)); } catch (NoPlayerException e) { System.out.println("could not find a player."); System.exit(-1); } catch (java.io.IOException e) { System.out.println(e); System.exit(-1); } if(p == null) {System.out.println("trouble crating player."); System.exit(-1);} p.addControllerListener(this); p.prefetch(); } public synchronized void controllerUpdate(ControllerEvent event) { if (event instanceof EndOfMediaEvent) { p.setMediaTime(new Time(0)); p.start(); return; } if (event instanceof PrefetchCompleteEvent) { p.start(); return; } if (event instanceof RealizeCompleteEvent) { vc=p.getVisualComponent(); if (vc != null){ add(vc);} pack(); setResizable(false); setVisible(true); } } public static void main(String args[]) { if(args.length != 1) {System.out.println("error url"); return;} player nowFrame=new player("player example1",args[0]); nowFrame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); } }