001: /*
002: * @(#)PaintPanel.java 2/22/2005
003: *
004: * Copyright 2002 - 2005 JIDE Software Inc. All rights reserved.
005: */
006: package com.jidesoft.swing;
007:
008: import javax.swing.*;
009: import java.awt.*;
010: import java.awt.image.BufferedImage;
011:
012: /**
013: * A panel which support Paint as background.
014: */
015: public class PaintPanel extends JPanel {
016: private Paint _backgroundPaint;
017:
018: public PaintPanel() {
019: }
020:
021: public PaintPanel(boolean isDoubleBuffered) {
022: super (isDoubleBuffered);
023: }
024:
025: public PaintPanel(LayoutManager layout) {
026: super (layout);
027: }
028:
029: public PaintPanel(LayoutManager layout, boolean isDoubleBuffered) {
030: super (layout, isDoubleBuffered);
031: }
032:
033: /**
034: * Gets the Paint that will be used to paint background.
035: *
036: * @return the background paint.
037: */
038: public Paint getBackgroundPaint() {
039: return _backgroundPaint;
040: }
041:
042: /**
043: * Sets the Paint that will be used to paint background.
044: *
045: * @param backgroundPaint
046: */
047: public void setBackgroundPaint(Paint backgroundPaint) {
048: _backgroundPaint = backgroundPaint;
049: }
050:
051: public static TexturePaint createTexturePaint(JPanel panel,
052: Image img, int x, int y, int w, int h) {
053: BufferedImage bi = new BufferedImage(w, h,
054: BufferedImage.TYPE_INT_ARGB);
055: Graphics2D tG2 = bi.createGraphics();
056: tG2.drawImage(img, x, y, Color.white, panel);
057: Rectangle r = new Rectangle(0, 0, w, h);
058: return new TexturePaint(bi, r);
059: }
060:
061: protected Color _startColor;
062: protected Color _endColor;
063: protected boolean _isVertical;
064:
065: /**
066: * This method allows you to use gradient background without using {@link #setBackgroundPaint(java.awt.Paint)}
067: * method. You can use GradientPaint to do the same thing. However if you use this method,
068: * it will use fast gradient paint defined in JideSwingUtilities to do the painting.
069: *
070: * @param startColor start color of the gradient
071: * @param endColor end color of the gradient
072: * @param isVertical vertical or not
073: */
074: public void setGradientPaint(Color startColor, Color endColor,
075: boolean isVertical) {
076: setStartColor(startColor);
077: setEndColor(endColor);
078: setVertical(isVertical);
079: }
080:
081: public Color getStartColor() {
082: return _startColor;
083: }
084:
085: public void setStartColor(Color startColor) {
086: _startColor = startColor;
087: }
088:
089: public Color getEndColor() {
090: return _endColor;
091: }
092:
093: public void setEndColor(Color endColor) {
094: _endColor = endColor;
095: }
096:
097: public boolean isVertical() {
098: return _isVertical;
099: }
100:
101: public void setVertical(boolean vertical) {
102: _isVertical = vertical;
103: }
104:
105: /**
106: * Paints the background.
107: *
108: * @param g
109: */
110: @Override
111: protected void paintComponent(Graphics g) {
112: super .paintComponent(g);
113: if (getStartColor() != null && getEndColor() != null) {
114: JideSwingUtilities.fillGradient((Graphics2D) g,
115: new Rectangle(0, 0, getWidth(), getHeight()),
116: getStartColor(), getEndColor(), isVertical());
117: } else if (isOpaque() && getBackgroundPaint() != null) {
118: Graphics2D g2d = (Graphics2D) g;
119: g2d.setPaint(getBackgroundPaint());
120: g2d.fillRect(0, 0, getWidth(), getHeight());
121: }
122: }
123: }
|