View Javadoc

1   package net.sourceforge.jpotpourri.gui.panel.brushed;
2   
3   import java.awt.Dimension;
4   import java.awt.Graphics;
5   import java.awt.Graphics2D;
6   import java.awt.Image;
7   import java.beans.PropertyChangeEvent;
8   import java.beans.PropertyChangeListener;
9   
10  import javax.swing.JComponent;
11  import javax.swing.JPanel;
12  import javax.swing.plaf.ComponentUI;
13  import javax.swing.plaf.basic.BasicPanelUI;
14  
15  
16  /**
17   * 
18   * @author http://blog.elevenworks.com/?p=10
19   */
20  public abstract class TiledImagePanelUI extends BasicPanelUI implements PropertyChangeListener {
21  	
22      private static Image image;
23  
24      public TiledImagePanelUI() {
25          /* nothing to do */
26      }
27  
28      public TiledImagePanelUI(final Image aImage) {
29          image = aImage;
30      }
31  
32      @SuppressWarnings("unused")
33      public static ComponentUI createUI(final JComponent component) {
34          return new DefaultTiledImagePanelUI();
35      }
36  
37      @Override
38  	public final void installUI(final JComponent c) {
39          super.installUI(c);
40  
41          if (image == null) {
42              if (c instanceof TiledImagePanel) {
43                  image = ((TiledImagePanel) c).getImage();
44                  c.addPropertyChangeListener("image", this);
45              } else {
46                  throw new RuntimeException("TiledImagePanelUI must be used with a TiledImagePanel or " +
47                  		"the image must be set explicitly");
48              }
49          }
50      }
51  
52      @Override
53  	protected final void uninstallDefaults(final JPanel p) {
54          p.removePropertyChangeListener("image", this);
55          super.uninstallDefaults(p);
56      }
57  
58      // -----------------------------------------------------------------------------------------------------------------
59      //  Custom painting methods
60      // -----------------------------------------------------------------------------------------------------------------
61  
62      protected abstract boolean overridesPaint();
63      protected abstract void overriddenPaint(final Graphics g, final JComponent c);
64      
65      protected final void actualPaint(final Graphics g, final JComponent c) {
66      	if (image == null) {
67              return;
68          }
69          Dimension vSize = c.getSize();
70  
71          Graphics2D g2d = (Graphics2D) g;
72  
73          for (int x = 0; x < vSize.width; x += 200) {
74              for (int y = 0; y < vSize.height; y += 200) {
75                  g2d.drawImage(image, x, y, null);
76              }
77          }
78      }
79      
80      @Override
81  	public final void paint(final Graphics g, final JComponent c) {
82          if(this.overridesPaint()) {
83          	this.overriddenPaint(g, c);
84          } else {
85          	this.actualPaint(g, c);
86          }
87      }
88  
89      // -----------------------------------------------------------------------------------------------------------------
90      //  Implementation of PropertyChangeListener interface
91      // -----------------------------------------------------------------------------------------------------------------
92  
93      public final void propertyChange(final PropertyChangeEvent evt) {
94          if ("image".equals(evt.getPropertyName())) {
95              image = (Image) evt.getNewValue();
96          }
97      }
98      
99  
100     /**
101      * @author christoph_pickl@users.sourceforge.net
102      */
103     public static class DefaultTiledImagePanelUI extends TiledImagePanelUI {
104 		@Override
105 		@SuppressWarnings("unused")
106 		protected final void overriddenPaint(final Graphics g, final JComponent c) {
107 			// nothing to do
108 		}
109 		@Override
110 		protected final boolean overridesPaint() {
111 			return false;
112 		}
113     	
114     }
115 }