1 16 17 package org.mc4j.console.swing.graph; 18 19 import java.awt.AlphaComposite ; 20 import java.awt.Color ; 21 import java.awt.Composite ; 22 import java.awt.Font ; 23 import java.awt.Graphics ; 24 import java.awt.Graphics2D ; 25 import java.awt.GridBagConstraints ; 26 import java.awt.GridBagLayout ; 27 import java.awt.Rectangle ; 28 import java.awt.event.WindowAdapter ; 29 import java.awt.event.WindowEvent ; 30 import java.awt.font.TextLayout ; 31 import java.awt.geom.Rectangle2D ; 32 33 import javax.swing.JFrame ; 34 import javax.swing.JLabel ; 35 import javax.swing.JPanel ; 36 37 42 public class MeterCanvas extends JPanel { 43 44 45 public MeterCanvas() { 46 this.setOpaque(false); 47 } 48 49 private int min = 0; 50 private int max; 51 private int current; 52 53 private String label = "label"; 54 55 private static final int BUBBLE_HEIGHT = 5; 56 57 private static final Color DARK = Color.decode("0x336699"); 58 private static final Color LIGHT = Color.decode("0x99CCFF"); 59 60 private static final Font KEY_FONT = Font.decode("Arial-BOLD-24"); 61 private static final Font LABEL_FONT = Font.decode("Arial-BOLD-12"); 62 63 public void paintComponent(Graphics g) { 64 super.paintComponent(g); 65 Graphics2D gg = (Graphics2D ) g; 66 67 Rectangle rect = this.getBounds(); 68 69 gg.clearRect(0,0, getWidth(), getHeight()); 71 73 int h = 0; 74 75 double topLine = ((double)getHeight() / (this.max - this.min)) * this.current; 76 77 while ( h < getHeight()) { 79 if (h < topLine) { 80 gg.setColor(DARK); 81 gg.fillRoundRect(10, getHeight() - h, getWidth() - 20, BUBBLE_HEIGHT, 2, 2); 82 84 } else { 85 86 gg.setColor(LIGHT); 87 gg.drawRoundRect( 10, getHeight() - h, getWidth() - 20, BUBBLE_HEIGHT, 2, 2); 88 90 } 91 h += BUBBLE_HEIGHT + 2; 92 } 93 Composite basicComposite = gg.getComposite(); 94 gg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .5F)); 95 gg.setColor(Color.black); 96 97 gg.setFont(KEY_FONT); 98 String text = String.valueOf(this.current); 99 TextLayout layout = new TextLayout (text, KEY_FONT, gg.getFontRenderContext()); 100 Rectangle2D textBounds = layout.getBounds(); 101 gg.drawString(text, (int) ((getWidth()/2) - (textBounds.getWidth()/2)), getHeight()/2); 102 103 gg.setFont(LABEL_FONT); 104 text = this.label; 105 layout = new TextLayout (text, LABEL_FONT, gg.getFontRenderContext()); 106 textBounds = layout.getBounds(); 107 108 gg.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .3F)); 109 gg.setColor(Color.white); 110 gg.fillRoundRect(15,(int) (getHeight() - textBounds.getHeight() - 25),(int) getWidth() - 30,(int) textBounds.getHeight() + 10, 5, 5); 111 112 gg.setComposite(basicComposite); 113 114 gg.setColor(Color.black); 115 gg.drawString(text, (int) ((getWidth()/2) - (textBounds.getWidth()/2)), (int) (getHeight() - 20)); 116 117 } 118 119 123 public int getMin() { 124 return min; 125 } 126 127 131 public void setMin(int min) { 132 this.min = min; 133 } 134 135 139 public int getMax() { 140 return max; 141 } 142 143 147 public void setMax(int max) { 148 this.max = max; 149 } 150 151 155 public int getCurrent() { 156 return current; 157 } 158 159 163 public void setCurrent(int current) { 164 this.current = current; 165 this.repaint(); 166 } 168 169 170 171 public static void main(String [] args) throws Exception { 172 173 final MeterCanvas mc = new MeterCanvas(); 174 mc.setMax(200); 175 mc.setMin(0); 176 mc.setLabel("Free Memory"); 177 mc.setMinimumSize(null); mc.setPreferredSize(null); 180 JFrame frame = new JFrame ("Meter test"); 181 frame.addWindowListener(new WindowAdapter () { 182 public void windowClosed(WindowEvent we) { 183 mc.setLabel(null); 184 } 185 }); 186 187 188 frame.setSize(150,350); 189 frame.getContentPane().setLayout(new GridBagLayout ()); 190 GridBagConstraints gc = new GridBagConstraints (); 191 gc.gridx = 2; 192 gc.gridy = 1; 193 gc.fill = gc.BOTH; 194 gc.weightx = 0.5; 195 gc.weighty = 0.5; 196 frame.getContentPane().add(mc,gc); 197 198 JLabel testLabel = new JLabel ("Hello, World!"); 199 GridBagConstraints tc = new GridBagConstraints (); 200 tc.gridx = 1; 201 tc.gridy = 1; 202 tc.fill = gc.BOTH; 203 frame.getContentPane().add(testLabel, tc); 206 207 208 209 GridBagConstraints sc = new GridBagConstraints (); 210 sc.gridx = 3; 211 sc.gridy = 1; 212 sc.fill = sc.BOTH; 213 sc.weightx = 0.5; 214 sc.weighty = 0.5; 215 frame.getContentPane().add(new JLabel ("This is a longer test. testing."),sc); 216 217 218 frame.getContentPane().doLayout(); 219 frame.setVisible(true); 220 int i = 0; 221 while (mc.getLabel() != null) { 222 i++; 223 224 mc.setCurrent((int) (Math.sin(i*.06) * 100.0) + 100); 225 226 Thread.currentThread().sleep(50); 227 } 228 } 229 230 234 public java.lang.String getLabel() { 235 return label; 236 } 237 238 242 public void setLabel(java.lang.String label) { 243 this.label = label; 244 } 245 246 } 247 | Popular Tags |