📄 colorblock.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.blocks;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import sourceforge.net.projects.jcollapse.engine.block.SimpleBlock;
/**
* This is a ColorBlock class.
* @author erico
*/
public class ColorBlock extends SimpleBlock implements DisplayableGraphic {
private boolean m_Composite;
private Color m_Color;
private BufferedImage m_BufferedImage;
/**
* Constructor
* @param name the name of Block
* @param enable enable or not (can be touched or not)
* @param color color of this Block
* @param minToRemove minimum quantity of same Blocks that can be removed
*/
public ColorBlock( String name, boolean enable, int minToRemove, Color color ) {
this( name, enable, minToRemove, color, true );
}
/**
* Constructor
* @param name the name of Block
* @param enable enable or not (can be touched or not)
* @param minToRemove minimum quantity of same Blocks that can be removed
* @param color color of this Block
* @param composite set true if you plan to use gradient fill
*/
public ColorBlock( String name, boolean enable, int minToRemove, Color color, boolean composite ) {
super( name, enable, minToRemove );
setComposite( composite );
setColor( color );
}
/**
* This Block can have gradient fill to display a beautiful image (slow) or
* plan color image (fast)
* @return true if component have gradient fill. Plan image, return false
*/
public boolean isComposite() {
return m_Composite;
}
/**
* This Block can have gradient fill to display a beautiful image (slow) or
* plan color image (fast)
* @param composite set true if you plan to use gradient fill
* To use plan image, set false
*/
public void setComposite( boolean composite ) {
m_Composite = composite;
if( composite )
setColor( getColor() );
else
m_BufferedImage = null;
}
/**
* Set the Color of this Block
* @param color the Color for this object
*/
public void setColor( Color color ) {
if( color == null || ( getColor() != null && getColor().equals( color ) ) ) {
return;
}
m_Color = color;
//if composite is true
if( isComposite() ) {
Graphics2D g;
m_BufferedImage = new BufferedImage( 32, 32, BufferedImage.TYPE_INT_RGB );
g = (Graphics2D)m_BufferedImage.getGraphics();
g.setPaint( new GradientPaint( 1, 1, getColor().brighter(), 30, 30, getColor().darker() ) );
g.fillRoundRect( 1, 1, 30, 30, 3, 3 );
g.setPaint( new GradientPaint( 0, 0, Color.WHITE, 31, 31, getColor().brighter() ) );
g.drawRoundRect( 0, 0, 32, 32, 3, 3 );
}
}
/**
* Get the Color for this Block
* @return the color for this Block
*/
public Color getColor() {
return m_Color;
}
/* (non-Javadoc)
* @see sourceforge.net.projects.jcollapse.game.blocks.PrintableBlock#drawBlock(java.awt.Graphics, int, int, int, int)
*/
public void drawBlock(Graphics2D graphics, int x, int y, int width, int height) {
if( isComposite() ) {
graphics.drawImage( m_BufferedImage, x, y, width, height, null );
} else {
graphics.setColor( getColor() );
graphics.fillRoundRect( x+1, y+1, width-2, height-2, width / 3, height / 3 );
graphics.setColor( Color.WHITE );
graphics.drawRoundRect( x, y, width, height, width / 3, height / 3 );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -