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

📄 texteffect.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.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;/** * Text effect * @author erico */public class TextEffect extends Effect {	private BufferedImage m_BufferedImage;	private Graphics2D m_Graphics2D;		private String m_Text;	private int m_Duration;	private Font m_Font = new Font( "Arial", Font.BOLD, 24 );		private static final Color m_ColorTransparent = new Color( 255, 255, 255, 80 );		/**	 * Constructor	 * @param text the text that will be displayed	 * @param duration the time that effect will be played	 */	public TextEffect( String text, int duration, boolean persistent ) {		super( persistent );		setText( text );		setDuration( duration );	}		public TextEffect( int duration, boolean persistent ) {		this( null, duration, persistent );	}		/**	 * Get time that effect will be played	 * @return the time that effect will be played	 */	public int getDuration() {		return m_Duration;	}		/**	 * Set the time that effect will be played	 * @param duration the time that effect will be played	 */	public void setDuration( int duration ) {		if( duration < 0 )			throw new IllegalArgumentException( "Interval can't be negative!" );				m_Duration = duration;	}		/**	 * Get the font for text that will be displayed	 * @return the font for text that will be displayed	 */	public Font getFont() {		return m_Font;	}		/**	 * Set the font for text that will be displayed	 * @param font the font for text that will be displayed	 */	public void setFont( Font font ) {		if( font == null )			throw new IllegalArgumentException( "Font can't be null!" );				m_Font = font;	}		/**	 * Set text that will be displayed	 * @param text the text that will be displayed	 */	public void setText( String text ) {		m_Text = text;	}		/**	 * Get text that will be displayed	 * @return the text that will be displayed	 */	public String getText() {		return m_Text;	}		private void createGraphicsContext( int width, int height ) {		m_BufferedImage = new BufferedImage( width, height, BufferedImage.TYPE_4BYTE_ABGR_PRE );		m_Graphics2D = m_BufferedImage.createGraphics();		m_Graphics2D.setBackground( new Color( 255, 255, 255, 0 ) );	}		/* (non-Javadoc)	 * @see sourceforge.net.projects.jcollapse.game.effects.Effect#drawEffect(java.awt.Graphics2D)	 */	public void draw( int width, int height ) {		final int w, h, x, y;				if( getText() == null )			throw new NullPointerException( "The text can't be null!" );				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_Graphics2D.setColor( Color.WHITE );		m_Graphics2D.setFont( getFont() );		m_Graphics2D.clearRect( 0, 0, width, height );				w = m_Graphics2D.getFontMetrics().charsWidth( getText().toCharArray(), 0, getText().length() );		h = m_Graphics2D.getFontMetrics().getHeight();		x = width / 2 - w / 2;		y = height / 2 - h / 2;		m_Graphics2D.setColor( m_ColorTransparent );		m_Graphics2D.fillRoundRect( x - 6, y - h + 3, w + 12, h + 3, 8, 8 );				new Thread( new Runnable() {			public void run() {				boolean color = false;				long duration = System.currentTimeMillis() + getDuration();								while( ( getDuration() == 0 || duration > System.currentTimeMillis() ) && !isStoped() ) {					m_Graphics2D.setColor( ( ( color = !color ) ? Color.GRAY : Color.WHITE ) );					m_Graphics2D.drawString( getText(), x, y );					fireImageEffect( m_BufferedImage );					try {						Thread.sleep( 150 );					} catch( InterruptedException ex1 ) {}				}								if( !isStoped() ) {					m_Graphics2D.setColor( Color.BLACK );					m_Graphics2D.drawString( getText(), x, y );					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 + -