001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import java.awt.Dimension; 005import java.awt.image.BufferedImage; 006 007import javax.swing.ImageIcon; 008 009/** class to describe how image overlay 010 * @since 8095 011 */ 012public class ImageOverlay implements ImageProcessor { 013 /** the image resource to use as overlay */ 014 public ImageProvider image; 015 /** offset of the image from left border, values between 0 and 1 */ 016 private final double offsetLeft; 017 /** offset of the image from top border, values between 0 and 1 */ 018 private final double offsetRight; 019 /** offset of the image from right border, values between 0 and 1*/ 020 private final double offsetTop; 021 /** offset of the image from bottom border, values between 0 and 1 */ 022 private final double offsetBottom; 023 024 /** 025 * Create an overlay info. All values are relative sizes between 0 and 1. Size of the image 026 * is the result of the difference between left/right and top/bottom. 027 * 028 * @param image image provider for the overlay icon 029 * @param offsetLeft offset of the image from left border, values between 0 and 1, -1 for auto-calculation 030 * @param offsetTop offset of the image from top border, values between 0 and 1, -1 for auto-calculation 031 * @param offsetRight offset of the image from right border, values between 0 and 1, -1 for auto-calculation 032 * @param offsetBottom offset of the image from bottom border, values between 0 and 1, -1 for auto-calculation 033 * @since 8095 034 */ 035 public ImageOverlay(ImageProvider image, double offsetLeft, double offsetTop, double offsetRight, double offsetBottom) { 036 this.image = image; 037 this.offsetLeft = offsetLeft; 038 this.offsetTop = offsetTop; 039 this.offsetRight = offsetRight; 040 this.offsetBottom = offsetBottom; 041 } 042 043 /** 044 * Create an overlay in southeast corner. All values are relative sizes between 0 and 1. 045 * Size of the image is the result of the difference between left/right and top/bottom. 046 * Right and bottom values are set to 1. 047 * 048 * @param image image provider for the overlay icon 049 * @see #ImageOverlay(ImageProvider, double, double, double, double) 050 * @since 8095 051 */ 052 public ImageOverlay(ImageProvider image) { 053 this.image = image; 054 this.offsetLeft = -1.0; 055 this.offsetTop = -1.0; 056 this.offsetRight = 1.0; 057 this.offsetBottom = 1.0; 058 } 059 060 /** 061 * Handle overlay. The image passed as argument is modified! 062 * 063 * @param ground the base image for the overlay (gets modified!) 064 * @return the modified image (same as argument) 065 * @since 8095 066 */ 067 @Override 068 public BufferedImage process(BufferedImage ground) { 069 /* get base dimensions for calculation */ 070 int w = ground.getWidth(); 071 int h = ground.getHeight(); 072 int width = -1; 073 int height = -1; 074 if (offsetRight > 0 && offsetLeft > 0) { 075 width = (int) (w*(offsetRight-offsetLeft)); 076 } 077 if (offsetTop > 0 && offsetBottom > 0) { 078 height = (int) (h*(offsetBottom-offsetTop)); 079 } 080 ImageIcon overlay; 081 image = new ImageProvider(image).setMaxSize(new Dimension(width, height)); 082 overlay = image.get(); 083 int x, y; 084 if (width == -1 && offsetLeft < 0) { 085 x = (int) (w*offsetRight) - overlay.getIconWidth(); 086 } else { 087 x = (int) (w*offsetLeft); 088 } 089 if (height == -1 && offsetTop < 0) { 090 y = (int) (h*offsetBottom) - overlay.getIconHeight(); 091 } else { 092 y = (int) (h*offsetTop); 093 } 094 overlay.paintIcon(null, ground.getGraphics(), x, y); 095 return ground; 096 } 097}