1 42 43 package org.jfree.demo; 44 45 import java.awt.BorderLayout ; 46 import java.awt.event.ActionEvent ; 47 import java.awt.event.ActionListener ; 48 49 import javax.swing.JButton ; 50 import javax.swing.JComboBox ; 51 import javax.swing.JLabel ; 52 import javax.swing.JOptionPane ; 53 import javax.swing.JPanel ; 54 import javax.swing.JSlider ; 55 import javax.swing.JTabbedPane ; 56 import javax.swing.SwingConstants ; 57 import javax.swing.event.ChangeEvent ; 58 import javax.swing.event.ChangeListener ; 59 60 import org.jfree.ui.ApplicationFrame; 61 import org.jfree.ui.FontChooserPanel; 62 import org.jfree.ui.RefineryUtilities; 63 import org.jfree.ui.TextAnchor; 64 65 68 public class DrawStringDemo extends ApplicationFrame 69 implements ActionListener , ChangeListener { 70 71 72 private JComboBox combo1; 73 74 75 private JComboBox combo2; 76 77 78 private JComboBox combo3; 79 80 81 private JSlider slider; 82 83 84 private DrawStringPanel drawStringPanel1; 85 86 87 private DrawStringPanel drawStringPanel2; 88 89 94 public DrawStringDemo(final String title) { 95 super(title); 96 setContentPane(createContentPane()); 97 } 98 99 104 public void actionPerformed(final ActionEvent event) { 105 if (event.getActionCommand().equals("fontButton.clicked")) { 106 displayFontDialog(); 107 } 108 if (event.getActionCommand().equals("combo1.changed")) { 109 handleCombo1Change(); 110 } 111 if (event.getActionCommand().equals("combo2.changed")) { 112 handleCombo2Change(); 113 } 114 if (event.getActionCommand().equals("combo3.changed")) { 115 handleCombo3Change(); 116 } 117 } 118 119 public void stateChanged(ChangeEvent event) { 120 int r = this.slider.getValue(); 121 double angle = Math.PI * 2.0 * (r / 360.0); 122 this.drawStringPanel2.setAngle(angle); 123 this.drawStringPanel2.invalidate(); 124 this.drawStringPanel2.repaint(); 125 } 126 127 130 private void handleCombo1Change() { 131 final String text = this.combo1.getSelectedItem().toString(); 132 this.drawStringPanel1.setAnchor(convertStringToAnchor(text)); 133 this.drawStringPanel1.invalidate(); 134 this.drawStringPanel1.repaint(); 135 } 136 137 140 private void handleCombo2Change() { 141 final String text = this.combo2.getSelectedItem().toString(); 142 this.drawStringPanel2.setAnchor(convertStringToAnchor(text)); 143 this.drawStringPanel2.invalidate(); 144 this.drawStringPanel2.repaint(); 145 } 146 147 150 private void handleCombo3Change() { 151 final String text = this.combo3.getSelectedItem().toString(); 152 this.drawStringPanel2.setRotationAnchor(convertStringToAnchor(text)); 153 this.drawStringPanel2.invalidate(); 154 this.drawStringPanel2.repaint(); 155 } 156 157 162 private JPanel createContentPane() { 163 final JPanel content = new JPanel (new BorderLayout ()); 164 final JTabbedPane tabs = new JTabbedPane (); 165 tabs.add("Alignment", createTab1Content()); 166 tabs.add("Rotation", createTab2Content()); 167 content.add(tabs); 168 return content; 169 } 170 171 176 private JPanel createTab1Content() { 177 final JPanel content = new JPanel (new BorderLayout ()); 178 this.combo1 = new JComboBox (); 179 this.combo1.setActionCommand("combo1.changed"); 180 populateTextAnchorCombo(this.combo1); 181 this.combo1.addActionListener(this); 182 183 final JPanel controls = new JPanel (); 184 controls.add(this.combo1); 185 186 final JButton fontButton = new JButton ("Select Font..."); 187 fontButton.setActionCommand("fontButton.clicked"); 188 fontButton.addActionListener(this); 189 controls.add(fontButton); 190 content.add(controls, BorderLayout.NORTH); 191 this.drawStringPanel1 = new DrawStringPanel("0123456789", false); 192 content.add(this.drawStringPanel1); 193 return content; 194 } 195 196 201 private JPanel createTab2Content() { 202 JPanel content = new JPanel (new BorderLayout ()); 203 JPanel controls = new JPanel (); 204 controls.add(new JLabel ("Text anchor: ")); 205 this.combo2 = new JComboBox (); 206 populateTextAnchorCombo(this.combo2); 207 this.combo2.setActionCommand("combo2.changed"); 208 this.combo2.addActionListener(this); 209 controls.add(this.combo2); 210 controls.add(new JLabel ("Rotation anchor: ")); 211 this.combo3 = new JComboBox (); 212 populateTextAnchorCombo(this.combo3); 213 this.combo3.setActionCommand("combo3.changed"); 214 this.combo3.addActionListener(this); 215 controls.add(this.combo3); 216 this.slider = new JSlider (SwingConstants.VERTICAL, 0, 360, 0); 217 this.slider.setMajorTickSpacing(45); 218 this.slider.setMinorTickSpacing(5); 219 this.slider.setPaintLabels(true); 220 this.slider.setPaintTicks(true); 221 this.slider.setPaintTrack(true); 222 this.slider.addChangeListener(this); 223 content.add(controls, BorderLayout.NORTH); 224 content.add(this.slider, BorderLayout.WEST); 225 this.drawStringPanel2 = new DrawStringPanel("Rotated Text", true); 226 content.add(this.drawStringPanel2); 227 return content; 228 } 229 230 233 private void displayFontDialog() { 234 235 final FontChooserPanel panel = new FontChooserPanel(this.drawStringPanel1.getFont()); 236 final int result = JOptionPane.showConfirmDialog(this, panel, "Font Selection", 237 JOptionPane.OK_CANCEL_OPTION, 238 JOptionPane.PLAIN_MESSAGE); 239 240 if (result == JOptionPane.OK_OPTION) { 241 this.drawStringPanel1.setFont(panel.getSelectedFont()); 242 this.drawStringPanel2.setFont(panel.getSelectedFont()); 243 } 244 245 } 246 247 252 private void populateTextAnchorCombo(final JComboBox combo) { 253 combo.addItem("TextAnchor.TOP_LEFT"); 254 combo.addItem("TextAnchor.TOP_CENTER"); 255 combo.addItem("TextAnchor.TOP_RIGHT"); 256 combo.addItem("TextAnchor.HALF_ASCENT_LEFT"); 257 combo.addItem("TextAnchor.HALF_ASCENT_CENTER"); 258 combo.addItem("TextAnchor.HALF_ASCENT_RIGHT"); 259 combo.addItem("TextAnchor.CENTER_LEFT"); 260 combo.addItem("TextAnchor.CENTER"); 261 combo.addItem("TextAnchor.CENTER_RIGHT"); 262 combo.addItem("TextAnchor.BASELINE_LEFT"); 263 combo.addItem("TextAnchor.BASELINE_CENTER"); 264 combo.addItem("TextAnchor.BASELINE_RIGHT"); 265 combo.addItem("TextAnchor.BOTTOM_LEFT"); 266 combo.addItem("TextAnchor.BOTTOM_CENTER"); 267 combo.addItem("TextAnchor.BOTTOM_RIGHT"); 268 } 269 270 277 private TextAnchor convertStringToAnchor(final String text) { 278 279 if (text.equals("TextAnchor.TOP_LEFT")) { 280 return TextAnchor.TOP_LEFT; 281 } 282 else if (text.equals("TextAnchor.TOP_CENTER")) { 283 return TextAnchor.TOP_CENTER; 284 } 285 else if (text.equals("TextAnchor.TOP_RIGHT")) { 286 return TextAnchor.TOP_RIGHT; 287 } 288 else if (text.equals("TextAnchor.CENTER_LEFT")) { 289 return TextAnchor.CENTER_LEFT; 290 } 291 else if (text.equals("TextAnchor.CENTER")) { 292 return TextAnchor.CENTER; 293 } 294 else if (text.equals("TextAnchor.CENTER_RIGHT")) { 295 return TextAnchor.CENTER_RIGHT; 296 } 297 else if (text.equals("TextAnchor.HALF_ASCENT_LEFT")) { 298 return TextAnchor.HALF_ASCENT_LEFT; 299 } 300 else if (text.equals("TextAnchor.HALF_ASCENT_CENTER")) { 301 return TextAnchor.HALF_ASCENT_CENTER; 302 } 303 else if (text.equals("TextAnchor.HALF_ASCENT_RIGHT")) { 304 return TextAnchor.HALF_ASCENT_RIGHT; 305 } 306 else if (text.equals("TextAnchor.BASELINE_LEFT")) { 307 return TextAnchor.BASELINE_LEFT; 308 } 309 else if (text.equals("TextAnchor.BASELINE_CENTER")) { 310 return TextAnchor.BASELINE_CENTER; 311 } 312 else if (text.equals("TextAnchor.BASELINE_RIGHT")) { 313 return TextAnchor.BASELINE_RIGHT; 314 } 315 else if (text.equals("TextAnchor.BOTTOM_LEFT")) { 316 return TextAnchor.BOTTOM_LEFT; 317 } 318 else if (text.equals("TextAnchor.BOTTOM_CENTER")) { 319 return TextAnchor.BOTTOM_CENTER; 320 } 321 else if (text.equals("TextAnchor.BOTTOM_RIGHT")) { 322 return TextAnchor.BOTTOM_RIGHT; 323 } 324 else { 325 return null; 326 } 327 328 } 329 330 335 public static void main(final String [] args) { 336 337 final DrawStringDemo demo = new DrawStringDemo("DrawString Demo"); 338 demo.pack(); 339 RefineryUtilities.centerFrameOnScreen(demo); 340 demo.setVisible(true); 341 342 } 343 344 } 345 | Popular Tags |