1 21 package net.mlw.vlh.swing.support; 22 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Graphics ; 26 27 import javax.swing.Icon ; 28 import javax.swing.SwingConstants ; 29 30 34 public class ArrowIcon implements Icon 35 { 36 private int width = 4; 37 38 private int height = 8; 39 40 private int numberOfArrows; 41 42 private int[] xPoints = new int[4]; 43 44 private int[] yPoints = new int[4]; 45 46 public ArrowIcon(int direction) 47 { 48 this(direction, 1); 49 } 50 51 public ArrowIcon(int direction, int numberOfArrows) 52 { 53 this.numberOfArrows = numberOfArrows; 54 if (direction == SwingConstants.LEFT) 55 { 56 xPoints[0] = width; 57 yPoints[0] = -1; 58 xPoints[1] = width; 59 yPoints[1] = height; 60 xPoints[2] = 0; 61 yPoints[2] = height / 2; 62 xPoints[3] = 0; 63 yPoints[3] = height / 2 - 1; 64 } 65 else if (direction == SwingConstants.RIGHT) 66 { 67 xPoints[0] = 0; 68 yPoints[0] = -1; 69 xPoints[1] = 0; 70 yPoints[1] = height; 71 xPoints[2] = width; 72 yPoints[2] = height / 2; 73 xPoints[3] = width; 74 yPoints[3] = height / 2 - 1; 75 } 76 else 77 { 78 throw new RuntimeException ("Valid directions: SwingConstants.LEFT, SwingConstants.RIGHT."); 79 } 80 } 81 82 public int getIconHeight() 83 { 84 return height; 85 } 86 87 public int getIconWidth() 88 { 89 return (width * numberOfArrows); 90 } 91 92 public void paintIcon(Component c, Graphics g, int x, int y) 93 { 94 int length = xPoints.length; 95 96 for (int number = 0; number < numberOfArrows; number++) 97 { 98 int adjustedXPoints[] = new int[length]; 99 int adjustedYPoints[] = new int[length]; 100 101 for (int i = 0; i < length; i++) 102 { 103 adjustedXPoints[i] = xPoints[i] + x + (number*width); 104 adjustedYPoints[i] = yPoints[i] + y; 105 } 106 107 if (c.isEnabled()) 108 { 109 g.setColor(Color.black); 110 } 111 else 112 { 113 g.setColor(Color.gray); 114 } 115 116 g.fillPolygon(adjustedXPoints, adjustedYPoints, length); 117 } 118 } 119 120 } | Popular Tags |