source: josm/trunk/src/org/openstreetmap/josm/tools/ImageOverlay.java@ 13206

Last change on this file since 13206 was 12782, checked in by Don-vip, 7 years ago

see #15229 - see #15182 - move ImageProcessor from gui.layer to tools

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