001 package com.croftsoft.core.animation.painter; 002 003 import java.awt.Color; 004 import java.awt.Graphics2D; 005 import java.awt.Shape; 006 import java.io.Serializable; 007 import javax.swing.JComponent; 008 009 import com.croftsoft.core.animation.ComponentPainter; 010 011 /********************************************************************* 012 * Fills an area of the JComponent with a Color. 013 * 014 * <p> 015 * Useful for setting the background Color of a Component. Note that 016 * if you are also using an opaque (non-transparent) background Image, 017 * using a background Color could be a waste of CPU cycles if it is 018 * completely covered by the background Image. 019 * </p> 020 * 021 * <p> 022 * Semi-transparent Colors may be useful for darkening or color 023 * tinting a scene. 024 * </p> 025 * 026 * @version 027 * 2003-08-05 028 * @since 029 * 2002-02-18 030 * @author 031 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 032 *********************************************************************/ 033 034 public final class ColorPainter 035 implements ComponentPainter, Serializable 036 ////////////////////////////////////////////////////////////////////// 037 ////////////////////////////////////////////////////////////////////// 038 { 039 040 private static final long serialVersionUID = 0L; 041 042 // 043 044 private Color color; 045 046 private Shape shape; 047 048 ////////////////////////////////////////////////////////////////////// 049 ////////////////////////////////////////////////////////////////////// 050 051 /********************************************************************* 052 * Main constructor. 053 * 054 * @param color 055 * 056 * If null, the component background color will be used. 057 * 058 * @param shape 059 * 060 * If null, the entire component will be colored. 061 *********************************************************************/ 062 public ColorPainter ( 063 Color color, 064 Shape shape ) 065 ////////////////////////////////////////////////////////////////////// 066 { 067 this.color = color; 068 069 this.shape = shape; 070 } 071 072 public ColorPainter ( Color color ) 073 ////////////////////////////////////////////////////////////////////// 074 { 075 this.color = color; 076 } 077 078 public ColorPainter ( ) 079 ////////////////////////////////////////////////////////////////////// 080 { 081 } 082 083 ////////////////////////////////////////////////////////////////////// 084 // accessor methods 085 ////////////////////////////////////////////////////////////////////// 086 087 public Color getColor ( ) 088 ////////////////////////////////////////////////////////////////////// 089 { 090 return color; 091 } 092 093 public Shape getShape ( ) 094 ////////////////////////////////////////////////////////////////////// 095 { 096 return shape; 097 } 098 099 ////////////////////////////////////////////////////////////////////// 100 // mutator methods 101 ////////////////////////////////////////////////////////////////////// 102 103 public void setColor ( Color color ) 104 ////////////////////////////////////////////////////////////////////// 105 { 106 this.color = color; 107 } 108 109 public void setShape ( Shape shape ) 110 ////////////////////////////////////////////////////////////////////// 111 { 112 this.shape = shape; 113 } 114 115 ////////////////////////////////////////////////////////////////////// 116 ////////////////////////////////////////////////////////////////////// 117 118 public void paint ( 119 JComponent component, 120 Graphics2D graphics ) 121 ////////////////////////////////////////////////////////////////////// 122 { 123 if ( color == null ) 124 { 125 graphics.setColor ( component.getBackground ( ) ); 126 } 127 else 128 { 129 graphics.setColor ( color ); 130 } 131 132 if ( shape == null ) 133 { 134 graphics.fillRect ( 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE ); 135 } 136 else 137 { 138 // graphics.fill(Shape) does not like Integer.MAX_VALUE 139 140 graphics.fill ( shape ); 141 } 142 } 143 144 ////////////////////////////////////////////////////////////////////// 145 ////////////////////////////////////////////////////////////////////// 146 }