Coverage report

| %line | %branch
--- | --- | ---
tsukuba_bunko.peko.canvas.stage.StageCanvas |
0% |
0% |

1 |  |
     /*
2 |  |
      * All Rights Reserved.
3 |  |
      * Copyright (C) 1999-2005 Tsukuba Bunko.
4 |  |
      *
5 |  |
      * Licensed under the BSD License ("the License"); you may not use
6 |  |
      * this file except in compliance with the License.
7 |  |
      * You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *       http://www.tsukuba-bunko.org/licenses/LICENSE.txt
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
16 |  |
      *
17 |  |
      * $Id: StageCanvas.java,v 1.3 2005/07/23 18:53:48 ppoi Exp $
18 |  |
      */
19 |  |
     package tsukuba_bunko.peko.canvas.stage;
20 |  |
21 |  |
     import	java.awt.Dimension;
22 |  |
     import	java.awt.Graphics;
23 |  |
     import	java.awt.Graphics2D;
24 |  |
     import	java.awt.Image;
25 |  |
26 |  |
     import	java.awt.image.BufferedImage;
27 |  |
28 |  |
     import	java.util.Iterator;
29 |  |
     import	java.util.Map;
30 |  |
31 |  |
     import	javax.swing.JComponent;
32 |  |
33 |  |
     import	tsukuba_bunko.peko.Logger;
34 |  |
35 |  |
     import	tsukuba_bunko.peko.resource.ResourceManager;
36 |  |
37 |  |
38 |  |
     /**
39 |  |
      * 舞台（ステージ）を描画するキャンバスです。
40 |  |
      * @author	$Author: ppoi $
41 |  |
      * @version	$Revision: 1.3 $
42 |  |
      */
43 |  |
     public class StageCanvas	extends JComponent	{
44 |  |
45 |  |
     	/**
46 |  |
     	 * serial version UID
47 |  |
     	 */
48 |  |
     	private static final long	serialVersionUID	= -4344927132903559315L;
49 |  |
50 |  |
     	/**
51 |  |
     	 * ステージ
52 |  |
     	 */
53 | 0 |
     	private Stage	_stage = null;
54 |  |
55 |  |
     	/**
56 |  |
     	 * エフェクトを使用するかどうか
57 |  |
     	 */
58 | 0 |
     	private boolean	_usingEffect = false;
59 |  |
60 |  |
     	/**
61 |  |
     	 * エフェクトマップ
62 |  |
     	 */
63 | 0 |
     	private Map	_effects = null;
64 |  |
65 |  |
     	/**
66 |  |
     	 * 描画用オフスクリーンバッファ
67 |  |
     	 */
68 | 0 |
     	private BufferedImage	_screen = null;
69 |  |
70 |  |
     	/**
71 |  |
     	 * 作業用オフスクリーンバッファ
72 |  |
     	 */
73 | 0 |
     	private BufferedImage	_working = null;
74 |  |
75 |  |
     	/**
76 |  |
     	 * size cache
77 |  |
     	 */
78 | 0 |
     	private Dimension	_size = new Dimension();
79 |  |
80 |  |
81 |  |
     	/**
82 |  |
     	 * <code>StageCanvas</code> のインスタンスを作成します。
83 |  |
     	 */
84 |  |
     	public StageCanvas()
85 |  |
     	{
86 | 0 |
     		super();
87 | 0 |
     		initialize();
88 | 0 |
     	}
89 |  |
90 |  |
91 |  |
     	/**
92 |  |
     	 * このキャンバスで描画するステージを設定します。
93 |  |
     	 * @param	stage	ステージ
94 |  |
     	 */
95 |  |
     	public void setStage( Stage stage )
96 |  |
     	{
97 | 0 |
     		if( _stage != null )	{
98 | 0 |
     			_stage.dispose();
99 |  |
     		}
100 |  |
101 | 0 |
     		_stage = stage;
102 | 0 |
     		_stage.setStageCanvas( this );
103 | 0 |
     	}
104 |  |
105 |  |
     	/**
106 |  |
     	 * このキャンバスが現在描画しているステージを取得します。
107 |  |
     	 * @return	ステージ
108 |  |
     	 */
109 |  |
     	public Stage getStage()
110 |  |
     	{
111 | 0 |
     		return _stage;
112 |  |
     	}
113 |  |
114 |  |
     	/**
115 |  |
     	 * エフェクトを使用するかどうかを設定します。
116 |  |
     	 * @param	useEffect	エフェクトを使用する場合 <code>true</code>、それ以外の場合 <code>false</code>
117 |  |
     	 */
118 |  |
     	public void setUsingEffect( boolean useEffect )
119 |  |
     	{
120 | 0 |
     		_usingEffect = useEffect;
121 | 0 |
     	}
122 |  |
123 |  |
     	/**
124 |  |
     	 * エフェクトを使用するかどうかを取得します。
125 |  |
     	 * @return	エフェクトを使用する場合 <code>true</code>、それ以外の場合 <code>false</code>
126 |  |
     	 */
127 |  |
     	public boolean isUsingEffect()
128 |  |
     	{
129 | 0 |
     		return _usingEffect;
130 |  |
     	}
131 |  |
132 |  |
     	/**
133 |  |
     	 * ステージキャンバスを初期化します。
134 |  |
     	 */
135 |  |
     	private void initialize()
136 |  |
     	{
137 | 0 |
     		ResourceManager	resources = ResourceManager.getInstance();
138 | 0 |
     		Dimension	canvasSize = (Dimension)resources.getResource( "peko.canvas.canvas-size" );
139 |  |
140 | 0 |
     		super.setPreferredSize( canvasSize );
141 | 0 |
     		super.setSize( canvasSize );
142 | 0 |
     		_screen = new BufferedImage( canvasSize.width, canvasSize.height, BufferedImage.TYPE_INT_RGB );
143 | 0 |
     		_working = new BufferedImage( canvasSize.width, canvasSize.height, BufferedImage.TYPE_INT_RGB );
144 |  |
145 | 0 |
     		super.setLayout( null );
146 | 0 |
     		setDoubleBuffered( false );
147 |  |
148 | 0 |
     		_effects = new java.util.HashMap( 89 );
149 |  |
150 | 0 |
     		Effect	effect = new tsukuba_bunko.peko.canvas.stage.effect.MofingEffect();
151 | 0 |
     		_effects.put( effect.getName(), effect );
152 |  |
153 | 0 |
     		effect = new tsukuba_bunko.peko.canvas.stage.effect.PataPataEffect();
154 | 0 |
     		_effects.put( effect.getName(), effect );
155 |  |
156 | 0 |
     		effect = (Effect)_effects.get( "immediate" );
157 | 0 |
     		if( effect == null )	{
158 | 0 |
     			effect = new tsukuba_bunko.peko.canvas.stage.effect.ImmediateEffect();
159 | 0 |
     			_effects.put( "immediate", effect );
160 |  |
     		}
161 | 0 |
     		_effects.put( null, effect );
162 | 0 |
     	}
163 |  |
164 |  |
165 |  |
     	/**
166 |  |
     	 * キャンバスの描画内容を更新します。
167 |  |
     	 */
168 |  |
     	public void updateCanvas()
169 |  |
     	{
170 | 0 |
     		updateCanvas( null );
171 | 0 |
     	}
172 |  |
173 |  |
     	/**
174 |  |
     	 * 指定されたエフェクトを用いて、キャンバスの描画内容を更新します。
175 |  |
     	 * @param	effectName	更新時に使用するエフェクト名
176 |  |
     	 */
177 |  |
     	public void updateCanvas( String effectName )
178 |  |
     	{
179 | 0 |
     		if( !isEnabled() )	{
180 | 0 |
     			return;
181 |  |
     		}
182 |  |
183 | 0 |
     		Graphics2D	g2 = _working.createGraphics();
184 | 0 |
     		Dimension	size = getSize( _size );
185 |  |
186 | 0 |
     		if( _stage.getSlide() == null )	{
187 | 0 |
     			Image	image = _stage.getBackgroundImage();
188 | 0 |
     			if( image != null )	{
189 | 0 |
     				g2.drawImage( image, 0, 0, this );
190 | 0 |
     			}
191 |  |
     			else	{
192 | 0 |
     				g2.setColor( _stage.getBackgroundColor() );
193 | 0 |
     				g2.fillRect( 0, 0, size.width, size.height );
194 |  |
     			}
195 |  |
196 | 0 |
     			Map	actors = _stage.getActors();
197 | 0 |
     			if( actors != null )	{
198 | 0 |
     				Iterator	itr = actors.values().iterator();
199 | 0 |
     				Actor	actor = null;
200 | 0 |
     				Image	looks = null;
201 | 0 |
     				int	x = 0;
202 | 0 |
     				int	y = 0;
203 | 0 |
     				while( itr.hasNext() )	{
204 | 0 |
     					actor = (Actor)itr.next();
205 | 0 |
     					looks = actor.getLooksImage();
206 | 0 |
     					if( looks != null )	{
207 | 0 |
     						x = computePosition( actor );
208 | 0 |
     						y = size.height - looks.getHeight( this );
209 | 0 |
     						g2.drawImage( looks, x, y, this );
210 | 0 |
     					}
211 |  |
     				}
212 |  |
     			}
213 | 0 |
     		}
214 |  |
     		else	{
215 | 0 |
     			Image	image = _stage.getSlideImage();
216 | 0 |
     			if( image != null )	{
217 | 0 |
     				g2.drawImage( image, 0, 0, this );
218 | 0 |
     			}
219 |  |
     			else	{
220 | 0 |
     				g2.fillRect( 0, 0, size.width, size.height );
221 |  |
     			}
222 |  |
     		}
223 |  |
224 | 0 |
     		if( _usingEffect )	{
225 | 0 |
     			Effect	effect = (Effect)_effects.get( effectName );
226 | 0 |
     			if( effect != null )	{
227 | 0 |
     				effect.process( this, _screen, _working );
228 | 0 |
     			}
229 |  |
     			else	{
230 | 0 |
     				Logger.error( "[canvas.stage] unsuported effect name. :" + effect );
231 | 0 |
     				_screen.setData( _working.getRaster() );
232 | 0 |
     				repaint();
233 |  |
     			}
234 | 0 |
     			synchronized( this )	{
235 |  |
     				try	{
236 | 0 |
     					wait( 100 );
237 |  |
     				}
238 | 0 |
     				catch( InterruptedException ie )	{
239 | 0 |
     					Logger.debug( "[canvas.stage] interrupted after effect.", ie );
240 | 0 |
     				}
241 | 0 |
     			}
242 | 0 |
     		}
243 |  |
     		else	{
244 | 0 |
     			_screen.setData( _working.getRaster() );
245 | 0 |
     			repaint();
246 |  |
     		}
247 | 0 |
     	}
248 |  |
249 |  |
     	public void paintPageBackground( boolean visiblePage )
250 |  |
     	{
251 | 0 |
     		Graphics2D	g2 = _screen.createGraphics();
252 | 0 |
     		g2.drawImage( _working, 0, 0, this );
253 | 0 |
     		if( visiblePage )	{
254 | 0 |
     			tsukuba_bunko.peko.PekoSystem.getInstance().getCanvasManager().getTextCanvas().paintPageBackground( g2 );
255 |  |
     		}
256 | 0 |
     		g2.dispose();
257 | 0 |
     		repaint();
258 | 0 |
     	}
259 |  |
260 |  |
261 |  |
     	/**
262 |  |
     	 * 立ち位置を計算します。
263 |  |
     	 * @param	position	立ち位置の文字列表現
264 |  |
     	 */
265 |  |
     	private int computePosition( Actor actor )
266 |  |
     	{
267 | 0 |
     		int	halfWidth = actor.getLooksImage().getWidth( this ) / 2;
268 | 0 |
     		Dimension	size = getSize( _size );
269 | 0 |
     		switch( actor.getPosition() )	{
270 |  |
     		case Actor.POSITION_CENTER:
271 | 0 |
     			return ((int)((float)size.width / 2f)) - halfWidth;
272 |  |
     		case Actor.POSITION_LEFT:
273 | 0 |
     			return ((int)((float)size.width / 4f)) - halfWidth;
274 |  |
     		case Actor.POSITION_RIGHT:
275 | 0 |
     			return ((int)((size.width / 4f) * 3f)) - halfWidth;
276 |  |
     		default:
277 | 0 |
     			return (int)actor.getAbsolutePosition() - halfWidth;
278 |  |
     		}
279 |  |
     	}
280 |  |
281 |  |
     //
282 |  |
     //	JComponent の実装
283 |  |
     //
284 |  |
     	protected void paintComponent( Graphics g )
285 |  |
     	{
286 | 0 |
     		g.drawImage( _screen, 0, 0, this );
287 | 0 |
     	}
288 |  |
     }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.