⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animator.java

📁 linux 下的源代码分析阅读器 red hat公司新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	return result;    }        /**     * Get the dimensions of an image.     * @return the image's dimensions.     */    synchronized Dimension getImageDimensions(Image im)    throws ImageNotFoundException {	// Get the width of the image.	int width;	int height;		while ((width = im.getWidth(this)) < 0) {	    try {		wait();	    } catch (InterruptedException e) { }	    if (imageLoadError) {		throw new ImageNotFoundException(im.getSource());	    }	}		// Get the height of the image.	while ((height = im.getHeight(this)) < 0) {	    try {		wait();	    } catch (InterruptedException e) { }	    if (imageLoadError) {		throw new ImageNotFoundException(im.getSource());	    }	}	return new Dimension(width, height);    }    /**     * Stuff a range of image names into a Vector.     * @return a Vector of image URLs.     */    Vector prepareImageRange(int startImage, int endImage)    throws MalformedURLException {	Vector result = new Vector(Math.abs(endImage - startImage) + 1);	if (startImage > endImage) {	    for (int i = startImage; i >= endImage; i--) {		result.addElement(new URL(imageSource, "T"+i+".gif"));	    }	} else {	    for (int i = startImage; i <= endImage; i++) {		result.addElement(new URL(imageSource, "T"+i+".gif"));	    }	}	return result;    }        /**     * Initialize the applet.  Get parameters.     */    public void init() {	try {	    String param = getParameter("IMAGESOURCE");		    imageSource = (param == null) ? getDocumentBase() : new URL(getDocumentBase(), param + "/");	    dbg("IMAGESOURCE = "+param);		    param = getParameter("PAUSE");	    globalPause =		(param != null) ? Integer.parseInt(param) : defaultPause;	    dbg("PAUSE = "+param);	    param = getParameter("REPEAT");	    repeat = (param == null) ? true : (param.equalsIgnoreCase("yes") ||					       param.equalsIgnoreCase("true"));	    int startImage = 1;	    int endImage = 1;	    param = getParameter("ENDIMAGE");	    dbg("ENDIMAGE = "+param);	    if (param != null) {		endImage = Integer.parseInt(param);		param = getParameter("STARTIMAGE");		dbg("STARTIMAGE = "+param);		if (param != null) {		    startImage = Integer.parseInt(param);		}		images = prepareImageRange(startImage, endImage);	    } else {		param = getParameter("STARTIMAGE");		dbg("STARTIMAGE = "+param);		if (param != null) {		    startImage = Integer.parseInt(param);		    images = prepareImageRange(startImage, endImage);		} else {		    param = getParameter("IMAGES");		    if (param == null) {			showStatus("No legal IMAGES, STARTIMAGE, or ENDIMAGE "+				   "specified.");			return;		    } else {			images = parseImages(param);		    }		}	    }	    param = getParameter("BACKGROUND");	    dbg("BACKGROUND = "+param);	    if (param != null) {		backgroundImageURL = new URL(imageSource, param);	    }	    param = getParameter("STARTUP");	    dbg("STARTUP = "+param);	    if (param != null) {		startUpImageURL = new URL(imageSource, param);	    }	    param = getParameter("SOUNDSOURCE");	    soundSource = (param == null) ? imageSource : new URL(getDocumentBase(), param + "/");	    dbg("SOUNDSOURCE = "+param);		    param = getParameter("SOUNDS");	    dbg("SOUNDS = "+param);	    if (param != null) {		sounds = parseSounds(param, images);	    }	    param = getParameter("PAUSES");	    dbg("PAUSES = "+param);	    if (param != null) {		durations = parseDurations(param, images);	    }	    param = getParameter("POSITIONS");	    dbg("POSITIONS = "+param);	    if (param != null) {		positions = parsePositions(param, images);	    }	    param = getParameter("SOUNDTRACK");	    dbg("SOUNDTRACK = "+param);	    if (param != null) {		soundtrackURL = new URL(soundSource, param);	    }	} catch (MalformedURLException e) {	    showParseError(e);	} catch (ParseException e) {	    showParseError(e);	}		setFrameNum(0);    }    void tellLoadingMsg(String file, String fileType) {	showStatus("Animator: loading "+fileType+" "+abridge(file, 20));    }    void tellLoadingMsg(URL url, String fileType) {	tellLoadingMsg(url.toExternalForm(), fileType);    }    void clearLoadingMessage() {	showStatus("");    }        /**     * Cut the string down to length=len, while still keeping it readable.     */    static String abridge(String s, int len) {	String ellipsis = "...";	if (len >= s.length()) {	    return s;	}	int trim = len - ellipsis.length();	return s.substring(0, trim / 2)+ellipsis+	    s.substring(s.length() - trim / 2);    }        void loadError(URL badURL, String fileType) {	String errorMsg = "Animator: Couldn't load "+fileType+" "+	    badURL.toExternalForm();	showStatus(errorMsg);	System.err.println(errorMsg);	error = true;	repaint();    }    void showParseError(Exception e) {	String errorMsg = "Animator: Parse error: "+e;	showStatus(errorMsg);	System.err.println(errorMsg);	error = true;	repaint();    }    void startPlaying() {	if (soundtrack != null) {	    soundtrack.loop();	}    }    void stopPlaying() {	if (soundtrack != null) {	    soundtrack.stop();	}    }    /**     * Run the animation. This method is called by class Thread.     * @see java.lang.Thread     */    public void run() {	Thread me = Thread.currentThread();	me.setPriority(Thread.MIN_PRIORITY);	if (! loaded) {	    try {		// ... to do a bunch of loading.		if (startUpImageURL != null) {		    tellLoadingMsg(startUpImageURL, imageLabel);		    startUpImage = getImage(startUpImageURL);		    try {			updateMaxDims(getImageDimensions(startUpImage));		    } catch (Exception e) {			loadError(startUpImageURL, "start-up image");		    }		    resize(maxWidth, maxHeight);		    repaint();		}		if (backgroundImageURL != null) {		    tellLoadingMsg(backgroundImageURL, imageLabel);		    backgroundImage = getImage(backgroundImageURL);		    repaint();		    try {			updateMaxDims(			   getImageDimensions(backgroundImage));		    } catch (Exception e) {			loadError(backgroundImageURL, "background image");		    }		}		URL badURL = fetchImages(images);		if (badURL != null) {		    loadError(badURL, imageLabel);		    return;		}		if (soundtrackURL != null && soundtrack == null) {		    tellLoadingMsg(soundtrackURL, imageLabel);		    soundtrack = getAudioClip(soundtrackURL);		    if (soundtrack == null) {			loadError(soundtrackURL, "soundtrack");			return;		    }		}		if (sounds != null) {		    badURL = fetchSounds(sounds);		    if (badURL != null) {			loadError(badURL, soundLabel);			return;		    }		}		clearLoadingMessage();		offScrImage = createImage(maxWidth, maxHeight);		offScrGC = offScrImage.getGraphics();		offScrGC.setColor(Color.lightGray);		resize(maxWidth, maxHeight);		loaded = true;		error = false;	    } catch (Exception e) {		error = true;		e.printStackTrace();	    }	}	if (userPause) {	    return;	}	if (repeat || frameNum < images.size()) {	    startPlaying();	}	try {	    if (images.size() > 1) {		while (maxWidth > 0 && maxHeight > 0 && engine == me) {		    if (frameNum >= images.size()) {			if (!repeat) {			    return;			}			setFrameNum(0);		    }		    repaint();		    if (sounds != null) {			AudioClip clip =			    (AudioClip)sounds.get(frameNumKey);			if (clip != null) {			    clip.play();			}		    }		    try {			Integer pause = null;			if (durations != null) {			    pause = (Integer)durations.get(frameNumKey);			}			if (pause == null) {			    Thread.sleep(globalPause);			} else {			    Thread.sleep(pause.intValue());			}		    } catch (InterruptedException e) {			// Should we do anything?		    }		    setFrameNum(frameNum+1);		}	    }	} finally {	    stopPlaying();	}    }    /**     * Paint the current frame.     */    public void paint(Graphics g) {	if (error || !loaded) {	    if (startUpImage != null) {		g.drawImage(startUpImage, 0, 0, this);	    } else {		if (backgroundImage != null) {		    g.drawImage(backgroundImage, 0, 0, this);		} else {		    g.clearRect(0, 0, maxWidth, maxHeight);		}	    }	} else {	    if ((images != null) && (images.size() > 0)) {		if (frameNum < images.size()) {		    if (backgroundImage == null) {			offScrGC.fillRect(0, 0, maxWidth, maxHeight);		    } else {			offScrGC.drawImage(backgroundImage, 0, 0, this);		    }		    Image image = (Image)images.elementAt(frameNum);		    Point pos = null;		    if (positions != null) {			pos = (Point)positions.get(frameNumKey);		    }		    if (pos != null) {			xPos = pos.x;			yPos = pos.y;		    }		    offScrGC.drawImage(image, xPos, yPos, this);		    g.drawImage(offScrImage, 0, 0, this);		} else {		    // no more animation, but need to draw something		    dbg("No more animation; drawing last image.");		    g.drawImage((Image)images.lastElement(), 0, 0, this);		}	    }	}    }    /**     * Start the applet by forking an animation thread.     */    public void start() {	if (engine == null) {	    engine = new Thread(this);	    engine.start();	}    }    /**     * Stop the insanity, um, applet.     */    public void stop() {	if (engine != null && engine.isAlive()) {	    engine.stop();	}	engine = null;    }    /**     * Pause the thread when the user clicks the mouse in the applet.     * If the thread has stopped (as in a non-repeat performance),     * restart it.     */    public boolean handleEvent(Event evt) {	if (evt.id == Event.MOUSE_DOWN) {	    if (loaded) {		if (engine != null && engine.isAlive()) {		    if (userPause) {			engine.resume();			startPlaying();		    } else {			engine.suspend();			stopPlaying();		    }		    userPause = !userPause;		} else {		    userPause = false;		    setFrameNum(0);		    engine = new Thread(this);		    engine.start();		}	    }	    return true;	} else {	    	    return super.handleEvent(evt);	}    }    }class ParseException extends Exception {    ParseException(String s) {	super(s);    }}class ImageNotFoundException extends Exception {    ImageNotFoundException(ImageProducer source) {	super(source+"");    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -