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

📄 fadeimageeffect.java

📁 非常好的java collapse游戏代码
💻 JAVA
字号:
/* * JCollapse - Java Collapse Game * Copyright (C) 2005 Erico Gon鏰lves Rimoli *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. *  * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. */package sourceforge.net.projects.jcollapse.game.effects;import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.image.BufferedImage;import javax.swing.ImageIcon;/** * Fade image effect can be used in wait process like intro game screen * @author erico */public class FadeImageEffect extends Effect {	private static final int EFFECT_INTERVAL = 35;		private BufferedImage m_BufferedImage;	private Graphics2D m_Graphics2D;	public static enum Fade { IN, OUT };		private ImageIcon m_ImageIcon = new ImageIcon();	private Image m_Image;	private final Fade m_Fade;		/**	 * Constructor	 * @param image the image that will be displayed	 */	public FadeImageEffect( Image image, Fade fade, boolean persistent ) {		super( persistent );		setImage( image );		m_Fade = fade;	}		/**	 * Set image that will be displayed	 * @param image the image that will be displayed	 */	public void setImage( Image image ) {		m_Image = image;	}		/**	 * Get image that will be displayed	 * @return the image that will be displayed	 */	public Image getImage() {		return m_Image;	}		private void createGraphicsContext( int width, int height ) {		m_BufferedImage = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );		m_Graphics2D = m_BufferedImage.createGraphics();		m_Graphics2D.setBackground( new Color( 255, 255, 255 ) );	}		/* (non-Javadoc)	 * @see sourceforge.net.projects.jcollapse.game.effects.Effect#drawEffect(java.awt.Graphics2D)	 */	public void draw( final int width, final int height ) {		if( getImage() == null )			throw new NullPointerException( "The image can't be null!" );				new Thread( new Runnable() {			public void run() {				long timeDraw;								setRunning( true );								//if necessary, create a new graphic context...				if( m_BufferedImage == null ||						m_BufferedImage.getWidth() != width ||						m_BufferedImage.getHeight() != height )				{					createGraphicsContext( width, height );				}								m_ImageIcon.setImage( getImage().getScaledInstance( width, height, Image.SCALE_FAST ) );								//FADE IN				//----------------------------------------------------------------------------------------				if( m_Fade == Fade.IN ) {					for( int x = 0; x <= width / 2 && !isStoped(); x+=3 ) {						timeDraw = System.nanoTime();												m_Graphics2D.drawImage( m_ImageIcon.getImage(), 0, 0, x, height, 0, 0, x, height, null );						m_Graphics2D.drawImage( m_ImageIcon.getImage(), width, 0, width - x, height, width, 0, width - x, height, null );												fireImageEffect( m_BufferedImage );						try {							Thread.sleep( Math.max( EFFECT_INTERVAL - ( ( System.nanoTime() - timeDraw ) ) / 1000000, 0 ) );						} catch( InterruptedException ex1 ) {}					}										if( !isStoped() ) {						m_Graphics2D.drawImage( m_ImageIcon.getImage(), 0, 0, width, height, null );						fireImageEffect( m_BufferedImage );					}				} else {				//FADE OUT				//----------------------------------------------------------------------------------------					m_Graphics2D.setColor( Color.BLACK );					for( int x = 0; x <= width / 2 && !isStoped(); x+=3 ) {						timeDraw = System.nanoTime();												m_Graphics2D.drawImage( m_ImageIcon.getImage(), 0, 0, null );												m_Graphics2D.fillRect( 0, 0, x, height );						m_Graphics2D.fillRect( width - x, 0, x, height );												fireImageEffect( m_BufferedImage );						try {							Thread.sleep( Math.max( EFFECT_INTERVAL - ( ( System.nanoTime() - timeDraw ) ) / 1000000, 0 ) );						} catch( InterruptedException ex1 ) {}					}										if( !isStoped() ) {						m_Graphics2D.fillRect( 0, 0, width, height );						fireImageEffect( m_BufferedImage );					}									}								setRunning( false );				fireEndImageEffect();				System.out.println( "Thread terminated: " + Thread.currentThread().getName() );			}		}, getClass().getSimpleName() ).start();	}}

⌨️ 快捷键说明

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