[8095] | 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 |
|
---|
[9078] | 9 | import org.openstreetmap.josm.gui.layer.ImageProcessor;
|
---|
| 10 |
|
---|
[8095] | 11 | /** class to describe how image overlay
|
---|
| 12 | * @since 8095
|
---|
| 13 | */
|
---|
[8723] | 14 | public class ImageOverlay implements ImageProcessor {
|
---|
[8722] | 15 | /** the image resource to use as overlay */
|
---|
[8095] | 16 | public ImageProvider image;
|
---|
| 17 | /** offset of the image from left border, values between 0 and 1 */
|
---|
[9078] | 18 | private final double offsetLeft;
|
---|
[8095] | 19 | /** offset of the image from top border, values between 0 and 1 */
|
---|
[9078] | 20 | private final double offsetRight;
|
---|
[8095] | 21 | /** offset of the image from right border, values between 0 and 1*/
|
---|
[9078] | 22 | private final double offsetTop;
|
---|
[8095] | 23 | /** offset of the image from bottom border, values between 0 and 1 */
|
---|
[9078] | 24 | private final double offsetBottom;
|
---|
[8290] | 25 |
|
---|
[8095] | 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 | *
|
---|
[8722] | 30 | * @param image image provider for the overlay icon
|
---|
[8095] | 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 | }
|
---|
[8290] | 44 |
|
---|
[8095] | 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 | *
|
---|
[8722] | 50 | * @param image image provider for the overlay icon
|
---|
[8095] | 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 | */
|
---|
[8723] | 69 | @Override
|
---|
| 70 | public BufferedImage process(BufferedImage ground) {
|
---|
[8095] | 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) {
|
---|
[8365] | 77 | width = (int) (w*(offsetRight-offsetLeft));
|
---|
[8095] | 78 | }
|
---|
| 79 | if (offsetTop > 0 && offsetBottom > 0) {
|
---|
[8722] | 80 | height = (int) (h*(offsetBottom-offsetTop));
|
---|
[8095] | 81 | }
|
---|
| 82 | ImageIcon overlay;
|
---|
[8510] | 83 | if (width != -1 || height != -1) {
|
---|
[8097] | 84 | image = new ImageProvider(image).resetMaxSize(new Dimension(width, height));
|
---|
[8095] | 85 | }
|
---|
[8097] | 86 | overlay = image.get();
|
---|
[8095] | 87 | int x, y;
|
---|
| 88 | if (width == -1 && offsetLeft < 0) {
|
---|
[8510] | 89 | x = (int) (w*offsetRight) - overlay.getIconWidth();
|
---|
[8095] | 90 | } else {
|
---|
[8510] | 91 | x = (int) (w*offsetLeft);
|
---|
[8095] | 92 | }
|
---|
| 93 | if (height == -1 && offsetTop < 0) {
|
---|
[8510] | 94 | y = (int) (h*offsetBottom) - overlay.getIconHeight();
|
---|
[8095] | 95 | } else {
|
---|
[8510] | 96 | y = (int) (h*offsetTop);
|
---|
[8095] | 97 | }
|
---|
| 98 | overlay.paintIcon(null, ground.getGraphics(), x, y);
|
---|
| 99 | return ground;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|