001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.gui.form;
031:
032: import java.awt.Dimension;
033: import java.awt.Graphics;
034: import java.awt.Rectangle;
035:
036: import javax.swing.JPanel;
037:
038: import com.jeta.forms.gui.effects.Painter;
039: import com.jeta.forms.logger.FormsLogger;
040:
041: /**
042: * This component renders a background effect for an entire form. When the user
043: * sets the fill property in the designer for a form, this is the object that
044: * will ultimately render the fill effect. The caller specifies the type of
045: * painter such as solid, gradient, image, or texture. The background effect is
046: * specified by a Painter object. See:
047: * {@link com.jeta.forms.gui.effects.Painter}
048: *
049: * @author Jeff Tassin
050: */
051: public class BackgroundPainter extends JPanel {
052: /**
053: * A painter for rendering a background effect. This can be null for no
054: * effect.
055: */
056: private Painter m_background_painter;
057:
058: /**
059: * A rectangle used for specifing the paint area. We use this object so we
060: * don't have to re-instantiate everytime a paint operation is required.
061: */
062: private Rectangle m_painter_rect;
063:
064: /**
065: * The preferred size for this component. Just set to 10x10 pixels so we
066: * don't have a 0x0 size.
067: */
068: private Dimension m_pref_size = new Dimension(10, 10);
069:
070: /**
071: * Creates a <code>BackgroundPainter</code> instance with a null painter.
072: */
073: public BackgroundPainter() {
074: setOpaque(false);
075: }
076:
077: /**
078: * Returns the preferred size for this component.
079: *
080: * @return the preferred size for this component.
081: */
082: public Dimension getPreferredSize() {
083: return m_pref_size;
084: }
085:
086: /**
087: * Override paintComponent so we can paint the fill effect
088: *
089: * @param g
090: * the graphics context.
091: */
092: public void paintComponent(Graphics g) {
093: try {
094: if (m_background_painter != null) {
095: if (m_painter_rect == null)
096: m_painter_rect = new Rectangle();
097:
098: m_painter_rect.setBounds(0, 0, getWidth(), getHeight());
099: m_background_painter.paint(this , g, m_painter_rect);
100: }
101: } catch (Exception e) {
102: FormsLogger.severe(e);
103: }
104: }
105:
106: /**
107: * Sets an object used to render an effect on this components background
108: *
109: * @param p
110: * the painter for this background
111: */
112: public void setBackgroundPainter(Painter p) {
113: m_background_painter = p;
114: m_painter_rect = null;
115: repaint();
116: }
117:
118: /**
119: * Override updateUI so we can force a repaint.
120: */
121: public void updateUI() {
122: super.updateUI();
123: if (m_background_painter != null) {
124: repaint();
125: }
126: }
127: }
|