omscalingraster.java
来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 588 行 · 第 1/2 页
JAVA
588 行
hasLabel = false; } return true; } /** * No op for this class, can be use to manage image use for subclasses. * Called from within generate. * * @param proj current projection. * @return false if the rest of generate() should be skipped. */ protected boolean updateImageForProjection(Projection proj) { return true; } /** * Since the OMScalingRaster changes height and width depending on scale, we * need to rotate the image over that point and factor in the scaled height * and width of the image. Called from within OMRasterObject.render(). */ protected void rotate(Graphics2D g) { int rotOffsetX = point1.x + (point2.x - point1.x) / 2; int rotOffsetY = point1.y + (point2.y - point1.y) / 2; ((Graphics2D) g).rotate(rotationAngle, rotOffsetX, rotOffsetY); } /** * Take the current projection and the sourceImage, and make the image that * gets displayed fit the projection. If the source image isn't over the * map, then this OMGraphic is set to be invisible. If part of the image is * on the map, only that part is used. The OMRaster bitmap variable is set * with an image that is created from the source image, and the point1 * variable is set to the point where the image should be placed. For * instance, if the source image upper left corner is off the map to the * NorthWest, then the OMRaster bitmap is set to a image, clipped from the * source, that is entirely on the map. The OMRaster point1 is set to 0, 0, * since that is where the clipped image should be placed. * * @param thisProj the projection that the image should be scaled to. */ protected void scaleTo(Projection thisProj) { if (DEBUG) Debug.output("OMScalingRaster: scaleTo()"); if (sourceImage == null) { if (DEBUG) { Debug.output("OMScalingRaster.scaleTo() sourceImage is null"); } return; } // Get the projection window rectangle in pix Rectangle winRect = new Rectangle(thisProj.getWidth(), thisProj.getHeight()); // Get image projection rectangle Rectangle projRect = new Rectangle(); projRect.setLocation(point1); projRect.setSize(point2.x - point1.x, point2.y - point1.y); Rectangle sourceRect = new Rectangle(); sourceRect.width = sourceImage.getWidth(); sourceRect.height = sourceImage.getHeight(); // Now we have everything we need to sort out this new // projection. // boolean currentVisibility = isVisible(); // Assume we will not see it, in order to see if any part of // the // image will appear on map. If so, then reset visibility to // what's needed. // setVisible(false); clipRect = null; Rectangle iRect = winRect.intersection(projRect); if (!iRect.isEmpty()) { // Now we have the visible rectangle of the projected // image we need to figure out which pixels from the // source image get scaled to produce it. // Assume will need whole image, set the clipRect so it's // on the map, somewhere. clipRect = new Rectangle(); clipRect.setBounds(sourceRect); // If big enough to see if ((iRect.width >= 1) && (iRect.height >= 1)) { // If it didn't all fit if (!winRect.contains(projRect)) { // calc X scale factor float xScaleFactor = (float) sourceRect.width / (float) projRect.width; // and Y scale factor float yScaleFactor = (float) sourceRect.height / (float) projRect.height; int xOffset = (int) ((iRect.x - projRect.x)); // and // x // offset int yOffset = (int) ((iRect.y - projRect.y)); // and // y // offset clipRect.x = (int) (xOffset * xScaleFactor); // scale // the // x // position clipRect.y = (int) (yOffset * yScaleFactor); // scale // the // y // position // Do Math.ceil because the icon was getting // clipped a little if it started to move off the // screen a little. clipRect.width = (int) Math.ceil(iRect.width * xScaleFactor); // scale // the // width clipRect.height = (int) Math.ceil(iRect.height * yScaleFactor); // scale the height // Make sure the rounding doesn't exceed the // original icon // bounds if (clipRect.width + clipRect.x > sourceRect.width) clipRect.width = sourceRect.width - clipRect.x; if (clipRect.height + clipRect.y > sourceRect.height) clipRect.height = sourceRect.height - clipRect.y; } // check width and height of clipRect, in case it got // rounded down to zero. if (clipRect.width <= 0) { clipRect.width = 1; } if (clipRect.height <= 0) { clipRect.height = 1; } // Now we can grab the bit we want out of the source // and // scale it to fit the intersection. // Calc width adjustment double widthAdj = (double) iRect.width / (double) clipRect.width; // Calc height adjustment double heightAdj = (double) iRect.height / (double) clipRect.height; // Create the transform AffineTransform xform = new AffineTransform(); // Specify scaling xform.setToScale(widthAdj, heightAdj); // Create the transform op. AffineTransformOp xformOp = new AffineTransformOp(xform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); // Scale clip area -> newImage // extract sub-image BufferedImage newImage = xformOp.filter(sourceImage.getSubimage(clipRect.x, clipRect.y, clipRect.width, clipRect.height), null); bitmap = newImage; point1.setLocation(iRect.x, iRect.y); // setVisible(currentVisibility); } } else { bitmap = null; } } /** * Return the rectangle in screen co-ordinates that the scaled image has * been clipped to. This may return a null rectangle (i.e. the image is out * of the window). Otherwise the returned rectangle should always at least * partially lie within the bounds of the window. */ public Rectangle getClippedRectangle() { return clipRect; } /** * Change the upper latitude attribute. * * @param value latitude in decimal degrees. */ public void setULLat(float value) { setLat(value); } /** * Get the upper latitude. * * @return the latitude in decimal degrees. */ public float getULLat() { return getLat(); } /** * Change the western longitude attribute. * * @param value the longitude in decimal degrees. */ public void setULLon(float value) { setLon(value); } /** * Get the western longitude. * * @return longitude in decimal degrees. */ public float getULLon() { return getLon(); } /** * Change the southern latitude attribute. * * @param value latitude in decimal degrees. */ public void setLRLat(float value) { if (lat2 == value) return; lat2 = value; setNeedToReposition(true); } /** * Get the southern latitude. * * @return the latitude in decimal degrees. */ public float getLRLat() { return lat2; } /** * Change the eastern longitude attribute. * * @param value the longitude in decimal degrees. */ public void setLRLon(float value) { if (lon2 == value) return; lon2 = value; setNeedToReposition(true); } /** * Get the eastern longitude. * * @return longitude in decimal degrees. */ public float getLRLon() { return lon2; } /** * Set the rectangle, based on the location and size of the image after * scaling. */ public void setShape() { // generate shape that is a boundary of the generated image. // We'll make it a GeneralPath rectangle. int w = point2.x - point1.x; int h = point2.y - point1.y; setShape(createBoxShape(point1.x, point1.y, w, h)); } public boolean isOnMap(Projection proj) { Point p1 = proj.forward(lat, lon); Point p2 = proj.forward(lat2, lon2); int h = (int) Math.abs(p2.getY() - p1.getY()); int w = (int) Math.abs(p2.getX() - p1.getX()); Rectangle imageRect = new Rectangle((int) p1.getX(), (int) p1.getY(), w, h); proj.forward(proj.getUpperLeft(), p1); proj.forward(proj.getLowerRight(), p2); h = (int) Math.abs(p2.getY() - p1.getY()); w = (int) Math.abs(p2.getX() - p1.getX()); Rectangle mapRect = new Rectangle((int) p1.getX(), (int) p1.getY(), w, h); return mapRect.intersects(imageRect); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?