DrawStringDemo.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * -------------------
  28. * DrawStringDemo.java
  29. * -------------------
  30. * (C) Copyright 2003-2005, by Object Refinery Limited.
  31. *
  32. * Original Author: David Gilbert (for Object Refinery Limited);
  33. * Contributor(s): -;
  34. *
  35. * $Id: DrawStringDemo.java,v 1.6 2008/09/10 09:14:22 mungady Exp $
  36. *
  37. * Changes
  38. * -------
  39. * 10-Jun-2003 : Version 1;
  40. *
  41. */
  42. package org.jfree.demo;
  43. import java.awt.BorderLayout;
  44. import java.awt.event.ActionEvent;
  45. import java.awt.event.ActionListener;
  46. import javax.swing.JButton;
  47. import javax.swing.JComboBox;
  48. import javax.swing.JLabel;
  49. import javax.swing.JOptionPane;
  50. import javax.swing.JPanel;
  51. import javax.swing.JSlider;
  52. import javax.swing.JTabbedPane;
  53. import javax.swing.SwingConstants;
  54. import javax.swing.event.ChangeEvent;
  55. import javax.swing.event.ChangeListener;
  56. import org.jfree.ui.ApplicationFrame;
  57. import org.jfree.ui.FontChooserPanel;
  58. import org.jfree.ui.RefineryUtilities;
  59. import org.jfree.ui.TextAnchor;
  60. /**
  61. * A demo of some of the string drawing methods in the JCommon class library.
  62. */
  63. public class DrawStringDemo extends ApplicationFrame
  64. implements ActionListener, ChangeListener {
  65. /** The alignment anchor for the first panel. */
  66. private JComboBox combo1;
  67. /** The alignment anchor for the second panel. */
  68. private JComboBox combo2;
  69. /** The rotation anchor for the second panel. */
  70. private JComboBox combo3;
  71. /** A slider for the second panel - controls the angle of rotation. */
  72. private JSlider slider;
  73. /** String panel 1. */
  74. private DrawStringPanel drawStringPanel1;
  75. /** String panel 2. */
  76. private DrawStringPanel drawStringPanel2;
  77. /**
  78. * Creates a new demo instance.
  79. *
  80. * @param title the frame title.
  81. */
  82. public DrawStringDemo(final String title) {
  83. super(title);
  84. setContentPane(createContentPane());
  85. }
  86. /**
  87. * Receives action events.
  88. *
  89. * @param event the event.
  90. */
  91. public void actionPerformed(final ActionEvent event) {
  92. if (event.getActionCommand().equals("fontButton.clicked")) {
  93. displayFontDialog();
  94. }
  95. if (event.getActionCommand().equals("combo1.changed")) {
  96. handleCombo1Change();
  97. }
  98. if (event.getActionCommand().equals("combo2.changed")) {
  99. handleCombo2Change();
  100. }
  101. if (event.getActionCommand().equals("combo3.changed")) {
  102. handleCombo3Change();
  103. }
  104. }
  105. /**
  106. * Receives change event notification from the slider.
  107. *
  108. * @param event the event.
  109. */
  110. public void stateChanged(ChangeEvent event) {
  111. int r = this.slider.getValue();
  112. double angle = Math.PI * 2.0 * (r / 360.0);
  113. this.drawStringPanel2.setAngle(angle);
  114. this.drawStringPanel2.invalidate();
  115. this.drawStringPanel2.repaint();
  116. }
  117. /**
  118. * Updates the display when combo 1 is updated.
  119. */
  120. private void handleCombo1Change() {
  121. final String text = this.combo1.getSelectedItem().toString();
  122. this.drawStringPanel1.setAnchor(convertStringToAnchor(text));
  123. this.drawStringPanel1.invalidate();
  124. this.drawStringPanel1.repaint();
  125. }
  126. /**
  127. * Updates the display when combo 2 is updated.
  128. */
  129. private void handleCombo2Change() {
  130. final String text = this.combo2.getSelectedItem().toString();
  131. this.drawStringPanel2.setAnchor(convertStringToAnchor(text));
  132. this.drawStringPanel2.invalidate();
  133. this.drawStringPanel2.repaint();
  134. }
  135. /**
  136. * Updates the display when combo 3 is updated.
  137. */
  138. private void handleCombo3Change() {
  139. final String text = this.combo3.getSelectedItem().toString();
  140. this.drawStringPanel2.setRotationAnchor(convertStringToAnchor(text));
  141. this.drawStringPanel2.invalidate();
  142. this.drawStringPanel2.repaint();
  143. }
  144. /**
  145. * Creates the content pane for the demo frame.
  146. *
  147. * @return The content pane.
  148. */
  149. private JPanel createContentPane() {
  150. final JPanel content = new JPanel(new BorderLayout());
  151. final JTabbedPane tabs = new JTabbedPane();
  152. tabs.add("Alignment", createTab1Content());
  153. tabs.add("Rotation", createTab2Content());
  154. content.add(tabs);
  155. return content;
  156. }
  157. /**
  158. * Creates the content for tab 1.
  159. *
  160. * @return The content panel.
  161. */
  162. private JPanel createTab1Content() {
  163. final JPanel content = new JPanel(new BorderLayout());
  164. this.combo1 = new JComboBox();
  165. this.combo1.setActionCommand("combo1.changed");
  166. populateTextAnchorCombo(this.combo1);
  167. this.combo1.addActionListener(this);
  168. final JPanel controls = new JPanel();
  169. controls.add(this.combo1);
  170. final JButton fontButton = new JButton("Select Font...");
  171. fontButton.setActionCommand("fontButton.clicked");
  172. fontButton.addActionListener(this);
  173. controls.add(fontButton);
  174. content.add(controls, BorderLayout.NORTH);
  175. this.drawStringPanel1 = new DrawStringPanel("0123456789", false);
  176. content.add(this.drawStringPanel1);
  177. return content;
  178. }
  179. /**
  180. * Creates the content for tab 2.
  181. *
  182. * @return The content panel.
  183. */
  184. private JPanel createTab2Content() {
  185. JPanel content = new JPanel(new BorderLayout());
  186. JPanel controls = new JPanel();
  187. controls.add(new JLabel("Text anchor: "));
  188. this.combo2 = new JComboBox();
  189. populateTextAnchorCombo(this.combo2);
  190. this.combo2.setActionCommand("combo2.changed");
  191. this.combo2.addActionListener(this);
  192. controls.add(this.combo2);
  193. controls.add(new JLabel("Rotation anchor: "));
  194. this.combo3 = new JComboBox();
  195. populateTextAnchorCombo(this.combo3);
  196. this.combo3.setActionCommand("combo3.changed");
  197. this.combo3.addActionListener(this);
  198. controls.add(this.combo3);
  199. this.slider = new JSlider(SwingConstants.VERTICAL, 0, 360, 0);
  200. this.slider.setMajorTickSpacing(45);
  201. this.slider.setMinorTickSpacing(5);
  202. this.slider.setPaintLabels(true);
  203. this.slider.setPaintTicks(true);
  204. this.slider.setPaintTrack(true);
  205. this.slider.addChangeListener(this);
  206. content.add(controls, BorderLayout.NORTH);
  207. content.add(this.slider, BorderLayout.WEST);
  208. this.drawStringPanel2 = new DrawStringPanel("Rotated Text", true);
  209. content.add(this.drawStringPanel2);
  210. return content;
  211. }
  212. /**
  213. * Displays a primitive font chooser dialog to allow the user to change the font.
  214. */
  215. private void displayFontDialog() {
  216. final FontChooserPanel panel = new FontChooserPanel(this.drawStringPanel1.getFont());
  217. final int result = JOptionPane.showConfirmDialog(this, panel, "Font Selection",
  218. JOptionPane.OK_CANCEL_OPTION,
  219. JOptionPane.PLAIN_MESSAGE);
  220. if (result == JOptionPane.OK_OPTION) {
  221. this.drawStringPanel1.setFont(panel.getSelectedFont());
  222. this.drawStringPanel2.setFont(panel.getSelectedFont());
  223. }
  224. }
  225. /**
  226. * Populates a combo box with the available {@link TextAnchor} options.
  227. *
  228. * @param combo the combo box.
  229. */
  230. private void populateTextAnchorCombo(final JComboBox combo) {
  231. combo.addItem("TextAnchor.TOP_LEFT");
  232. combo.addItem("TextAnchor.TOP_CENTER");
  233. combo.addItem("TextAnchor.TOP_RIGHT");
  234. combo.addItem("TextAnchor.HALF_ASCENT_LEFT");
  235. combo.addItem("TextAnchor.HALF_ASCENT_CENTER");
  236. combo.addItem("TextAnchor.HALF_ASCENT_RIGHT");
  237. combo.addItem("TextAnchor.CENTER_LEFT");
  238. combo.addItem("TextAnchor.CENTER");
  239. combo.addItem("TextAnchor.CENTER_RIGHT");
  240. combo.addItem("TextAnchor.BASELINE_LEFT");
  241. combo.addItem("TextAnchor.BASELINE_CENTER");
  242. combo.addItem("TextAnchor.BASELINE_RIGHT");
  243. combo.addItem("TextAnchor.BOTTOM_LEFT");
  244. combo.addItem("TextAnchor.BOTTOM_CENTER");
  245. combo.addItem("TextAnchor.BOTTOM_RIGHT");
  246. }
  247. /**
  248. * Converts a string to a corresponding {@link TextAnchor} instance.
  249. *
  250. * @param text the text.
  251. *
  252. * @return The anchor.
  253. */
  254. private TextAnchor convertStringToAnchor(final String text) {
  255. if (text.equals("TextAnchor.TOP_LEFT")) {
  256. return TextAnchor.TOP_LEFT;
  257. }
  258. else if (text.equals("TextAnchor.TOP_CENTER")) {
  259. return TextAnchor.TOP_CENTER;
  260. }
  261. else if (text.equals("TextAnchor.TOP_RIGHT")) {
  262. return TextAnchor.TOP_RIGHT;
  263. }
  264. else if (text.equals("TextAnchor.CENTER_LEFT")) {
  265. return TextAnchor.CENTER_LEFT;
  266. }
  267. else if (text.equals("TextAnchor.CENTER")) {
  268. return TextAnchor.CENTER;
  269. }
  270. else if (text.equals("TextAnchor.CENTER_RIGHT")) {
  271. return TextAnchor.CENTER_RIGHT;
  272. }
  273. else if (text.equals("TextAnchor.HALF_ASCENT_LEFT")) {
  274. return TextAnchor.HALF_ASCENT_LEFT;
  275. }
  276. else if (text.equals("TextAnchor.HALF_ASCENT_CENTER")) {
  277. return TextAnchor.HALF_ASCENT_CENTER;
  278. }
  279. else if (text.equals("TextAnchor.HALF_ASCENT_RIGHT")) {
  280. return TextAnchor.HALF_ASCENT_RIGHT;
  281. }
  282. else if (text.equals("TextAnchor.BASELINE_LEFT")) {
  283. return TextAnchor.BASELINE_LEFT;
  284. }
  285. else if (text.equals("TextAnchor.BASELINE_CENTER")) {
  286. return TextAnchor.BASELINE_CENTER;
  287. }
  288. else if (text.equals("TextAnchor.BASELINE_RIGHT")) {
  289. return TextAnchor.BASELINE_RIGHT;
  290. }
  291. else if (text.equals("TextAnchor.BOTTOM_LEFT")) {
  292. return TextAnchor.BOTTOM_LEFT;
  293. }
  294. else if (text.equals("TextAnchor.BOTTOM_CENTER")) {
  295. return TextAnchor.BOTTOM_CENTER;
  296. }
  297. else if (text.equals("TextAnchor.BOTTOM_RIGHT")) {
  298. return TextAnchor.BOTTOM_RIGHT;
  299. }
  300. else {
  301. return null;
  302. }
  303. }
  304. /**
  305. * The starting point for the demo.
  306. *
  307. * @param args ignored.
  308. */
  309. public static void main(final String[] args) {
  310. final DrawStringDemo demo = new DrawStringDemo("DrawString Demo");
  311. demo.pack();
  312. RefineryUtilities.centerFrameOnScreen(demo);
  313. demo.setVisible(true);
  314. }
  315. }