DrawablePanel.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * ========================================
  3. * JCommon : a free Java report library
  4. * ========================================
  5. *
  6. * Project Info: http://www.jfree.org/jcommon/
  7. * Project Lead: Thomas Morgner;
  8. *
  9. * (C) Copyright 2000-2006, by Object Refinery Limited and Contributors.
  10. *
  11. * This library is free software; you can redistribute it and/or modify it under the terms
  12. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  13. * either version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  16. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. * See the GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License along with this
  20. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. *
  23. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  24. * in the United States and other countries.]
  25. *
  26. * ------------
  27. * DrawablePanel.java
  28. * ------------
  29. * (C) Copyright 2002-2006, by Object Refinery Limited.
  30. *
  31. * Original Author: Thomas Morgner;
  32. * Contributor(s): -;
  33. *
  34. * $Id: DrawablePanel.java,v 1.4 2008/09/10 09:25:09 mungady Exp $
  35. *
  36. * Changes
  37. * -------
  38. *
  39. *
  40. */
  41. package org.jfree.ui;
  42. import java.awt.Dimension;
  43. import java.awt.Graphics;
  44. import java.awt.Graphics2D;
  45. import java.awt.geom.Rectangle2D;
  46. import javax.swing.JPanel;
  47. /**
  48. * A component, that accepts a drawable and which draws that drawable.
  49. *
  50. * @author Thomas Morgner
  51. */
  52. public class DrawablePanel extends JPanel
  53. {
  54. private Drawable drawable;
  55. /**
  56. * Creates a new instance.
  57. */
  58. public DrawablePanel()
  59. {
  60. setOpaque(false);
  61. }
  62. /**
  63. * Returns the drawable item.
  64. *
  65. * @return The drawable item.
  66. */
  67. public Drawable getDrawable()
  68. {
  69. return this.drawable;
  70. }
  71. /**
  72. * Sets the drawable item.
  73. *
  74. * @param drawable the drawable item.
  75. */
  76. public void setDrawable(final Drawable drawable)
  77. {
  78. this.drawable = drawable;
  79. revalidate();
  80. repaint();
  81. }
  82. /**
  83. * If the <code>preferredSize</code> has been set to a non-<code>null</code>
  84. * value just returns it. If the UI delegate's <code>getPreferredSize</code>
  85. * method returns a non <code>null</code> value then return that; otherwise
  86. * defer to the component's layout manager.
  87. *
  88. * @return the value of the <code>preferredSize</code> property
  89. * @see #setPreferredSize
  90. * @see javax.swing.plaf.ComponentUI
  91. */
  92. public Dimension getPreferredSize()
  93. {
  94. if (this.drawable instanceof ExtendedDrawable)
  95. {
  96. final ExtendedDrawable ed = (ExtendedDrawable) this.drawable;
  97. return ed.getPreferredSize();
  98. }
  99. return super.getPreferredSize();
  100. }
  101. /**
  102. * If the minimum size has been set to a non-<code>null</code> value just
  103. * returns it. If the UI delegate's <code>getMinimumSize</code> method
  104. * returns a non-<code>null</code> value then return that; otherwise defer to
  105. * the component's layout manager.
  106. *
  107. * @return the value of the <code>minimumSize</code> property
  108. * @see #setMinimumSize
  109. * @see javax.swing.plaf.ComponentUI
  110. */
  111. public Dimension getMinimumSize()
  112. {
  113. if (this.drawable instanceof ExtendedDrawable)
  114. {
  115. final ExtendedDrawable ed = (ExtendedDrawable) this.drawable;
  116. return ed.getPreferredSize();
  117. }
  118. return super.getMinimumSize();
  119. }
  120. /**
  121. * Returns true if this component is completely opaque.
  122. * <p>
  123. * An opaque component paints every pixel within its rectangular bounds. A
  124. * non-opaque component paints only a subset of its pixels or none at all,
  125. * allowing the pixels underneath it to "show through". Therefore, a
  126. * component that does not fully paint its pixels provides a degree of
  127. * transparency.</p>
  128. * <p>
  129. * Subclasses that guarantee to always completely paint their contents should
  130. * override this method and return true.</p>
  131. *
  132. * @return true if this component is completely opaque
  133. * @see #setOpaque
  134. */
  135. public boolean isOpaque()
  136. {
  137. if (this.drawable == null)
  138. {
  139. return false;
  140. }
  141. return super.isOpaque();
  142. }
  143. /**
  144. * Calls the UI delegate's paint method, if the UI delegate is
  145. * non-<code>null</code>. We pass the delegate a copy of the
  146. * <code>Graphics</code> object to protect the rest of the paint code from
  147. * irrevocable changes (for example, <code>Graphics.translate</code>).
  148. * <p>
  149. * If you override this in a subclass you should not make permanent changes to
  150. * the passed in <code>Graphics</code>. For example, you should not alter the
  151. * clip <code>Rectangle</code> or modify the transform. If you need to do
  152. * these operations you may find it easier to create a new
  153. * <code>Graphics</code> from the passed in <code>Graphics</code> and
  154. * manipulate it. Further, if you do not invoker super's implementation you
  155. * must honor the opaque property, that is if this component is opaque, you
  156. * must completely fill in the background in a non-opaque color. If you do not
  157. * honor the opaque property you will likely see visual artifacts.</p>
  158. * <p>
  159. * The passed in <code>Graphics</code> object might have a transform other
  160. * than the identify transform installed on it. In this case, you might get
  161. * unexpected results if you cumulatively apply another transform.</p>
  162. *
  163. * @param g the <code>Graphics</code> object to protect
  164. * @see #paint
  165. * @see javax.swing.plaf.ComponentUI
  166. */
  167. protected void paintComponent(Graphics g)
  168. {
  169. super.paintComponent(g);
  170. if (this.drawable == null)
  171. {
  172. return;
  173. }
  174. final Graphics2D g2 = (Graphics2D) g.create
  175. (0, 0, getWidth(), getHeight());
  176. this.drawable.draw(g2, new Rectangle2D.Double(0, 0, getWidth(), getHeight()));
  177. g2.dispose();
  178. }
  179. }