BasicArrowButton.java [plain text]
package javax.swing.plaf.basic;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Polygon;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
public class BasicArrowButton extends JButton implements SwingConstants
{
private static int defaultSize = 10;
private static Polygon upIcon = new Polygon(new int[] { 0, 5, 9 },
new int[] { 7, 2, 7 }, 3);
private static Polygon downIcon = new Polygon(new int[] { 1, 5, 9 },
new int[] { 3, 7, 3 }, 3);
private static Polygon leftIcon = new Polygon(new int[] { 7, 3, 7 },
new int[] { 1, 5, 9 }, 3);
private static Polygon rightIcon = new Polygon(new int[] { 3, 7, 3 },
new int[] { 1, 5, 9 }, 3);
protected int direction;
private transient Color shadow = Color.GRAY;
private transient Color darkShadow = Color.DARK_GRAY;
private transient Color highlight = Color.WHITE;
private transient Border buttonBorder = new Border()
{
public Insets getBorderInsets(Component c)
{
return new Insets(2, 2, 2, 2);
}
public boolean isBorderOpaque()
{
return true;
}
public void paintBorder(Component c, Graphics g, int x, int y, int w,
int h)
{
Color saved = g.getColor();
g.setColor(highlight);
g.drawLine(x + 1, y + 1, x + w - 1, y + 1);
g.drawLine(x + 1, y + 1, x + 1, y + h - 1);
g.setColor(shadow);
g.drawLine(x + 1, y + h - 1, x + w - 1, y + h - 1);
g.drawLine(x + w - 1, y + 1, x + w - 1, y + h - 1);
g.setColor(darkShadow);
g.drawLine(x, y + h, x + w, y + h);
g.drawLine(x + w, y, x + w, y + h);
g.setColor(saved);
}
};
public BasicArrowButton(int direction)
{
super();
setBorder(buttonBorder);
setDirection(direction);
}
public BasicArrowButton(int direction, Color background, Color shadow,
Color darkShadow, Color highlight)
{
this(direction);
setBackground(background);
this.shadow = shadow;
this.darkShadow = darkShadow;
this.highlight = highlight;
}
public boolean isFocusTraversable()
{
return false;
}
public int getDirection()
{
return direction;
}
public void setDirection(int dir)
{
this.direction = dir;
}
public void paint(Graphics g)
{
super.paint(g);
Insets insets = getInsets();
Rectangle bounds = getBounds();
int x = insets.left
+ (bounds.width - insets.left - insets.right - defaultSize) / 2;
int y = insets.top
+ (bounds.height - insets.left - insets.right - defaultSize) / 2;
paintTriangle(g, x, y, defaultSize, direction, isEnabled());
}
public Dimension getPreferredSize()
{
Insets insets = getInsets();
int w = defaultSize + insets.left + insets.right;
int h = defaultSize + insets.top + insets.bottom;
return new Dimension(w, h);
}
public Dimension getMinimumSize()
{
return getPreferredSize();
}
public Dimension getMaximumSize()
{
return getPreferredSize();
}
public void paintTriangle(Graphics g, int x, int y, int size, int direction,
boolean isEnabled)
{
Polygon arrow = null;
switch (direction)
{
case NORTH:
arrow = upIcon;
break;
case SOUTH:
arrow = downIcon;
break;
case EAST:
case RIGHT:
arrow = rightIcon;
break;
case WEST:
case LEFT:
arrow = leftIcon;
break;
}
int[] xPoints = arrow.xpoints;
int[] yPoints = arrow.ypoints;
int x1;
int y1;
int x2;
int y2;
x1 = y1 = x2 = y2 = 0;
if (size != defaultSize)
{
float scale = size * 1f / defaultSize;
for (int i = 0; i < 3; i++)
{
xPoints[i] *= scale;
yPoints[i] *= scale;
}
}
g.translate(x, y);
switch (direction)
{
case NORTH:
x1 = xPoints[0] + 2;
y1 = yPoints[0];
y2 = y1;
x2 = xPoints[2] - 1;
break;
case SOUTH:
x1 = xPoints[1];
y1 = yPoints[1] + 1;
x2 = xPoints[2] - 1;
y2 = yPoints[2];
break;
case LEFT:
case WEST:
x1 = xPoints[0] + 1;
y1 = yPoints[0] + 1;
x2 = x1;
y2 = yPoints[2] + 1;
break;
case RIGHT:
case EAST:
x1 = xPoints[2];
y1 = yPoints[2] + 1;
x2 = xPoints[1] - 1;
y2 = yPoints[1] + 1;
break;
}
Color saved = g.getColor();
if (isEnabled)
{
g.setColor(Color.DARK_GRAY);
if (arrow != null)
g.fillPolygon(xPoints, yPoints, 3);
}
else
{
g.setColor(Color.GRAY);
g.fillPolygon(xPoints, yPoints, 3);
g.setColor(Color.WHITE);
g.drawLine(x1, y1, x2, y2);
}
g.setColor(saved);
g.translate(-x, -y);
}
}