001: package com.xoetrope.carousel.survey;
002:
003: import java.awt.Color;
004: import java.awt.Dimension;
005: import java.awt.Graphics;
006: import java.awt.Graphics2D;
007: import java.awt.Polygon;
008: import java.awt.RenderingHints;
009: import javax.swing.JComponent;
010:
011: /**
012: * A view of an arrow button being used to scroll the content of components.
013: *
014: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
015: * the GNU Public License (GPL), please see license.txt for more details. If
016: * you make commercial use of this software you must purchase a commercial
017: * license from Xoetrope.</p>
018: * <p> $Revision: 1.5 $</p>
019: */
020: public class XArrowButton extends JComponent {
021:
022: public static final int UP = 1;
023: public static final int DOWN = 2;
024:
025: protected double yScale;
026: protected Color backgroundColor, borderColor;
027: protected int type;
028: protected Polygon shape;
029: protected int width, height;
030:
031: protected static double getScale() {
032: return XRulesEditorPane.SCALE;
033: }
034:
035: public XArrowButton(int w, int h, double ys, Color bgc, Color brc,
036: int t) {
037: super ();
038: width = w;
039: height = h;
040: yScale = ys;
041: backgroundColor = bgc;
042: borderColor = brc;
043: type = t;
044: setSize();
045: }
046:
047: public boolean contains(int x, int y) {
048: return (isEnabled() && (shape != null) && shape.contains(x, y));
049: }
050:
051: public void paintArrow(Graphics2D g) {
052: if (shape != null && isEnabled()) {
053: g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
054: RenderingHints.VALUE_ANTIALIAS_ON);
055: g.setColor(backgroundColor);
056: g.fillPolygon(shape);
057: g.setColor(borderColor);
058: g.drawPolygon(shape);
059: }
060: }
061:
062: public void paint(Graphics g) {
063: if (isEnabled()) {
064: Graphics2D g2d = (Graphics2D) g;
065: paintArrow(g2d);
066: }
067: }
068:
069: public void setSize() {
070: Dimension size = new Dimension(getWidth(), getHeight());
071: setSize(size);
072: setMinimumSize(size);
073: setPreferredSize(size);
074: createShape();
075: }
076:
077: protected void createShape() {
078: int w = getWidth() - 1;
079: int h = getHeight() - 1;
080: int x1 = 0, y1 = 0;
081: int x2 = 0, y2 = 0;
082: int x3 = 0, y3 = 0;
083: int x4 = 0, y4 = 0;
084: int x5 = 0, y5 = 0;
085: if (type == UP) {
086: x1 = w;
087: y1 = h;
088: x2 = w;
089: y2 = (int) ((1 - yScale) * h);
090: x3 = w / 2;
091: y3 = 0;
092: x4 = 0;
093: y4 = (int) ((1 - yScale) * h);
094: x5 = 0;
095: y5 = h;
096: } else if (type == DOWN) {
097: x1 = w;
098: y1 = (int) (yScale * h);
099: x2 = w;
100: y2 = 0;
101: x3 = 0;
102: y3 = 0;
103: x4 = 0;
104: y4 = (int) (yScale * h);
105: x5 = (w / 2);
106: y5 = h;
107: }
108: int[] xPoints = { x1, x2, x3, x4, x5 };
109: int[] yPoints = { y1, y2, y3, y4, y5 };
110: shape = new Polygon(xPoints, yPoints, 5);
111: }
112:
113: public int getWidth() {
114: return (int) (getScale() * width);
115: }
116:
117: public int getHeight() {
118: return (int) (getScale() * height);
119: }
120:
121: }
|