1 /* 2 * BackgroundImage.java 8 juin 07 3 * 4 * Sweet Home 3D, Copyright (c) 2007 Emmanuel PUYBARET / eTeks <info@eteks.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 package com.eteks.sweethome3d.model; 21 22 import java.awt.geom.Point2D; 23 import java.io.Serializable; 24 25 /** 26 * The image displayed in background of the plan. 27 * @author Emmanuel Puybaret 28 */ 29 public class BackgroundImage implements Serializable { 30 private static final long serialVersionUID = 1L; 31 32 private final Content image; 33 private final float scaleDistance; 34 private final float scaleDistanceXStart; 35 private final float scaleDistanceYStart; 36 private final float scaleDistanceXEnd; 37 private final float scaleDistanceYEnd; 38 private final float xOrigin; 39 private final float yOrigin; 40 private final boolean invisible; 41 42 /** 43 * Creates a visible background image. 44 */ BackgroundImage(Content image, float scaleDistance, float scaleDistanceXStart, float scaleDistanceYStart, float scaleDistanceXEnd, float scaleDistanceYEnd, float xOrigin, float yOrigin)45 public BackgroundImage(Content image, float scaleDistance, 46 float scaleDistanceXStart, float scaleDistanceYStart, 47 float scaleDistanceXEnd, float scaleDistanceYEnd, 48 float xOrigin, float yOrigin) { 49 this(image, scaleDistance, scaleDistanceXStart, 50 scaleDistanceYStart, scaleDistanceXEnd, scaleDistanceYEnd, xOrigin, yOrigin, true); 51 } 52 53 /** 54 * Creates a background image. 55 * @since 1.8 56 */ BackgroundImage(Content image, float scaleDistance, float scaleDistanceXStart, float scaleDistanceYStart, float scaleDistanceXEnd, float scaleDistanceYEnd, float xOrigin, float yOrigin, boolean visible)57 public BackgroundImage(Content image, float scaleDistance, 58 float scaleDistanceXStart, float scaleDistanceYStart, 59 float scaleDistanceXEnd, float scaleDistanceYEnd, 60 float xOrigin, float yOrigin, boolean visible) { 61 this.image = image; 62 this.scaleDistance = scaleDistance; 63 this.scaleDistanceXStart = scaleDistanceXStart; 64 this.scaleDistanceYStart = scaleDistanceYStart; 65 this.scaleDistanceXEnd = scaleDistanceXEnd; 66 this.scaleDistanceYEnd = scaleDistanceYEnd; 67 this.xOrigin = xOrigin; 68 this.yOrigin = yOrigin; 69 // Use an invisible field instead of a visible field to get a default false value 70 // for images created before version 1.8 71 this.invisible = !visible; 72 } 73 74 /** 75 * Returns the image content of this background image. 76 */ getImage()77 public Content getImage() { 78 return this.image; 79 } 80 81 /** 82 * Returns the distance used to compute the scale of this image. 83 */ getScaleDistance()84 public float getScaleDistance() { 85 return this.scaleDistance; 86 } 87 88 /** 89 * Returns the abscissa of the start point used to compute 90 * the scale of this image. 91 */ getScaleDistanceXStart()92 public float getScaleDistanceXStart() { 93 return this.scaleDistanceXStart; 94 } 95 96 /** 97 * Returns the ordinate of the start point used to compute 98 * the scale of this image. 99 */ getScaleDistanceYStart()100 public float getScaleDistanceYStart() { 101 return this.scaleDistanceYStart; 102 } 103 104 /** 105 * Returns the abscissa of the end point used to compute 106 * the scale of this image. 107 */ getScaleDistanceXEnd()108 public float getScaleDistanceXEnd() { 109 return this.scaleDistanceXEnd; 110 } 111 112 /** 113 * Returns the ordinate of the end point used to compute 114 * the scale of this image. 115 */ getScaleDistanceYEnd()116 public float getScaleDistanceYEnd() { 117 return this.scaleDistanceYEnd; 118 } 119 120 /** 121 * Returns the scale of this image. 122 */ getScale()123 public float getScale() { 124 return getScale(this.scaleDistance, 125 this.scaleDistanceXStart, this.scaleDistanceYStart, 126 this.scaleDistanceXEnd, this.scaleDistanceYEnd); 127 } 128 129 /** 130 * Returns the scale equal to <code>scaleDistance</code> divided 131 * by the distance between the points 132 * (<code>scaleDistanceXStart</code>, <code>scaleDistanceYStart</code>) 133 * and (<code>scaleDistanceXEnd</code>, <code>scaleDistanceYEnd</code>). 134 */ getScale(float scaleDistance, float scaleDistanceXStart, float scaleDistanceYStart, float scaleDistanceXEnd, float scaleDistanceYEnd)135 public static float getScale(float scaleDistance, 136 float scaleDistanceXStart, float scaleDistanceYStart, 137 float scaleDistanceXEnd, float scaleDistanceYEnd) { 138 return (float)(scaleDistance 139 / Point2D.distance(scaleDistanceXStart, scaleDistanceYStart, 140 scaleDistanceXEnd, scaleDistanceYEnd)); 141 } 142 143 /** 144 * Returns the origin abscissa of this image. 145 */ getXOrigin()146 public float getXOrigin() { 147 return this.xOrigin; 148 } 149 150 /** 151 * Returns the origin ordinate of this image. 152 */ getYOrigin()153 public float getYOrigin() { 154 return this.yOrigin; 155 } 156 157 /** 158 * Returns <code>true</code> if this image is visible in plan. 159 * @since 1.8 160 */ isVisible()161 public boolean isVisible() { 162 return !this.invisible; 163 } 164 } 165