01: /*
02: * @(#)PartialGradientLineBorder.java 4/13/2005
03: *
04: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
05: */
06: package com.jidesoft.swing;
07:
08: import javax.swing.border.AbstractBorder;
09: import java.awt.*;
10:
11: /**
12: * This is a border which allows you to have gradient line only at one side or several sides.
13: */
14: public class PartialGradientLineBorder extends AbstractBorder implements
15: PartialSide {
16:
17: private int _sides = ALL;
18: private Color[] _colors;
19: protected int _thickness;
20:
21: public PartialGradientLineBorder(Color[] colors) {
22: this (colors, 1);
23: }
24:
25: public PartialGradientLineBorder(Color[] colors, int thickness) {
26: this (colors, thickness, ALL);
27: }
28:
29: public PartialGradientLineBorder(Color[] colors, int thickness,
30: int sides) {
31: if (colors.length < 2) {
32: throw new IllegalArgumentException(
33: "Array \"colors\" should have at least 2 elements.");
34: }
35: _colors = colors;
36: _thickness = thickness;
37: _sides = sides;
38: }
39:
40: public int getSides() {
41: return _sides;
42: }
43:
44: public void setSides(int sides) {
45: _sides = sides;
46: }
47:
48: @Override
49: public void paintBorder(Component c, Graphics g, int x, int y,
50: int width, int height) {
51: Color oldColor = g.getColor();
52: Graphics2D g2d = (Graphics2D) g;
53: int i = 0;
54: if ((_sides & NORTH) != 0) {
55: JideSwingUtilities.fillGradient(g2d, new Rectangle(x, y,
56: width, _thickness), _colors[i++], _colors[i++],
57: false);
58: }
59: if ((_sides & SOUTH) != 0) {
60: if (i >= _colors.length) {
61: i -= 2;
62: }
63: JideSwingUtilities.fillGradient(g2d, new Rectangle(x, y
64: + height - _thickness, width, _thickness),
65: _colors[i++], _colors[i++], false);
66: }
67: if ((_sides & WEST) != 0) {
68: if (i >= _colors.length) {
69: i -= 2;
70: }
71: JideSwingUtilities.fillGradient(g2d, new Rectangle(x, y,
72: _thickness, height), _colors[i++], _colors[i++],
73: true);
74: }
75: if ((_sides & EAST) != 0) {
76: if (i >= _colors.length) {
77: i -= 2;
78: }
79: JideSwingUtilities.fillGradient(g2d, new Rectangle(x
80: + width - _thickness, y, _thickness, height),
81: _colors[i++], _colors[i], true);
82: }
83: g.setColor(oldColor);
84: }
85: }
|