/** * Set the current transform. Calls writeSetTransform(Transform). * * @param transform to be set */ public void setTransform(AffineTransform transform) { // Fix for FREEHEP-569 oldTransform.setTransform(currentTransform); currentTransform.setTransform(transform); try { writeSetTransform(transform); } catch (IOException e) { handleException(e); } }
/** * Applique une translation à la perspective selon le mouvement de la souris par rapport au centre * de l'image. * * @param coordSouris Les coordonnées de la souris. */ public void translate(double[] coordSouris) { double largeur; double hauteur; // Garde en mémoire la transformation courant pileTransform.push(new AffineTransform(transformation)); // Calcul des dimensions après le zoom largeur = panneauSource.getImagePlus().getWidth() * panneauSource.getImagePlus().getPerspective().getTransformation().getScaleX(); hauteur = panneauSource.getImagePlus().getHeight() * panneauSource.getImagePlus().getPerspective().getTransformation().getScaleY(); // Déplacement de la perspective selon la position de la souris. transformation.setTransform( transformation.getScaleX(), 0, 0, transformation.getScaleY(), coordSouris[0] - (largeur / 2), coordSouris[1] - (hauteur / 2)); setChanged(); notifyObservers(); }
public void render(Graphics g) { Graphics2D g2 = (Graphics2D) g; AffineTransform xform = new AffineTransform(); xform.setTransform(identity); xform.translate(getVector().getX(), getVector().getY()); xform.rotate(getVector().getBearing() + Math.PI / 2, 3, 3); g2.drawImage(sprite, xform, null); }
@Override public void draw(Graphics g) { super.draw(g); AffineTransform at = new AffineTransform(); at.setTransform(scaler.getScale(), 0, 0, scaler.getScale(), 0, 0); anim.setAffineTransform(at); anim.draw(g); }
private void transformGraphics(Graphics2D g, Vec2 center) { Vec2 e = viewportTransform.getExtents(); Vec2 vc = viewportTransform.getCenter(); Mat22 vt = viewportTransform.getMat22Representation(); int flip = yFlip ? -1 : 1; tr.setTransform(vt.ex.x, flip * vt.ex.y, vt.ey.x, flip * vt.ey.y, e.x, e.y); tr.translate(-vc.x, -vc.y); tr.translate(center.x, center.y); g.transform(tr); }
/** * Tries to get tiles of a lower or higher zoom level (one or two level difference) from cache and * use it as a placeholder until the tile has been loaded. */ public void loadPlaceholderFromCache(TileCache cache) { BufferedImage tmpImage = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D) tmpImage.getGraphics(); // g.drawImage(image, 0, 0, null); for (int zoomDiff = 1; zoomDiff < 5; zoomDiff++) { // first we check if there are already the 2^x tiles // of a higher detail level int zoom_high = zoom + zoomDiff; if (zoomDiff < 3 && zoom_high <= JMapViewer.MAX_ZOOM) { int factor = 1 << zoomDiff; int xtile_high = xtile << zoomDiff; int ytile_high = ytile << zoomDiff; double scale = 1.0 / factor; g.setTransform(AffineTransform.getScaleInstance(scale, scale)); int paintedTileCount = 0; for (int x = 0; x < factor; x++) { for (int y = 0; y < factor; y++) { Tile tile = cache.getTile(source, xtile_high + x, ytile_high + y, zoom_high); if (tile != null && tile.isLoaded()) { paintedTileCount++; tile.paint(g, x * SIZE, y * SIZE); } } } if (paintedTileCount == factor * factor) { image = tmpImage; return; } } int zoom_low = zoom - zoomDiff; if (zoom_low >= JMapViewer.MIN_ZOOM) { int xtile_low = xtile >> zoomDiff; int ytile_low = ytile >> zoomDiff; int factor = (1 << zoomDiff); double scale = factor; AffineTransform at = new AffineTransform(); int translate_x = (xtile % factor) * SIZE; int translate_y = (ytile % factor) * SIZE; at.setTransform(scale, 0, 0, scale, -translate_x, -translate_y); g.setTransform(at); Tile tile = cache.getTile(source, xtile_low, ytile_low, zoom_low); if (tile != null && tile.isLoaded()) { tile.paint(g, 0, 0); image = tmpImage; return; } } } }
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; AffineTransform trans = new AffineTransform(); int width = getSize().width; int height = getSize().height; g2d.setColor(Color.BLACK); g2d.fillRect(0, 0, width, height); trans.setTransform(identity); trans.scale(scale, scale); g2d.drawImage(image, trans, this); }
public void mouseDragged(MouseEvent e) { if (e.getModifiers() == modifiers) { Point2D pEndScale = e.getPoint(); double scale = pEndScale.distance(viewOrigin) / pStartScale.distance(viewOrigin); AffineTransform transform = super.plot.getTransform(); transform.setTransform( transform.getScaleX() * scale, transform.getShearY(), transform.getShearX(), transform.getScaleY() * scale, transform.getTranslateX() * scale, transform.getTranslateY() * scale); pStartScale = pEndScale; super.plot.setTransform(transform); } }
private List<Tile> toTiles( ImageReaderSpi provider, TileReaderPool readers, Set<ImageReaderSpi> providers, Path input) throws IOException { // Creates the tile for the first image, which usually have the maximal resolution. // The Tile constructor will read the TFW file and infer a provider if the given // 'provider' argument is null. If this is a new provider, then we need to declare // it to the pool of image readers before to use it. final List<Tile> tiles = new ArrayList<>(); final Tile root = new Tile(provider, input, 0); if (providers.add(root.getImageReaderSpi())) { readers.setProviders(providers); } final AffineTransform scaledGridToCRS = new AffineTransform(); final AffineTransform gridToCRS = root.getPendingGridToCRS(false); final ImageReader reader = root.getImageReader(readers, true, true); final int numImages = reader.getNumImages(false); // Result may be -1. for (int index = 0; index != numImages; index++) { // Intentional use of !=, not <. final int width, height; try { width = reader.getWidth(index); height = reader.getHeight(index); } catch (IndexOutOfBoundsException e) { // As explained in ImageReader javadoc, this approach is sometime // more efficient than invoking reader.getNumImages(true) first. break; } final Tile tile; if (index == 0) { tile = root; } else { final Rectangle region = root.getRegion(); scaledGridToCRS.setTransform(new AffineTransform(gridToCRS)); scaledGridToCRS.scale(region.width / (double) width, region.height / (double) height); tile = new Tile(root.getImageReaderSpi(), input, index, region, scaledGridToCRS); } tile.setSize(width, height); tiles.add(tile); } reader.dispose(); return tiles; }
/** * Applique un zoom à la perspective selon la direction de la molette. * * @param facteurZoom La direction de la molette. */ public void zoom(double wheelRotation) { /* * Pour conserver le zoom centrer, on doit d'abord déplacer le coin * supérieur gauche vers le centre, appliquer le zoom, puis revenir * vers la position originale. En utilisant la variation de dimension * avant et après le zoom, l'image ne reviendra pas exactement à sa * position originale, s'ajustant ainsi proportionnellement au zoom * appliqué. */ double largeur; double hauteur; // Garde en mémoire la transformation courant pileTransform.push(new AffineTransform(transformation)); zoom = 1; zoom += (.1 * -wheelRotation); // Calcul des dimensions avant le zoom largeur = panneauSource.getImagePlus().getWidth() * panneauSource.getImagePlus().getPerspective().getTransformation().getScaleX(); hauteur = panneauSource.getImagePlus().getHeight() * panneauSource.getImagePlus().getPerspective().getTransformation().getScaleY(); /* On translate l'image vers le centre. A noter que d'utiliser translate ou setToTranslate ne donne pas le résultat escompté en raison du "state" ou du fonctionnement un peu anodin du mutateur. */ transformation.setTransform( transformation.getScaleX(), 0, 0, transformation.getScaleY(), transformation.getTranslateX() + largeur / 2, transformation.getTranslateY() + hauteur / 2); // On applique le zoom transformation.scale(zoom, zoom); // Calcul des dimensions après le zoom largeur = panneauSource.getImagePlus().getWidth() * panneauSource.getImagePlus().getPerspective().getTransformation().getScaleX(); hauteur = panneauSource.getImagePlus().getHeight() * panneauSource.getImagePlus().getPerspective().getTransformation().getScaleY(); /* On translate l'image vers sa position d'origine. A noter que d'utiliser translate ou setToTranslate ne donne pas le résultat escompté en raison du "state" ou du fonctionnement un peu anodin du mutateur. */ transformation.setTransform( transformation.getScaleX(), 0, 0, transformation.getScaleY(), transformation.getTranslateX() - largeur / 2, transformation.getTranslateY() - hauteur / 2); setChanged(); notifyObservers(); }
/** * Run the test in the flipped or unflipped case. * * @param f -1 for the flipped case, or +1 for the unflipped case. */ private static void runTest(final int f) { // Test identity final AffineTransform tr = new AffineTransform(); tr.setToScale(1, f); assertEquals(1, XAffineTransform.getScaleX0(tr), EPS); assertEquals(1, XAffineTransform.getScaleY0(tr), EPS); assertEquals(0, XAffineTransform.getRotation(tr), EPS); assertEquals(1, XAffineTransform.getSwapXY(tr)); assertEquals(f, XAffineTransform.getFlip(tr)); assertEquals(f, getFlipFromType(tr)); // Tests rotation (< 45°) double r = Math.toRadians(25); tr.rotate(r); assertEquals(1, XAffineTransform.getScaleX0(tr), EPS); assertEquals(1, XAffineTransform.getScaleY0(tr), EPS); assertEquals(r, XAffineTransform.getRotation(tr), EPS); assertEquals(1, XAffineTransform.getSwapXY(tr)); assertEquals(f, XAffineTransform.getFlip(tr)); assertEquals(f, getFlipFromType(tr)); // Tests more rotation (> 45°) r = Math.toRadians(65); tr.rotate(Math.toRadians(40)); assertEquals(1, XAffineTransform.getScaleX0(tr), EPS); assertEquals(1, XAffineTransform.getScaleY0(tr), EPS); assertEquals(r, XAffineTransform.getRotation(tr), EPS); assertEquals(-1, XAffineTransform.getSwapXY(tr)); assertEquals(f, XAffineTransform.getFlip(tr)); assertEquals(f, getFlipFromType(tr)); // Tests scale tr.setToScale(2, 3 * f); assertEquals(2, XAffineTransform.getScaleX0(tr), EPS); assertEquals(3, XAffineTransform.getScaleY0(tr), EPS); assertEquals(0, XAffineTransform.getRotation(tr), EPS); assertEquals(1, XAffineTransform.getSwapXY(tr)); assertEquals(f, XAffineTransform.getFlip(tr)); assertEquals(f, getFlipFromType(tr)); // Tests rotation + scale tr.rotate(r); assertEquals(2, XAffineTransform.getScaleX0(tr), EPS); assertEquals(3, XAffineTransform.getScaleY0(tr), EPS); assertEquals(r, XAffineTransform.getRotation(tr), EPS); assertEquals(-1, XAffineTransform.getSwapXY(tr)); assertEquals(f, XAffineTransform.getFlip(tr)); assertEquals(1, getFlipFromType(tr)); // Always unflipped according Java 1.5.0_09... // Tests axis swapping r = Math.toRadians(-90 * f); tr.setTransform(0, 1, f, 0, 0, 0); assertEquals(1, XAffineTransform.getScaleX0(tr), EPS); assertEquals(1, XAffineTransform.getScaleY0(tr), EPS); assertEquals(r, XAffineTransform.getRotation(tr), EPS); assertEquals(-1, XAffineTransform.getSwapXY(tr)); assertEquals(-f, XAffineTransform.getFlip(tr)); assertEquals(-f, getFlipFromType(tr)); // Tests axis swapping + scale tr.scale(2, 3); assertEquals(3, XAffineTransform.getScaleX0(tr), EPS); assertEquals(2, XAffineTransform.getScaleY0(tr), EPS); assertEquals(r, XAffineTransform.getRotation(tr), EPS); assertEquals(-1, XAffineTransform.getSwapXY(tr)); assertEquals(-f, XAffineTransform.getFlip(tr)); assertEquals(-f, getFlipFromType(tr)); }
/** * Invoked automatically when a polyline is about to be draw. This implementation paints the * polyline according to the rendered style * * @param graphics The graphics in which to draw. * @param shape The polygon to draw. * @param style The style to apply, or <code>null</code> if none. * @param scale The scale denominator for the current zoom level * @throws FactoryException * @throws TransformException */ public void paint( final Graphics2D graphics, final LiteShape2 shape, final Style2D style, final double scale, boolean isLabelObstacle) { if (style == null) { // TODO: what's going on? Should not be reached... LOGGER.severe("ShapePainter has been asked to paint a null style!!"); return; } // Is the current scale within the style scale range? if (!style.isScaleInRange(scale)) { LOGGER.fine("Out of scale"); return; } if (style instanceof IconStyle2D) { AffineTransform temp = graphics.getTransform(); try { IconStyle2D icoStyle = (IconStyle2D) style; Icon icon = icoStyle.getIcon(); graphics.setComposite(icoStyle.getComposite()); // the displacement to be applied to all points, centers the icon and applies the // Graphic displacement as well float dx = icoStyle.getDisplacementX(); float dy = icoStyle.getDisplacementY(); // iterate over all points float[] coords = new float[2]; PathIterator citer = getPathIterator(shape); AffineTransform at = new AffineTransform(temp); while (!(citer.isDone())) { if (citer.currentSegment(coords) != PathIterator.SEG_MOVETO) { at.setTransform(temp); double x = coords[0] + dx; double y = coords[1] + dy; at.translate(x, y); at.rotate(icoStyle.getRotation()); at.translate( -(icon.getIconWidth() * icoStyle.getAnchorPointX()), (icon.getIconHeight() * (icoStyle.getAnchorPointY() - 1))); graphics.setTransform(at); icon.paintIcon(null, graphics, 0, 0); if (isLabelObstacle) { // TODO: rotation? labelCache.put( new Rectangle2D.Double(x, y, icon.getIconWidth(), icon.getIconHeight())); } } citer.next(); } } finally { graphics.setTransform(temp); } } else if (style instanceof MarkStyle2D) { PathIterator citer = getPathIterator(shape); // get the point onto the shape has to be painted float[] coords = new float[2]; MarkStyle2D ms2d = (MarkStyle2D) style; Shape transformedShape; while (!(citer.isDone())) { if (citer.currentSegment(coords) != PathIterator.SEG_MOVETO) { transformedShape = ms2d.getTransformedShape(coords[0], coords[1]); if (transformedShape != null) { if (ms2d.getFill() != null) { graphics.setPaint(ms2d.getFill()); graphics.setComposite(ms2d.getFillComposite()); graphics.fill(transformedShape); } if (ms2d.getContour() != null) { graphics.setPaint(ms2d.getContour()); graphics.setStroke(ms2d.getStroke()); graphics.setComposite(ms2d.getContourComposite()); graphics.draw(transformedShape); } if (isLabelObstacle) { labelCache.put(transformedShape.getBounds2D()); } } } citer.next(); } } else if (style instanceof GraphicStyle2D) { float[] coords = new float[2]; PathIterator iter = getPathIterator(shape); iter.currentSegment(coords); GraphicStyle2D gs2d = (GraphicStyle2D) style; BufferedImage image = gs2d.getImage(); double dx = gs2d.getDisplacementX() - gs2d.getAnchorPointX() * image.getWidth(); double dy = gs2d.getDisplacementY() - ((1 - gs2d.getAnchorPointY()) * image.getHeight()); while (!(iter.isDone())) { if (iter.currentSegment(coords) != PathIterator.SEG_MOVETO) { renderImage( graphics, coords[0], coords[1], dx, dy, image, gs2d.getRotation(), gs2d.getComposite(), isLabelObstacle); } iter.next(); } } else { if (isLabelObstacle) { labelCache.put(shape.getBounds2D()); } // if the style is a polygon one, process it even if the polyline is // not closed (by SLD specification) if (style instanceof PolygonStyle2D) { PolygonStyle2D ps2d = (PolygonStyle2D) style; if (ps2d.getFill() != null) { Paint paint = ps2d.getFill(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D cornerRect = tp.getAnchorRect(); Point2D anchorPoint = (Point2D) graphics.getRenderingHint(TEXTURE_ANCHOR_HINT_KEY); Rectangle2D alignedRect = null; if (anchorPoint != null) { alignedRect = new Rectangle2D.Double( Math.round(anchorPoint.getX()), Math.round(anchorPoint.getY()), cornerRect.getWidth(), cornerRect.getHeight()); } else { alignedRect = new Rectangle2D.Double(0.0, 0.0, cornerRect.getWidth(), cornerRect.getHeight()); } paint = new TexturePaint(image, alignedRect); } graphics.setPaint(paint); graphics.setComposite(ps2d.getFillComposite()); fillLiteShape(graphics, shape); } if (ps2d.getGraphicFill() != null) { Shape oldClip = graphics.getClip(); try { paintGraphicFill(graphics, shape, ps2d.getGraphicFill(), scale); } finally { graphics.setClip(oldClip); } } } if (style instanceof LineStyle2D) { LineStyle2D ls2d = (LineStyle2D) style; if (ls2d.getStroke() != null) { // see if a graphic stroke is to be used, the drawing method // is completely // different in this case if (ls2d.getGraphicStroke() != null) { drawWithGraphicsStroke( graphics, dashShape(shape, ls2d.getStroke()), ls2d.getGraphicStroke(), isLabelObstacle); } else { Paint paint = ls2d.getContour(); if (paint instanceof TexturePaint) { TexturePaint tp = (TexturePaint) paint; BufferedImage image = tp.getImage(); Rectangle2D rect = tp.getAnchorRect(); AffineTransform at = graphics.getTransform(); double width = rect.getWidth() * at.getScaleX(); double height = rect.getHeight() * at.getScaleY(); Rectangle2D scaledRect = new Rectangle2D.Double(0, 0, width, height); paint = new TexturePaint(image, scaledRect); } // debugShape(shape); Stroke stroke = ls2d.getStroke(); if (graphics.getRenderingHint(RenderingHints.KEY_ANTIALIASING) == RenderingHints.VALUE_ANTIALIAS_ON) { if (stroke instanceof BasicStroke) { BasicStroke bs = (BasicStroke) stroke; stroke = new BasicStroke( bs.getLineWidth() + 0.5f, bs.getEndCap(), bs.getLineJoin(), bs.getMiterLimit(), bs.getDashArray(), bs.getDashPhase()); } } graphics.setPaint(paint); graphics.setStroke(stroke); graphics.setComposite(ls2d.getContourComposite()); graphics.draw(shape); } } } } }
// overwrite of the paint method to do transforming, rescaling and error // handling @Override public synchronized void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g; if (image == null) { g2d.setFont(new Font("Tahoma", Font.BOLD, 14)); g2d.drawString("Image does not exist", 100, 100); return; } if (scrollPane != null) { dependentScale = getContainerDependingScaleFactor( image.getWidth(), image.getHeight(), scrollPane.getSize()); } finalScale = dependentScale * scale; if (paintOnResize) { paintOnResize = false; setPreferredSize( new Dimension( (int) (image.getWidth() * dependentScale * scale), (int) (image.getHeight() * dependentScale * scale))); } Insets insets = getInsets(); int tx = insets.left + originX; int ty = insets.top + originY; Rectangle clipBounds = g2d.getClipBounds(); g2d.setColor(getBackground()); g2d.fillRect(clipBounds.x, clipBounds.y, clipBounds.width, clipBounds.height); affineTrans = new AffineTransform(); affineTrans.setTransform(AffineTransform.getTranslateInstance(tx, ty)); if (finalScale != 0) { affineTrans.scale(finalScale, finalScale); } g2d.drawRenderedImage(image, affineTrans); // draw the lines g2d.setStroke(new BasicStroke(5)); g2d.setColor(Color.green); Point startPoint = testPoint[0]; for (int i = 1; i <= 4; i++) { g2d.drawLine( (int) ((startPoint.x + .5) * finalScale), (int) ((startPoint.y + .5) * finalScale), (int) ((testPoint[i % 4].x + .5) * finalScale), (int) ((testPoint[i % 4].y + .5) * finalScale)); startPoint = testPoint[i % 4]; } // Point first = null; // g2d.setStroke(new BasicStroke(5)); // g2d.setColor(Color.GREEN); // for (Point p : pointList) { // if (first == null) // first = p; // g2d.drawLine((int) ((first.x + .5) * finalScale), (int) ((first.y + // .5) * finalScale), // (int) ((p.x + .5) * finalScale), (int) ((p.y + .5) * finalScale)); // first = p; // } }
/** * Sets the current transform, which is concatenated with the base transform. * * @param Tx the new transform */ public void setTransform(AffineTransform Tx) { transform.setTransform(Tx); transform.concatenate(baseTransform); }
/** * Sets the base transform which is pre-concatenated with the current transform. * * @param Tx the new transform */ public void setBaseTransform(AffineTransform Tx) { baseTransform.setTransform(Tx); }