1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.image.BufferedImage;
|
---|
6 |
|
---|
7 | import javax.swing.ImageIcon;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.gui.layer.ImageProcessor;
|
---|
10 |
|
---|
11 | /** class to describe how image overlay
|
---|
12 | * @since 8095
|
---|
13 | */
|
---|
14 | public class ImageOverlay implements ImageProcessor {
|
---|
15 | /** the image resource to use as overlay */
|
---|
16 | public ImageProvider image;
|
---|
17 | /** offset of the image from left border, values between 0 and 1 */
|
---|
18 | private final double offsetLeft;
|
---|
19 | /** offset of the image from top border, values between 0 and 1 */
|
---|
20 | private final double offsetRight;
|
---|
21 | /** offset of the image from right border, values between 0 and 1*/
|
---|
22 | private final double offsetTop;
|
---|
23 | /** offset of the image from bottom border, values between 0 and 1 */
|
---|
24 | private final double offsetBottom;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Create an overlay info. All values are relative sizes between 0 and 1. Size of the image
|
---|
28 | * is the result of the difference between left/right and top/bottom.
|
---|
29 | *
|
---|
30 | * @param image image provider for the overlay icon
|
---|
31 | * @param offsetLeft offset of the image from left border, values between 0 and 1, -1 for auto-calculation
|
---|
32 | * @param offsetTop offset of the image from top border, values between 0 and 1, -1 for auto-calculation
|
---|
33 | * @param offsetRight offset of the image from right border, values between 0 and 1, -1 for auto-calculation
|
---|
34 | * @param offsetBottom offset of the image from bottom border, values between 0 and 1, -1 for auto-calculation
|
---|
35 | * @since 8095
|
---|
36 | */
|
---|
37 | public ImageOverlay(ImageProvider image, double offsetLeft, double offsetTop, double offsetRight, double offsetBottom) {
|
---|
38 | this.image = image;
|
---|
39 | this.offsetLeft = offsetLeft;
|
---|
40 | this.offsetTop = offsetTop;
|
---|
41 | this.offsetRight = offsetRight;
|
---|
42 | this.offsetBottom = offsetBottom;
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Create an overlay in southeast corner. All values are relative sizes between 0 and 1.
|
---|
47 | * Size of the image is the result of the difference between left/right and top/bottom.
|
---|
48 | * Right and bottom values are set to 1.
|
---|
49 | *
|
---|
50 | * @param image image provider for the overlay icon
|
---|
51 | * @see #ImageOverlay(ImageProvider, double, double, double, double)
|
---|
52 | * @since 8095
|
---|
53 | */
|
---|
54 | public ImageOverlay(ImageProvider image) {
|
---|
55 | this.image = image;
|
---|
56 | this.offsetLeft = -1.0;
|
---|
57 | this.offsetTop = -1.0;
|
---|
58 | this.offsetRight = 1.0;
|
---|
59 | this.offsetBottom = 1.0;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Handle overlay. The image passed as argument is modified!
|
---|
64 | *
|
---|
65 | * @param ground the base image for the overlay (gets modified!)
|
---|
66 | * @return the modified image (same as argument)
|
---|
67 | * @since 8095
|
---|
68 | */
|
---|
69 | @Override
|
---|
70 | public BufferedImage process(BufferedImage ground) {
|
---|
71 | /* get base dimensions for calculation */
|
---|
72 | int w = ground.getWidth();
|
---|
73 | int h = ground.getHeight();
|
---|
74 | int width = -1;
|
---|
75 | int height = -1;
|
---|
76 | if (offsetRight > 0 && offsetLeft > 0) {
|
---|
77 | width = (int) (w*(offsetRight-offsetLeft));
|
---|
78 | }
|
---|
79 | if (offsetTop > 0 && offsetBottom > 0) {
|
---|
80 | height = (int) (h*(offsetBottom-offsetTop));
|
---|
81 | }
|
---|
82 | ImageIcon overlay;
|
---|
83 | if (width != -1 || height != -1) {
|
---|
84 | image = new ImageProvider(image).resetMaxSize(new Dimension(width, height));
|
---|
85 | }
|
---|
86 | overlay = image.get();
|
---|
87 | int x, y;
|
---|
88 | if (width == -1 && offsetLeft < 0) {
|
---|
89 | x = (int) (w*offsetRight) - overlay.getIconWidth();
|
---|
90 | } else {
|
---|
91 | x = (int) (w*offsetLeft);
|
---|
92 | }
|
---|
93 | if (height == -1 && offsetTop < 0) {
|
---|
94 | y = (int) (h*offsetBottom) - overlay.getIconHeight();
|
---|
95 | } else {
|
---|
96 | y = (int) (h*offsetTop);
|
---|
97 | }
|
---|
98 | overlay.paintIcon(null, ground.getGraphics(), x, y);
|
---|
99 | return ground;
|
---|
100 | }
|
---|
101 | }
|
---|