001 package com.croftsoft.core.animation.painter; 002 003 import java.awt.Graphics2D; 004 import java.awt.Image; 005 import javax.swing.JComponent; 006 007 import com.croftsoft.core.animation.*; 008 009 /********************************************************************* 010 * Centers the image on the Component without scaling. 011 * 012 * @see 013 * ScalePainter 014 * @see 015 * StretchPainter 016 * 017 * @version 018 * $Date: 2006/05/17 18:28:57 $ 019 * @since 020 * 2006-05-10 021 * @author 022 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 023 *********************************************************************/ 024 025 public final class CenterPainter 026 implements ComponentPainter 027 ////////////////////////////////////////////////////////////////////// 028 ////////////////////////////////////////////////////////////////////// 029 { 030 031 private Image image; 032 033 ////////////////////////////////////////////////////////////////////// 034 // constructor methods 035 ////////////////////////////////////////////////////////////////////// 036 037 public CenterPainter ( final Image image ) 038 ////////////////////////////////////////////////////////////////////// 039 { 040 this.image = image; 041 } 042 043 public CenterPainter ( ) 044 ////////////////////////////////////////////////////////////////////// 045 { 046 // no-arg constructor has empty method body 047 } 048 049 ////////////////////////////////////////////////////////////////////// 050 // accessor/mutator methods 051 ////////////////////////////////////////////////////////////////////// 052 053 public Image getImage ( ) { return image; } 054 055 public void setImage ( final Image image ) { this.image = image; } 056 057 ////////////////////////////////////////////////////////////////////// 058 // interface ComponentPainter method 059 ////////////////////////////////////////////////////////////////////// 060 061 public void paint ( 062 final JComponent component, 063 final Graphics2D graphics ) 064 ////////////////////////////////////////////////////////////////////// 065 { 066 if ( image == null ) 067 { 068 return; 069 } 070 071 final int imageWidth = image.getWidth ( null ); 072 073 final int imageHeight = image.getHeight ( null ); 074 075 final int componentWidth = component.getWidth ( ); 076 077 final int componentHeight = component.getHeight ( ); 078 079 final int x = ( componentWidth - imageWidth ) / 2; 080 081 final int y = ( componentHeight - imageHeight ) / 2; 082 083 graphics.drawImage ( image, x, y, null ); 084 } 085 086 ////////////////////////////////////////////////////////////////////// 087 ////////////////////////////////////////////////////////////////////// 088 }