1 7 package javax.swing.plaf.basic; 8 9 import java.awt.Dimension ; 10 import java.awt.Graphics ; 11 import java.awt.Color ; 12 13 import javax.swing.*; 14 import javax.swing.plaf.UIResource ; 15 16 31 public class BasicArrowButton extends JButton implements SwingConstants 32 { 33 protected int direction; 34 35 private Color shadow; 36 private Color darkShadow; 37 private Color highlight; 38 39 public BasicArrowButton(int direction, Color background, Color shadow, 40 Color darkShadow, Color highlight) { 41 super(); 42 setRequestFocusEnabled(false); 43 setDirection(direction); 44 setBackground(background); 45 this.shadow = shadow; 46 this.darkShadow = darkShadow; 47 this.highlight = highlight; 48 } 49 50 public BasicArrowButton(int direction) { 51 this(direction, UIManager.getColor("control"), UIManager.getColor("controlShadow"), 52 UIManager.getColor("controlDkShadow"), UIManager.getColor("controlLtHighlight")); 53 } 54 55 public int getDirection() { return direction; } 56 57 public void setDirection(int dir) { direction = dir; } 58 59 public void paint(Graphics g) { 60 Color origColor; 61 boolean isPressed, isEnabled; 62 int w, h, size; 63 64 w = getSize().width; 65 h = getSize().height; 66 origColor = g.getColor(); 67 isPressed = getModel().isPressed(); 68 isEnabled = isEnabled(); 69 70 g.setColor(getBackground()); 71 g.fillRect(1, 1, w-2, h-2); 72 73 if (getBorder() != null && !(getBorder() instanceof UIResource )) { 75 paintBorder(g); 76 } else if (isPressed) { 77 g.setColor(shadow); 78 g.drawRect(0, 0, w-1, h-1); 79 } else { 80 g.drawLine(0, 0, 0, h-1); 82 g.drawLine(1, 0, w-2, 0); 83 84 g.setColor(highlight); g.drawLine(1, 1, 1, h-3); 86 g.drawLine(2, 1, w-3, 1); 87 88 g.setColor(shadow); g.drawLine(1, h-2, w-2, h-2); 90 g.drawLine(w-2, 1, w-2, h-3); 91 92 g.setColor(darkShadow); g.drawLine(0, h-1, w-1, h-1); 94 g.drawLine(w-1, h-1, w-1, 0); 95 } 96 97 if(h < 5 || w < 5) { 99 g.setColor(origColor); 100 return; 101 } 102 103 if (isPressed) { 104 g.translate(1, 1); 105 } 106 107 size = Math.min((h - 4) / 3, (w - 4) / 3); 109 size = Math.max(size, 2); 110 paintTriangle(g, (w - size) / 2, (h - size) / 2, 111 size, direction, isEnabled); 112 113 if (isPressed) { 115 g.translate(-1, -1); 116 } 117 g.setColor(origColor); 118 119 } 120 121 public Dimension getPreferredSize() { 122 return new Dimension (16, 16); 123 } 124 125 public Dimension getMinimumSize() { 126 return new Dimension (5, 5); 127 } 128 129 public Dimension getMaximumSize() { 130 return new Dimension (Integer.MAX_VALUE, Integer.MAX_VALUE); 131 } 132 133 public boolean isFocusTraversable() { 134 return false; 135 } 136 137 public void paintTriangle(Graphics g, int x, int y, int size, 138 int direction, boolean isEnabled) { 139 Color oldColor = g.getColor(); 140 int mid, i, j; 141 142 j = 0; 143 size = Math.max(size, 2); 144 mid = (size / 2) - 1; 145 146 g.translate(x, y); 147 if(isEnabled) 148 g.setColor(darkShadow); 149 else 150 g.setColor(shadow); 151 152 switch(direction) { 153 case NORTH: 154 for(i = 0; i < size; i++) { 155 g.drawLine(mid-i, i, mid+i, i); 156 } 157 if(!isEnabled) { 158 g.setColor(highlight); 159 g.drawLine(mid-i+2, i, mid+i, i); 160 } 161 break; 162 case SOUTH: 163 if(!isEnabled) { 164 g.translate(1, 1); 165 g.setColor(highlight); 166 for(i = size-1; i >= 0; i--) { 167 g.drawLine(mid-i, j, mid+i, j); 168 j++; 169 } 170 g.translate(-1, -1); 171 g.setColor(shadow); 172 } 173 174 j = 0; 175 for(i = size-1; i >= 0; i--) { 176 g.drawLine(mid-i, j, mid+i, j); 177 j++; 178 } 179 break; 180 case WEST: 181 for(i = 0; i < size; i++) { 182 g.drawLine(i, mid-i, i, mid+i); 183 } 184 if(!isEnabled) { 185 g.setColor(highlight); 186 g.drawLine(i, mid-i+2, i, mid+i); 187 } 188 break; 189 case EAST: 190 if(!isEnabled) { 191 g.translate(1, 1); 192 g.setColor(highlight); 193 for(i = size-1; i >= 0; i--) { 194 g.drawLine(j, mid-i, j, mid+i); 195 j++; 196 } 197 g.translate(-1, -1); 198 g.setColor(shadow); 199 } 200 201 j = 0; 202 for(i = size-1; i >= 0; i--) { 203 g.drawLine(j, mid-i, j, mid+i); 204 j++; 205 } 206 break; 207 } 208 g.translate(-x, -y); 209 g.setColor(oldColor); 210 } 211 212 } 213 214 | Popular Tags |