KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > progra > charting > render > AbstractRenderer


1 /*
2     JOpenChart Java Charting Library and Toolkit
3     Copyright (C) 2001 Sebastian Müller
4     http://jopenchart.sourceforge.net
5
6     This library is free software; you can redistribute it and/or
7     modify it under the terms of the GNU Lesser General Public
8     License as published by the Free Software Foundation; either
9     version 2.1 of the License, or (at your option) any later version.
10
11     This library is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     Lesser General Public License for more details.
15
16     You should have received a copy of the GNU Lesser General Public
17     License along with this library; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
20     AbstractRenderer.java
21     Created on 21. Juni 2001, 12:32
22 */

23
24 package de.progra.charting.render;
25
26 import java.awt.image.BufferedImage JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28 import java.awt.Image JavaDoc;
29 import java.awt.Graphics2D JavaDoc;
30 import java.awt.Dimension JavaDoc;
31 import java.awt.Color JavaDoc;
32
33 /**
34  * The AbstractRenderer provides default implementations for the set and
35  * get methods of every Renderer. Especially it provides a default mechanism
36  * for scaling Renderer instances whose actual bounds are smaller than their
37  * preferred size. As a consequence, every Renderer instance only needs
38  * to implement paintDefault() which has to render the object from coordinates
39  * 0,0 onwards using the preferred size.
40  * @author mueller
41  * @version 1.0
42  */

43 public abstract class AbstractRenderer implements Renderer {
44
45     Rectangle JavaDoc bounds = new Rectangle JavaDoc(0, 0,
46                                      Integer.MAX_VALUE,
47                                      Integer.MAX_VALUE);
48     
49     /** Creates new AbstractRenderer */
50     public AbstractRenderer() {
51     }
52     
53     /** Sets the bounds the layout manager has assigned to
54      * this renderer. Those, of course, have to be
55      * considered in the rendering process.
56      * @param bounds the new bounds for the renderer.
57      */

58     public void setBounds(Rectangle JavaDoc bounds) {
59         this.bounds = bounds;
60     }
61     
62     /** Gets the bounds for this renderer.
63      * @return the bounds of this renderer. If <code>setBounds</code> has not
64      * been called before, the bounds computed from
65      * <code>getPreferredSize</code> is returned.
66      */

67     public Rectangle JavaDoc getBounds() {
68         return bounds;
69     }
70     
71   
72     /** Renders the Object in the Graphics object. Creates a BufferedImage
73      * and the corresponding Graphics2D object to paint in. The Image is
74      * created using the preferred size. Afterwards <code>paintDefault</code>
75      * is called to perform a standard painting in the Graphics object.
76      * If the bounds and the preferred size don't match the image is
77      * scaled afterwards.
78      * @param g the Graphics2D object in which to render
79      */

80     public void render(Graphics2D JavaDoc g) {
81         Dimension JavaDoc d = getPreferredSize();
82         BufferedImage JavaDoc im = new BufferedImage JavaDoc((int)d.width,
83                                              (int)d.height,
84                                              BufferedImage.TYPE_INT_RGB);
85         Graphics2D JavaDoc g2 = im.createGraphics();
86         g2.setColor(Color.white);
87         g2.fillRect(0, 0, d.width, d.height);
88         g2.setColor(Color.black);
89         
90         paintDefault(g2);
91         
92         if(d.width > getBounds().getWidth() ||
93            d.height > getBounds().getHeight()) {
94            // Scale Image
95
Image JavaDoc scale = im.getScaledInstance((int)getBounds().getWidth(),
96                                               (int)getBounds().getHeight(),
97                                               Image.SCALE_SMOOTH);
98            
99            g.drawImage(scale,
100                        (int)getBounds().getX(),
101                        (int)getBounds().getY(),
102                        null);
103         } else
104            g.drawImage(im, (int)getBounds().getX(),
105                        (int)getBounds().getY(), null);
106     }
107     
108     /** This method is called by the paint method to do the actual painting.
109      * The painting is supposed to start at point (0,0) and the size is
110      * always the same as the preferred size. The paint method performs
111      * the possible scaling.
112      * @param g the Graphics2D object to paint in.
113      */

114     public abstract void paintDefault(Graphics2D JavaDoc g);
115 }
Popular Tags