]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/gui/map/OverlayPanel.java
Version 13.4, May 2012
[GpsPrune.git] / tim / prune / gui / map / OverlayPanel.java
1 package tim.prune.gui.map;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.Graphics;
6
7 import javax.swing.JPanel;
8
9 /**
10  * Semi-transparent panel to go on top of the map
11  * to contain a set of controls
12  */
13 public class OverlayPanel extends JPanel
14 {
15         // Previous dimensions to see if calculations necessary
16         private int _prevWidth = -1, _prevHeight = -1;
17         // Previously calculated border limits
18         private int _minX, _minY, _width, _height;
19
20         /**
21          * Constructor
22          */
23         public OverlayPanel()
24         {
25                 setOpaque(false);
26         }
27
28         /**
29          * Paint the contents
30          */
31         public void paint(Graphics g)
32         {
33                 int panelWidth = getWidth();
34                 int panelHeight = getHeight();
35                 if (panelWidth != _prevWidth || panelHeight != _prevHeight)
36                 {
37                         calculateBorder();
38                         _prevWidth = panelWidth;
39                         _prevHeight = panelHeight;
40                 }
41                 // Draw white background
42                 final Color BG = new Color(255, 255, 255, 200);
43                 g.setColor(BG);
44                 g.fillRect(_minX, _minY, _width, _height);
45                 // Draw black border
46                 g.setColor(Color.BLACK);
47                 g.drawRect(_minX, _minY, _width, _height);
48                 // Paint everything else
49                 super.paint(g);
50         }
51
52         /**
53          * Calculate the boundaries to paint over
54          */
55         private void calculateBorder()
56         {
57                 final int PADDING = 2;
58                 // Calculate where the border should be drawn
59                 final Component firstComp = getComponent(0);
60                 final Component lastComp = getComponent(getComponentCount()-1);
61                 _minX = Math.max(firstComp.getX() - PADDING, 0);
62                 final int maxX = Math.min(lastComp.getX() + lastComp.getWidth() + PADDING, getWidth()-1);
63                 _width = maxX - _minX;
64                 _minY = Math.max(Math.min(firstComp.getY(), lastComp.getY()) - PADDING, 0);
65                 final int maxY = Math.max(firstComp.getY()+firstComp.getHeight(), lastComp.getY()+lastComp.getHeight()) + PADDING;
66                 _height = maxY - _minY;
67                 //System.out.println("x from " + minx + " to " + maxx + ", y from " + miny + " to " + maxy);
68         }
69 }