(no commit message)
[utils] / gps / src / main / java / org / wamblee / gpx / ZoomableBackgroundXYPlot.java
1 /*
2  * Copyright 2006 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16
17 package org.wamblee.gpx;
18
19 import java.awt.Graphics2D;
20 import java.awt.Image;
21 import java.awt.geom.Rectangle2D;
22
23 import org.jfree.chart.axis.ValueAxis;
24 import org.jfree.chart.plot.XYPlot;
25 import org.jfree.chart.renderer.xy.XYItemRenderer;
26 import org.jfree.data.xy.XYDataset;
27
28 /**
29  * Extension of XY plot that provides automatic zooming of the background 
30  * image. 
31  *
32  * @author Erik Brakkee
33  */
34 public class ZoomableBackgroundXYPlot extends XYPlot {
35     
36     /*
37      * Initial domain axis. 
38      */
39     private double _x1 = 1.0;
40     private double _x2 = -1.0; // _x2 < _x1 initially to make signify uninitialized values.
41     
42     /*
43      * Initial range axis.
44      */
45     private double _y1 = 1.0;
46     private double _y2 = -1.0; // _y2 < _y1 initially to make signify uninitialized values.
47     
48     public ZoomableBackgroundXYPlot(XYDataset aDataset, 
49             ValueAxis aDomainAxis, ValueAxis aRangeAxis, XYItemRenderer aRenderer) { 
50         super(aDataset, aDomainAxis, aRangeAxis, aRenderer);
51     }
52     
53     /* (non-Javadoc)
54      * @see org.jfree.chart.plot.Plot#drawBackgroundImage(java.awt.Graphics2D, java.awt.geom.Rectangle2D)
55      */
56     @Override
57     protected void drawBackgroundImage(Graphics2D g2, Rectangle2D area) {
58         //System.out.println("--------");
59         //System.out.println("Area: " + area);
60         //System.out.println("Graphics clip: " + g2.getClipBounds());
61         //System.out.println("Domain axis: " + getDomainAxis().getLowerBound() + " " + 
62         //        getDomainAxis().getUpperBound());
63         
64         // Get the current domain axis bounds
65         double y1 = getDomainAxis().getLowerBound();
66         double y2 = getDomainAxis().getUpperBound();
67         double x1 = getRangeAxis().getLowerBound();
68         double x2 = getRangeAxis().getUpperBound();
69         
70         if ( _x2 < _x1 ) { 
71             // initial domain axis bounds
72             _y1 = y1;
73             _y2 = y2;
74             _x1 = x1;
75             _x2 = x2;
76         }
77         
78         Image background = getBackgroundImage();
79         int width = background.getWidth(null);
80         int height = background.getHeight(null);
81         
82         // Determine the part of the image to be drawn on the screen based on the scaling
83         // of the domain axes. 
84         int imageX1 = (int)Math.round(1 + (x1 - _x1)*(width-1)/(_x2 - _x1));
85         int imageX2 = (int)Math.round(1 + (x2 - _x1)*(width-1)/(_x2 - _x1));
86         // Note: y-axis of image goes from bottom to top. 
87         int imageY2 = (int)Math.round(height + (y1 - _y1)*(1-height)/(_y2 - _y1));
88         int imageY1 = (int)Math.round(height + (y2 - _y1)*(1-height)/(_y2 - _y1));
89         
90         // Draw the correct part of the image on the screen. 
91         g2.drawImage(background, (int)area.getMinX(), (int)area.getMinY(), (int)area.getMaxX(), (int)area.getMaxY(), 
92                        imageX1, imageY1, imageX2, imageY2, null);
93      
94        // System.out.println("========");
95     }
96
97 }