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 * Scales the image to fit the entire Component. 011 * 012 * This class differs from StretchPainter in that it scales both width 013 * and height by the same factor to prevent distortion. 014 * 015 * You can override the default scaling by calling setScale(). 016 * 017 * @see 018 * StretchPainter 019 * 020 * @version 021 * $Date: 2007/02/25 03:30:11 $ 022 * @since 023 * 2006-04-23 024 * @author 025 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 026 **********************************************************************/ 027 028 public final class ScalePainter 029 implements ComponentPainter 030 /////////////////////////////////////////////////////////////////////// 031 /////////////////////////////////////////////////////////////////////// 032 { 033 034 private Image image; 035 036 private double scale; 037 038 /////////////////////////////////////////////////////////////////////// 039 // constructor methods 040 /////////////////////////////////////////////////////////////////////// 041 042 public ScalePainter ( final Image image ) 043 /////////////////////////////////////////////////////////////////////// 044 { 045 this.image = image; 046 } 047 048 public ScalePainter ( ) 049 /////////////////////////////////////////////////////////////////////// 050 { 051 // no-arg constructor has empty method body 052 } 053 054 /////////////////////////////////////////////////////////////////////// 055 // accessor/mutator methods 056 /////////////////////////////////////////////////////////////////////// 057 058 public Image getImage ( ) { return image; } 059 060 public void setImage ( final Image image ) { this.image = image; } 061 062 public void setScale ( final double scale ) { this.scale = scale; } 063 064 /////////////////////////////////////////////////////////////////////// 065 // interface ComponentPainter method 066 /////////////////////////////////////////////////////////////////////// 067 068 public void paint ( 069 final JComponent component, 070 final Graphics2D graphics ) 071 /////////////////////////////////////////////////////////////////////// 072 { 073 if ( image == null ) 074 { 075 return; 076 } 077 078 final int imageWidth = image.getWidth ( null ); 079 080 final int imageHeight = image.getHeight ( null ); 081 082 final int componentWidth = component.getWidth ( ); 083 084 final int componentHeight = component.getHeight ( ); 085 086 int width, height, x, y; 087 088 if ( scale <= 0.0 ) 089 { 090 final double scaleWidth 091 = ( ( double ) componentWidth ) / imageWidth; 092 093 final double scaleHeight 094 = ( ( double ) componentHeight ) / imageHeight; 095 096 if ( scaleWidth < scaleHeight ) 097 { 098 width = componentWidth; // = scaleWidth * imageWidth; 099 100 height = ( int ) Math.round ( scaleWidth * imageHeight ); 101 102 x = 0; 103 104 y = ( componentHeight - height ) / 2; 105 } 106 else 107 { 108 width = ( int ) Math.round ( scaleHeight * imageWidth ); 109 110 height = componentHeight; //= scaleHeight * imageHeight; 111 112 x = ( componentWidth - width ) / 2; 113 114 y = 0; 115 } 116 } 117 else 118 { 119 width = ( int ) Math.round ( scale * imageWidth ); 120 121 height = ( int ) Math.round ( scale * imageHeight ); 122 123 x = ( componentWidth - width ) / 2; 124 125 y = ( componentHeight - height ) / 2; 126 } 127 128 graphics.drawImage ( image, x, y, width, height, null ); 129 } 130 131 /////////////////////////////////////////////////////////////////////// 132 /////////////////////////////////////////////////////////////////////// 133 }