1 19 20 package com.sshtools.ui.awt; 21 22 import java.awt.Canvas ; 23 import java.awt.Color ; 24 import java.awt.Component ; 25 import java.awt.Dimension ; 26 import java.awt.Graphics ; 27 import java.awt.SystemColor ; 28 29 36 37 public class Separator extends Canvas { 38 39 42 public final static int HORIZONTAL = 0; 43 44 47 public final static int VERTICAL = 1; 48 49 private int orientation; 51 private Color background; 52 private Dimension preferredSize; 53 54 67 public Separator(int orientation) { 68 super(); 69 setOrientation(orientation); 70 } 71 72 public void setBackground(Color background) { 73 super.setForeground(background); 74 this.background = background; 75 } 76 77 90 public void setOrientation(int orientation) { 91 this.orientation = orientation; 92 repaint(); 93 } 94 95 public void paint(Graphics g) { 96 Dimension d = getSize(); 97 98 Color fg = background; 100 101 Component co = getParent(); 103 while( fg == null && co != null) { 104 fg = co.getBackground(); 105 co = co.getParent(); 106 } 107 108 Color l1 = null; 110 Color l2 = null; 111 if(fg == null) { 112 l1 = SystemColor.controlHighlight; 113 l2 = SystemColor.controlShadow; 114 } 115 else { 116 float[] hsbvals = new float[3]; 117 Color.RGBtoHSB(fg.getRed(), fg.getGreen(), fg.getBlue(), hsbvals); 118 l1 = Color.getHSBColor(hsbvals[0], hsbvals[1], hsbvals[2] * 0.9f); 119 l2 = Color.getHSBColor(hsbvals[0], hsbvals[1], hsbvals[2] * 1.1f); 120 } 121 122 g.setColor(l1); 123 switch (orientation) { 124 case HORIZONTAL: 125 int c = ( d.height - 1 ) / 2; 126 g.drawLine(0, c, d.width, c); 127 g.setColor(l2); 128 g.drawLine(0, c - 1, d.width, c - 1); 129 break; 130 case VERTICAL: 131 int m = ( d.width - 1 ) / 2; 132 g.drawLine(m, 0, m, d.height); 133 g.setColor(l2); 134 g.drawLine(m - 1, 0, m - 1, d.height - 1); 135 break; 136 } 137 } 138 139 public Dimension getPreferredSize() { 140 if(preferredSize != null) { 141 return preferredSize; 142 } 143 else { 144 switch (orientation) { 145 case HORIZONTAL: 146 return new Dimension (0, 2); 147 default: 148 return new Dimension (2, 0); 149 } 150 } 151 } 152 153 public void setPreferredSize(Dimension preferredSize) { 154 this.preferredSize = preferredSize; 155 } 156 } | Popular Tags |