File: OverlayPanel.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (69 lines) | stat: -rw-r--r-- 1,860 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package tim.prune.gui.map;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.JPanel;

/**
 * Semi-transparent panel to go on top of the map
 * to contain a set of controls
 */
public class OverlayPanel extends JPanel
{
	// Previous dimensions to see if calculations necessary
	private int _prevWidth = -1, _prevHeight = -1;
	// Previously calculated border limits
	private int _minX, _minY, _width, _height;

	/**
	 * Constructor
	 */
	public OverlayPanel()
	{
		setOpaque(false);
	}

	/**
	 * Paint the contents
	 */
	public void paint(Graphics g)
	{
		int panelWidth = getWidth();
		int panelHeight = getHeight();
		if (panelWidth != _prevWidth || panelHeight != _prevHeight)
		{
			calculateBorder();
			_prevWidth = panelWidth;
			_prevHeight = panelHeight;
		}
		// Draw white background
		final Color BG = new Color(255, 255, 255, 200);
		g.setColor(BG);
		g.fillRect(_minX, _minY, _width, _height);
		// Draw black border
		g.setColor(Color.BLACK);
		g.drawRect(_minX, _minY, _width, _height);
		// Paint everything else
		super.paint(g);
	}

	/**
	 * Calculate the boundaries to paint over
	 */
	private void calculateBorder()
	{
		final int PADDING = 2;
		// Calculate where the border should be drawn
		final Component firstComp = getComponent(0);
		final Component lastComp = getComponent(getComponentCount()-1);
		_minX = Math.max(firstComp.getX() - PADDING, 0);
		final int maxX = Math.min(lastComp.getX() + lastComp.getWidth() + PADDING, getWidth()-1);
		_width = maxX - _minX;
		_minY = Math.max(Math.min(firstComp.getY(), lastComp.getY()) - PADDING, 0);
		final int maxY = Math.max(firstComp.getY()+firstComp.getHeight(), lastComp.getY()+lastComp.getHeight()) + PADDING;
		_height = maxY - _minY;
		//System.out.println("x from " + minx + " to " + maxx + ", y from " + miny + " to " + maxy);
	}
}