KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > experimental > swt > SWTPaintCanvas


1 /* ===========================================================
2  * JFreeChart : a free chart 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/jfreechart/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  * SwtPaintCanvas.java
29  * -------------------
30  * (C) Copyright 2000-2005, by Object Refinery Limited.
31  *
32  * Original Author: Henry Proudhon (henry.proudhon AT insa-lyon.fr);
33  * Contributor(s):
34  *
35  * Changes
36  * -------
37  * 4 Aug 2006 : New class (HP);
38  */

39
40 package org.jfree.experimental.swt;
41
42 import org.eclipse.swt.SWT;
43 import org.eclipse.swt.events.PaintEvent;
44 import org.eclipse.swt.events.PaintListener;
45 import org.eclipse.swt.graphics.Color;
46 import org.eclipse.swt.widgets.Canvas;
47 import org.eclipse.swt.widgets.Composite;
48
49 /**
50  * A paint canvas.
51  */

52 public class SWTPaintCanvas extends Canvas
53 {
54     private Color myColor;
55     
56     /**
57      * Creates a new instance.
58      *
59      * @param parent the parent.
60      * @param style the style.
61      * @param color the color.
62      */

63     public SWTPaintCanvas(Composite parent, int style, Color color) {
64         this(parent, style);
65         this.setColor(color);
66     }
67     
68     /**
69      * Creates a new instance.
70      *
71      * @param parent the parent.
72      * @param style the style.
73      */

74     public SWTPaintCanvas(Composite parent, int style) {
75         super(parent, style);
76         addPaintListener(new PaintListener() {
77             public void paintControl(PaintEvent e) {
78                 e.gc.setForeground(e.gc.getDevice().getSystemColor(
79                         SWT.COLOR_BLACK));
80                 e.gc.setBackground(myColor);
81                 e.gc.fillRectangle(getClientArea());
82                 e.gc.drawRectangle(getClientArea().x, getClientArea().y,
83                         getClientArea().width - 1, getClientArea().height - 1);
84             }
85         });
86     }
87     
88     /**
89      * Sets the color.
90      *
91      * @param color the color.
92      */

93     public void setColor(Color color) {
94         if (this.myColor != null) {
95             myColor.dispose();
96         }
97         //this.myColor = new Color( getDisplay(), color.getRGB() );
98
this.myColor = color;
99     }
100
101     /**
102      * Returns the color.
103      *
104      * @return The color.
105      */

106     public Color getColor() {
107         return myColor;
108     }
109     
110     /**
111      * Overridden to do nothing.
112      *
113      * @param c the color.
114      */

115     public void setBackground(Color c) {
116         return;
117     }
118
119     /**
120      * Overridden to do nothing.
121      *
122      * @param c the color.
123      */

124     public void setForeground(Color c) {
125         return;
126     }
127     
128     /**
129      * Frees resources.
130      */

131     public void dispose() {
132         myColor.dispose();
133     }
134 }
135
Popular Tags