001: /*
002: * Created on 21-Sep-2003
003: */
004: package uk.org.ponder.swingutil;
005:
006: import java.awt.RenderingHints;
007:
008: import java.awt.Color;
009: import java.awt.Component;
010: import java.awt.Container;
011: import java.awt.FlowLayout;
012: import java.awt.Graphics;
013: import java.awt.Rectangle;
014: import java.awt.Graphics2D;
015:
016: import javax.swing.BoxLayout; // QQQQQ this can probably be done without Swing
017: import javax.swing.JPanel;
018:
019: /**
020: * A collection of small utility methods useful in dealing with the AWT GUI libraries.
021: * @author Bosmon
022: */
023:
024: public class AWTUtil {
025: public static void labelledOutLayer(Container parent,
026: Component[] labels, Component[] labelled,
027: LayoutSpec layoutspec) {
028: GBLWrap gblw = new GBLWrap(parent);
029: for (int i = 0; i < labels.length; ++i) {
030: if (i == labels.length - 1) {
031: gblw.endcol();
032: }
033: gblw.apply(labels[i], "l");
034: if (i == labels.length - 1) {
035: gblw.endcol();
036: }
037: Component righty = labelled[i];
038: if (righty instanceof JPanel) {
039: ((JPanel) righty).add(layoutspec.getStrut());
040: } else {
041: JPanel newpanel = layoutspec.getPanel();
042: newpanel.add(righty);
043: righty = newpanel;
044: }
045: gblw.endrow().apply(righty, "l");
046: }
047:
048: }
049:
050: public static void flowOutLayer(Container parent,
051: Component[] labels, Component[] labelled,
052: LayoutSpec layoutspec) {
053: parent.setLayout(new BoxLayout(parent, BoxLayout.Y_AXIS));
054: for (int i = 0; i < labels.length; ++i) {
055: JPanel rowpanel = new JPanel(new FlowLayout(
056: FlowLayout.LEFT, 0, 0));
057: rowpanel.add(labels[i]);
058: rowpanel.add(labelled[i]);
059: rowpanel.add(layoutspec.getStrut());
060: parent.add(rowpanel);
061: }
062: }
063:
064: public static void fillRect(Graphics g, int x1, int y1, int width,
065: int height) {
066: if (width < 0) {
067: x1 = x1 + width;
068: width = -width;
069: }
070: if (height < 0) {
071: y1 = y1 + height;
072: height = -height;
073: }
074: g.fillRect(x1, y1, width, height);
075: }
076:
077: public static void drawRect(Graphics g, int x1, int y1, int width,
078: int height) {
079: if (width < 0) {
080: x1 = x1 + width;
081: width = -width;
082: }
083: if (height < 0) {
084: y1 = y1 + height;
085: height = -height;
086: }
087: g.drawRect(x1, y1, width, height);
088: }
089:
090: public static double colorToLuminance(Color c) {
091: // System.out.println(c.toString());
092: double luminance = (0.212671 * c.getRed() + 0.715160
093: * c.getGreen() + 0.072169 * c.getBlue()) / 255;
094: // System.out.println("" + luminance);
095: return luminance;
096: }
097:
098: public static void setClipBounds(Graphics g, Rectangle toset) {
099: g.setClip(toset.x, toset.y, toset.width, toset.height);
100: }
101:
102: /** Sets the clip bounds of the specified graphics context to the intersection of the
103: * two supplied clip bounds.
104: * @param g The graphics object for which the clip bounds are to be adjusted.
105: * @param origbounds The first rectangle to be intersected (the original OS clip
106: * bounds)
107: * @param toset The second rectangle to be intersected.
108: */
109: public static void setClipBounds(Graphics g, Rectangle origbounds,
110: Rectangle toset) {
111: Rectangle intersect = origbounds.intersection(toset);
112: setClipBounds(g, intersect);
113: }
114:
115: public static void expandRectangle(Rectangle toexpand, int amount) {
116: toexpand.x -= amount;
117: toexpand.y -= amount;
118: toexpand.width += 2 * amount;
119: toexpand.height += 2 * amount;
120: }
121:
122: public static void antiAlias(Graphics2D graphics, boolean state) {
123: graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
124: state ? RenderingHints.VALUE_ANTIALIAS_ON
125: : RenderingHints.VALUE_ANTIALIAS_OFF);
126:
127: }
128: }
|