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

Last change on this file since 8126 was 8097, checked in by stoecker, 9 years ago

see #10684, see #10688 - fix image scaling for mappaint

File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.Dimension;
5import java.awt.Graphics;
6import java.awt.Image;
7import java.awt.image.BufferedImage;
8
9import javax.swing.ImageIcon;
10
11/** class to describe how image overlay
12 * @since 8095
13 */
14public class ImageOverlay {
15 /** the image ressource to use as overlay */
16 public ImageProvider image;
17 /** offset of the image from left border, values between 0 and 1 */
18 private double offsetLeft;
19 /** offset of the image from top border, values between 0 and 1 */
20 private double offsetRight;
21 /** offset of the image from right border, values between 0 and 1*/
22 private double offsetTop;
23 /** offset of the image from bottom border, values between 0 and 1 */
24 private 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 imager 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 imager 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 public BufferedImage apply(BufferedImage ground) {
70 /* get base dimensions for calculation */
71 int w = ground.getWidth();
72 int h = ground.getHeight();
73 int width = -1;
74 int height = -1;
75 if (offsetRight > 0 && offsetLeft > 0) {
76 width = new Double(w*(offsetRight-offsetLeft)).intValue();
77 }
78 if (offsetTop > 0 && offsetBottom > 0) {
79 width = new Double(h*(offsetBottom-offsetTop)).intValue();
80 }
81 ImageIcon overlay;
82 if(width != -1 || height != -1) {
83 image = new ImageProvider(image).resetMaxSize(new Dimension(width, height));
84 }
85 overlay = image.get();
86 int x, y;
87 if (width == -1 && offsetLeft < 0) {
88 x = new Double(w*offsetRight).intValue() - overlay.getIconWidth();
89 } else {
90 x = new Double(w*offsetLeft).intValue();
91 }
92 if (height == -1 && offsetTop < 0) {
93 y = new Double(h*offsetBottom).intValue() - overlay.getIconHeight();
94 } else {
95 y = new Double(h*offsetTop).intValue();
96 }
97 overlay.paintIcon(null, ground.getGraphics(), x, y);
98 return ground;
99 }
100}
Note: See TracBrowser for help on using the repository browser.