1 5 package prefuse.util.display; 6 7 import java.awt.Container ; 8 import java.awt.Graphics2D ; 9 import java.awt.Image ; 10 import java.awt.MediaTracker ; 11 import java.awt.Toolkit ; 12 import java.awt.geom.AffineTransform ; 13 14 import prefuse.Display; 15 import prefuse.util.io.IOLib; 16 17 28 public class BackgroundPainter implements PaintListener { 29 30 private static final double THRESH = 0.01; 31 32 private Image m_img; 33 private boolean m_fixed; 34 private boolean m_tiled; 35 36 private AffineTransform m_identity; 37 private Clip m_clip; 38 39 51 public BackgroundPainter(String imageLocation, boolean fixed, boolean tile) 52 { 53 this(Toolkit.getDefaultToolkit() 54 .getImage(IOLib.urlFromString(imageLocation)), 55 fixed, tile); 56 } 57 58 67 public BackgroundPainter(Image image, boolean fixed, boolean tile) { 68 m_img = image; 69 70 MediaTracker mt = new MediaTracker (new Container ()); 72 mt.addImage(m_img, 0); 73 try { 74 mt.waitForID(0); 75 } catch ( Exception e ) { e.printStackTrace(); } 76 mt.removeImage(m_img, 0); 77 78 m_fixed = fixed; 79 m_tiled = tile; 80 } 81 82 86 public void prePaint(Display d, Graphics2D g) { 87 AffineTransform at = g.getTransform(); 88 boolean translate = isTranslation(at); 89 90 if ( m_fixed || translate ) 91 { 92 97 int tx = m_fixed ? 0 : (int)at.getTranslateX(); 98 int ty = m_fixed ? 0 : (int)at.getTranslateY(); 99 100 g.setTransform(getIdentity()); 101 if ( m_tiled ) { 102 int w = d.getWidth(), iw = m_img.getWidth(null); 104 int h = d.getHeight(), ih = m_img.getHeight(null); 105 106 int sx = m_fixed ? 0 : tx%iw; 107 int sy = m_fixed ? 0 : ty%ih; 108 if ( sx > 0 ) sx -= iw; 109 if ( sy > 0 ) sy -= ih; 110 111 for ( int x=sx; x<w-sx; x+=iw ) { 112 for ( int y=sy; y<h-sy; y+=ih ) 113 g.drawImage(m_img, x, y, null); 114 } 115 } else { 116 g.drawImage(m_img, tx, ty, null); 118 } 119 g.setTransform(at); 120 } 121 else 122 { 123 if ( m_tiled ) { 125 int iw = m_img.getWidth(null); 126 int ih = m_img.getHeight(null); 127 128 Clip c = getClip(); 130 c.setClip(0, 0, d.getWidth(), d.getHeight()); 131 c.transform(d.getInverseTransform()); 132 133 int w = (int)Math.ceil(c.getWidth()); 135 int h = (int)Math.ceil(c.getHeight()); 136 int tx = (int)c.getMinX(); 137 int ty = (int)c.getMinY(); 138 int dw = tx%iw + iw; 139 int dh = ty%ih + ih; 140 tx -= dw; w += dw; 141 ty -= dh; h += dh; 142 143 for ( int x=tx; x<tx+w; x+=iw ) { 145 for ( int y=ty; y<ty+h; y+=ih ) 146 g.drawImage(m_img, x, y, null); 147 } 148 } else { 149 g.drawImage(m_img, 0, 0, null); 151 } 152 } 153 154 } 155 156 160 private static boolean isTranslation(AffineTransform at) { 161 return ( Math.abs(at.getScaleX()-1.0) < THRESH && 162 Math.abs(at.getScaleY()-1.0) < THRESH && 163 Math.abs(at.getShearX()) < THRESH && 164 Math.abs(at.getShearY()) < THRESH ); 165 } 166 167 170 private AffineTransform getIdentity() { 171 if ( m_identity == null ) 172 m_identity = new AffineTransform (); 173 return m_identity; 174 } 175 176 179 private Clip getClip() { 180 if ( m_clip == null ) 181 m_clip = new Clip(); 182 return m_clip; 183 } 184 185 189 public void postPaint(Display d, Graphics2D g) { 190 } 192 193 } | Popular Tags |