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

📄 jccomponent.java

📁 非常好的java collapse游戏代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    	getJCollapseModel().clearStack();    	getJCollapseModel().getGameBoard().clear();    	getJCollapseModel().getGameBoard().setBoardSize( m_Level.getWidth(), m_Level.getHeight() );    	    	for( int i = 0; i < m_Level.countBlocks(); i++ )    		getJCollapseModel().pushBlock( m_Level.getBlock( i ) );    	fireLevelLoaded( m_Level );    }        /**     * Pause or continue the game     * @param pause if true pause the game, otherwise, continue     */    public void setPause( boolean pause ) {    	m_Paused = pause;    	repaint();    	fireGamePaused( isPaused() );    }        /**     * Get state pause in this component     * @return true if game is paused     */    public boolean isPaused() {    	return m_Paused;    }        /**     * Test if game is running     * @return true return true if game is not over     */    public boolean isRunning() {    	return !m_GameOver;    }        /**     * Play the game     */    public void start() {    	terminateEffect();    	fireGameStarted();    	    	m_GameOver = false;    	m_Paused = false;    	    	if( m_RollThread == null || !m_RollThread.isAlive() ) {    		m_RollThread = new Thread( this, "JCComponent" );    		m_RollThread.setDaemon( true );    		m_RollThread.start();    	}    }        /**     * Stop the game!     */    public void stop() {    	m_GameOver = true;    }        /**     * Reset the component     */    public void reset() {    	stop();    	m_JCollapseModel.clearStack();    	m_JCollapseModel.getGameBoard().clear();    	m_Level = null;    	terminateEffect();    	createGraphicsContext();    	repaint();    }        /* (non-Javadoc)     * @see java.awt.Component#paint(java.awt.Graphics)     */    public void paint( Graphics g ) {    	if( getWidth() > m_JCollapseModel.getGameBoard().getBoardWidth() &&    		getHeight() > m_JCollapseModel.getGameBoard().getBoardHeight() &&    		isDrawEnabled() )    	{    		drawJCollapseModel();    		g.drawImage( m_VolatileImageBoard, 0, 0, getWidth(), getHeight(), null );    	    		if( m_ImageEffect != null )    			g.drawImage( m_ImageEffect, 0, 0, getWidth(), getHeight(), null );    	}    }        /* (non-Javadoc)     * @see sourceforge.net.projects.jcollapse.game.effects.EffectObserver#drawImageEffect(java.awt.Image)     */    public void drawImageEffect( Image image ) {    	m_ImageEffect = image;    	repaint();    }        /* (non-Javadoc)     * @see sourceforge.net.projects.jcollapse.game.effects.EffectObserver#endImageEffect()     */    public void endImageEffect() {    	if( m_Effect != null && !m_Effect.isPersistent() ) {    		m_ImageEffect = null;    		repaint();    	}    }        /**     * Draw effect to the game board     * @param effect the Effect object that will be displayed in game board     */    public void drawEffect( Effect effect ) {    	if( m_Effect != null && !m_Effect.isStoped() )    		terminateEffect();    	    	m_Effect = effect;    	m_Effect.SetEffectObserver( this );		m_Effect.draw( getWidth(), getHeight() );    }        /**     * Terminate the image effect event     */    public void terminateEffect() {    	if( m_Effect != null )     		m_Effect.stop();    	m_Effect = null;    	m_ImageEffect = null;    }        private void drawJCollapseModel() {    	int m_BlockWidth;        int m_BlockHeight;        int curX, curY;        int offSetX, offSetY;            	// Determine how to fix the volatile image    	if( m_VolatileImageBoard.validate( m_G2DBoard.getDeviceConfiguration() ) == VolatileImage.IMAGE_INCOMPATIBLE ) {            // Create a new volatile image object;            // this could happen if the component was moved to another device            m_VolatileImageBoard.flush();            m_VolatileImageBoard = m_G2DBoard.getDeviceConfiguration().createCompatibleVolatileImage( getWidth(), getHeight() );            m_G2DBoard = (Graphics2D)m_VolatileImageBoard.getGraphics();            System.out.println( "Volatile image fixed!" );    	}    	    	//if have no level, exit...    	if( getLevel() == null )    		return;    	//draw background image/color        if( getLevel().getBackgroundImage() != null )        	m_G2DBoard.drawImage( getLevel().getBackgroundImage(), 0, 0, getWidth(), getHeight(), null );        else if( getLevel().getBackgroundColor() != null ) {        	m_G2DBoard.setColor( getLevel().getBackgroundColor() );        	m_G2DBoard.fillRect( 0, 0, getWidth(), getHeight() );        } else {        	m_G2DBoard.setColor( Color.BLACK );        	m_G2DBoard.fillRect( 0, 0, getWidth(), getHeight() );        }                //execute if is not paused or if is not hide board on pause        if( !isPaused() || !isHideBoardOnPause() ) {        	        //calculate block width/height	    	m_BlockWidth = getWidth() / m_JCollapseModel.getGameBoard().getBoardWidth();	        m_BlockHeight = getHeight() / ( m_JCollapseModel.getGameBoard().getBoardHeight() + 1 );	        	        //calculate offset	        offSetX = ( getWidth() % m_BlockWidth ) / 2;	        offSetY = ( getHeight() % m_BlockHeight );		        //draw the base Block's grid	        m_G2DBoard.setColor( Color.WHITE );	        for( int i = 0; i < m_JCollapseModel.getGameBoard().getBoardWidth(); i++ )	        	m_G2DBoard.drawRect( offSetX + i * m_BlockWidth, getHeight() - m_BlockHeight - offSetY, m_BlockWidth, m_BlockHeight );	        	        //draw main Block's	        for( int x = 0; x < m_JCollapseModel.getGameBoard().getBoardWidth(); x++ )	        	for( int y = 0; y < m_JCollapseModel.getGameBoard().getBoardHeight(); y++ )	                if( !m_JCollapseModel.getGameBoard().getMainBoard().isNullBlock( x, y ) ) {	                	curX = +offSetX + x * m_BlockWidth;	                	curY = -offSetY + ( getHeight() - ( 2 * m_BlockHeight ) ) - ( y * m_BlockHeight );	                		                	( (DisplayableGraphic) m_JCollapseModel.getGameBoard().getMainBoard().getBlock( x, y ) ).drawBlock(	                			m_G2DBoard,	                			curX,	                			curY,	                			m_BlockWidth,	                			m_BlockHeight );	                }	        	        	        //draw base Block's	        curY = getHeight() - m_BlockHeight - offSetY;	        for( int z = 0; z < m_JCollapseModel.getGameBoard().getBoardWidth(); z++ )	            if( !m_JCollapseModel.getGameBoard().getBaseBoard().isNullBlock( z, 0 ) ) {	            	curX = offSetX + z * m_BlockWidth;	            		            	( (DisplayableGraphic) m_JCollapseModel.getGameBoard().getBaseBoard().getBlock( z, 0 ) ).drawBlock(	            			m_G2DBoard,	            			curX,	            			curY,	            			m_BlockWidth,	            			m_BlockHeight );	            }	        	        //if has more blocks to roll	        if( m_JCollapseModel.hasBlockToRoll() ) {	        	//draw the base Block's over the base Block's	        	m_G2DBoard.setColor( TRANSPARENT_BLACK );	        	m_G2DBoard.fillRect( 0, getHeight() - m_BlockHeight - offSetY, getWidth(), m_BlockHeight + 1 );	        } else {	        	m_G2DBoard.setColor( TRANSPARENT_BLACK );	        	m_G2DBoard.fillRect( 0, getHeight() - m_BlockHeight - offSetY, getWidth() - ( m_HorizontalBlocksCount * m_BlockWidth ), m_BlockHeight + 1 );	        }        }    }        /* (non-Javadoc)     * @see java.lang.Runnable#run()     */    public void run() {    	System.out.println( "Thread " + m_RollThread.getName() + " initialized" );    	    	long ttr;    	//long ttr2;    	while( isRunning() ) {    		ttr = System.nanoTime();    		//ttr2 = System.nanoTime();    					if( m_JCollapseModel.hasBlockToRoll() ) 			{				//roll the block				//if base board is full, the event gameOver is fired and set				//m_GameOver to true ( m_GameOver == !isRunning() )				m_JCollapseModel.rollBlock();								//if game end				if( !isRunning() )					continue;								//fire event when Block was rolled and draw process was finished				fireBlockRolled( m_JCollapseModel.countStackBlocks(), m_JCollapseModel.getGameBoard().isBaseBoardFull() );			} else {				//decrement the number of blocks in horizontal while > 0				if( m_HorizontalBlocksCount-- == 0 )					fireLevelEnd( getLevel() );			}						//repaint the board image			repaint();			ttr = ( System.nanoTime() - ttr ) / 1000000;						try {				//sleep minimum 0 milliseconds (no negative values!)				Thread.sleep( Math.max( getLevel().getRollInterval() - ttr, 0 ) );								//System.out.println( "RunTime: " + ( ( System.nanoTime() - ttr2 ) / 1000000 ) );					    		//if game is paused				while( m_Paused && !m_GameOver )	    			Thread.sleep( SLEEP_TIME_IN_PAUSE_MODE );			} catch( InterruptedException ex1 ) {				continue;			}    	}//while    	    	System.out.println( "Thread " + m_RollThread.getName() + " end" );    	m_RollThread = null;    	    	fireGameStoped();    }}

⌨️ 快捷键说明

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