From: Kimmo R. <ki...@us...> - 2012-11-21 21:59:37
|
Update of /cvsroot/arianne/stendhal/src/games/stendhal/client/gui In directory vz-cvs-4.sog:/tmp/cvs-serv26253/src/games/stendhal/client/gui Modified Files: StatusDisplayBar.java Log Message: Extended to cope with complicated colour bars Index: StatusDisplayBar.java =================================================================== RCS file: /cvsroot/arianne/stendhal/src/games/stendhal/client/gui/StatusDisplayBar.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** StatusDisplayBar.java 21 Nov 2012 20:17:43 -0000 1.1 --- StatusDisplayBar.java 21 Nov 2012 21:59:35 -0000 1.2 *************** *** 15,21 **** --- 15,23 ---- import java.awt.Dimension; import java.awt.Graphics; + import java.awt.Graphics2D; import java.awt.Insets; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; + import java.awt.image.BufferedImage; import javax.swing.JComponent; *************** *** 28,36 **** public class StatusDisplayBar extends JComponent { /** Preferred height of the bar. */ ! private final int PREFERRED_HEIGHT = 10; /** Value model. */ private final ScalingModel model; /** Default bar color. */ private Color color = Color.WHITE; /** --- 30,45 ---- public class StatusDisplayBar extends JComponent { /** Preferred height of the bar. */ ! private static final int PREFERRED_HEIGHT = 10; /** Value model. */ private final ScalingModel model; /** Default bar color. */ private Color color = Color.WHITE; + /** Painter, or <code>null</code> if a plain color bar is used. */ + private BarPainter painter; + /** + * Image of a full color bar, or <code>null</code> if a plain color bar + * is used. + */ + private BufferedImage barImage; /** *************** *** 54,57 **** --- 63,73 ---- Insets insets = getInsets(); int barWidth = getWidth() - insets.left - insets.right - 2; + if (painter != null) { + int barHeight = getHeight() - insets.top - insets.bottom; + barImage = getGraphicsConfiguration().createCompatibleImage(barWidth, barHeight); + Graphics2D g = barImage.createGraphics(); + painter.paint(g, barWidth, barHeight); + g.dispose(); + } model.setMaxRepresentation(barWidth); } *************** *** 61,64 **** --- 77,89 ---- /** + * Return the value scaling model in use. + * + * @return model + */ + public ScalingModel getModel() { + return model; + } + + /** * Set the color of the bar. * *************** *** 69,72 **** --- 94,106 ---- } + /** + * Set painter for fancy colored bars. + * + * @param painter painter for coloring the template image + */ + protected void setPainter(BarPainter painter) { + this.painter = painter; + } + @Override public void paintComponent(Graphics g) { *************** *** 77,84 **** getHeight() - insets.top - insets.bottom); ! if (color != null) { g.setColor(color); ! g.fillRect(insets.left + 1, insets.top + 1, model.getRepresentation(), getHeight() - insets.top - insets.bottom - 2); } } } --- 111,138 ---- getHeight() - insets.top - insets.bottom); ! int barHeight = getHeight() - insets.top - insets.bottom - 2; ! if (barImage != null) { ! Graphics clipped = g.create(insets.left + 1, insets.top + 1, model.getRepresentation(), barHeight); ! clipped.drawImage(barImage, 0, 0, null); ! clipped.dispose(); ! } else { g.setColor(color); ! g.fillRect(insets.left + 1, insets.top + 1, model.getRepresentation(), barHeight); } } + + /** + * Interface for bars that need more complicated drawing than a simple color + * bar. + */ + public interface BarPainter { + /** + * Fill an area corresponding to a <em>full</em> bar. + * + * @param g graphics + * @param width width of the area + * @param height height of the area + */ + void paint(Graphics2D g, int width, int height); + } } |