1 21 22 package org.armedbear.j; 23 24 import java.awt.Color ; 25 import java.awt.Dimension ; 26 import java.awt.Graphics ; 27 import java.awt.Rectangle ; 28 import javax.swing.JButton ; 29 import javax.swing.JComponent ; 30 import javax.swing.UIManager ; 31 import javax.swing.plaf.ComponentUI ; 32 import javax.swing.plaf.basic.BasicScrollBarUI ; 33 34 public final class ScrollBarUI extends BasicScrollBarUI  35 { 36 public static ComponentUI createUI(JComponent c) 37 { 38 return new ScrollBarUI(); 39 } 40 41 protected void paintThumb(Graphics g, JComponent c, Rectangle thumbBounds) 42 { 43 if (thumbBounds.isEmpty() || !scrollbar.isEnabled()) 44 return; 45 int w = thumbBounds.width; 46 int h = thumbBounds.height; 47 g.translate(thumbBounds.x, thumbBounds.y); 48 g.setColor(thumbHighlightColor); 49 g.drawLine(0, 0, 0, h - 2); g.drawLine(0, 0, w - 2, 0); g.setColor(thumbDarkShadowColor); 52 g.drawLine(w - 1, 0, w - 1, h - 1); g.drawLine(0, h - 1, w - 1, h - 1); g.setColor(thumbLightShadowColor); 55 g.drawLine(w - 2, 1, w - 2, h - 2); g.drawLine(1, h - 2, w - 2, h - 2); g.setColor(thumbColor); 59 g.fillRect(1, 1, w - 3, h - 3); 60 g.translate(-thumbBounds.x, -thumbBounds.y); 62 } 63 64 protected JButton createDecreaseButton(int orientation) 65 { 66 return new ArrowButton(orientation); 67 } 68 69 protected JButton createIncreaseButton(int orientation) 70 { 71 return new ArrowButton(orientation); 72 } 73 74 private static class ArrowButton extends JButton  75 { 76 private final int direction; 77 private final int h; 78 private final int w; 79 private final Color thumb; 80 private final Color shadow; 81 private final Color darkShadow; 82 private final Color highlight; 83 84 public ArrowButton(int direction) 85 { 86 this.direction = direction; 87 h = w = UIManager.getInt("ScrollBar.width"); 88 thumb = UIManager.getColor("ScrollBar.thumb"); 89 shadow = UIManager.getColor("ScrollBar.thumbShadow"); 90 darkShadow = UIManager.getColor("ScrollBar.thumbDarkShadow"); 91 highlight = UIManager.getColor("ScrollBar.thumbHighlight"); 92 setRequestFocusEnabled(false); 93 } 94 95 public void paint(Graphics g) 96 { 97 final Color origColor = g.getColor(); 98 final boolean isPressed = getModel().isPressed(); 99 if (isPressed) { 100 g.setColor(shadow); 101 g.drawRect(0, 0, w - 1, h - 1); 102 } else { 103 g.setColor(highlight); 104 g.drawLine(0, 0, 0, h - 1); 105 g.drawLine(1, 0, w - 2, 0); 106 g.setColor(shadow); 107 g.drawLine(1, h - 2, w - 2, h - 2); 108 g.drawLine(w - 2, 1, w - 2, h - 3); 109 g.setColor(darkShadow); 110 g.drawLine(0, h - 1, w - 1, h - 1); 111 g.drawLine(w - 1, 0, w - 1, h - 1); 112 } 113 g.setColor(thumb); 114 g.fillRect(1, 1, w - 3, h - 3); 115 if (isPressed) 116 g.translate(1, 1); 117 paintTriangle(g); 118 if (isPressed) 119 g.translate(-1, -1); 120 g.setColor(origColor); 121 } 122 123 public Dimension getPreferredSize() 124 { 125 return new Dimension (w, h); 126 } 127 128 public Dimension getMinimumSize() 129 { 130 return new Dimension (w, h); 131 } 132 133 public Dimension getMaximumSize() 134 { 135 return new Dimension (w, h); 136 } 137 138 public boolean isFocusTraversable() 139 { 140 return false; 141 } 142 143 private void paintTriangle(Graphics g) 144 { 145 final int size = 4; 146 int x = (w - size) / 2; 147 int y = (h - size) / 2; 148 if (direction == NORTH) 149 --y; 150 else if (direction == WEST) 151 --x; 152 g.translate(x, y); 153 g.setColor(isEnabled() ? darkShadow : shadow); 154 final int mid = (size / 2) - 1; 155 switch (direction) { 156 case NORTH: 157 for (int i = 0; i < size; i++) 158 g.drawLine(mid - i, i, mid + i, i); 159 break; 160 case SOUTH: 161 for (int i = size, j = 0; i-- > 0;) { 162 g.drawLine(mid - i, j, mid + i, j); 163 j++; 164 } 165 break; 166 case WEST: 167 for (int i = 0; i < size; i++) 168 g.drawLine(i, mid - i, i, mid + i); 169 break; 170 case EAST: 171 for (int i = size, j = 0; i-- > 0;) { 172 g.drawLine(j, mid - i, j, mid + i); 173 j++; 174 } 175 break; 176 } 177 g.translate(-x, -y); 178 } 179 } 180 } 181 | Popular Tags |