List of usage examples for java.awt Graphics2D setTransform
public abstract void setTransform(AffineTransform Tx);
From source file:org.jcurl.core.swing.WCComponent.java
private BufferedImage renderPng(final String watermark) { final BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB); final Graphics2D g2 = (Graphics2D) img.getGraphics(); {/* w ww . jav a 2 s . com*/ final Map<Key, Object> hints = new HashMap<Key, Object>(); hints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); hints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); hints.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); hints.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE); hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.addRenderingHints(hints); } final Font f0 = g2.getFont(); paint(g2); g2.setTransform(new AffineTransform()); if (watermark != null) { if (log.isDebugEnabled()) log.debug(f0); g2.setFont(f0); g2.setColor(new Color(0, 0, 0, 128)); g2.drawString(watermark, 10, 20); } g2.dispose(); return img; }
From source file:org.jhotdraw.samples.svg.figures.SVGImage.java
@Override public void draw(Graphics2D g) { AffineTransform t = g.getTransform(); double scaleX = t.getScaleX(); if (scaleX == 1.0) { g.drawImage(image, null, 0, 0);// w ww. j av a 2 s .c o m return; } g.scale(1 / scaleX, 1 / scaleX); BufferedImage scaledImage = scaledImages.get(scaleX); if (scaledImage == null) { int w = (int) (image.getWidth() * scaleX); int h = (int) (image.getHeight() * scaleX); scaledImage = new BufferedImage(w, h, image.getType()); Graphics2D g2 = scaledImage.createGraphics(); g2.drawImage(image, 0, 0, w, h, null); g2.dispose(); scaledImages.put((Double) scaleX, scaledImage); } g.drawImage(scaledImage, null, 0, 0); g.setTransform(t); }
From source file:org.openstreetmap.gui.jmapviewer.Tile.java
/** * 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.// w w w .ja v a2 s.co m */ 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; } } } }
From source file:org.pentaho.reporting.engine.classic.core.function.PaintDynamicComponentFunction.java
/** * Creates the component./*from w w w .j av a 2 s .c om*/ * * @return the created image or null, if no image could be created. */ private Image createComponentImage() { final Object o = getDataRow().get(getField()); if ((o instanceof Component) == false) { return null; } final float scale = getScale() * getDeviceScale(); final ComponentDrawable drawable = new ComponentDrawable(); drawable.setComponent((Component) o); drawable.setAllowOwnPeer(true); drawable.setPaintSynchronized(true); final Dimension dim = drawable.getSize(); final int width = Math.max(1, (int) (scale * dim.width)); final int height = Math.max(1, (int) (scale * dim.height)); final BufferedImage bi = ImageUtils.createTransparentImage(width, height); final Graphics2D graph = bi.createGraphics(); graph.setBackground(new Color(0, 0, 0, 0)); graph.setTransform(AffineTransform.getScaleInstance(scale, scale)); drawable.draw(graph, new Rectangle2D.Float(0, 0, dim.width, dim.height)); graph.dispose(); return bi; }
From source file:org.springframework.cloud.stream.app.pose.estimation.processor.PoseEstimateOutputMessageBuilder.java
private void drawPartOval(Part part, int radius, Graphics2D g) { int partX = part.getNormalizedX(); int partY = part.getNormalizedY(); g.setColor(GraphicsUtils.LIMBS_COLORS[part.getPartType().getId()]); g.fillOval(partX - radius, partY - radius, 2 * radius, 2 * radius); if (this.poseProperties.isDrawPartLabels()) { String label = part.getPartType().getId() + ":" + part.getPartType().name(); FontMetrics fm = g.getFontMetrics(); int labelX = partX + 5; int labelY = partY - 5; AffineTransform t = g.getTransform(); g.setTransform(AffineTransform.getRotateInstance(Math.toRadians(-35), labelX, labelY)); g.drawString(label, labelX, labelY); g.setTransform(t);/*from www . ja v a 2s . co m*/ } }
From source file:org.tinymediamanager.ui.components.ImageLabel.java
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (scaledImage != null) { int originalWidth = scaledImage.getWidth(null); int originalHeight = scaledImage.getHeight(null); // calculate new height/width int newWidth = 0; int newHeight = 0; int offsetX = 0; int offsetY = 0; if (drawBorder && !drawFullWidth) { Point size = ImageCache.calculateSize(this.getWidth() - 8, this.getHeight() - 8, originalWidth, originalHeight, true); // calculate offsets if (position == Position.TOP_RIGHT || position == Position.BOTTOM_RIGHT) { offsetX = this.getWidth() - size.x - 8; }//from ww w. ja v a 2 s.c o m if (position == Position.BOTTOM_LEFT || position == Position.BOTTOM_RIGHT) { offsetY = this.getHeight() - size.y - 8; } if (position == Position.CENTER) { offsetX = (this.getWidth() - size.x - 8) / 2; offsetY = (this.getHeight() - size.y - 8) / 2; } newWidth = size.x; newHeight = size.y; // when the image size differs too much - reload and rescale the original image recreateScaledImageIfNeeded(originalWidth, originalHeight, newWidth, newHeight); g.setColor(Color.BLACK); g.drawRect(offsetX, offsetY, size.x + 7, size.y + 7); g.setColor(Color.WHITE); g.fillRect(offsetX + 1, offsetY + 1, size.x + 6, size.y + 6); // g.drawImage(Scaling.scale(originalImage, newWidth, newHeight), offsetX + 4, offsetY + 4, newWidth, newHeight, this); g.drawImage(scaledImage, offsetX + 4, offsetY + 4, newWidth, newHeight, this); } else { Point size = null; if (drawFullWidth) { size = new Point(this.getWidth(), this.getWidth() * originalHeight / originalWidth); } else { size = ImageCache.calculateSize(this.getWidth(), this.getHeight(), originalWidth, originalHeight, true); } // calculate offsets if (position == Position.TOP_RIGHT || position == Position.BOTTOM_RIGHT) { offsetX = this.getWidth() - size.x; } if (position == Position.BOTTOM_LEFT || position == Position.BOTTOM_RIGHT) { offsetY = this.getHeight() - size.y; } if (position == Position.CENTER) { offsetX = (this.getWidth() - size.x) / 2; offsetY = (this.getHeight() - size.y) / 2; } newWidth = size.x; newHeight = size.y; // when the image size differs too much - reload and rescale the original image recreateScaledImageIfNeeded(originalWidth, originalHeight, newWidth, newHeight); // g.drawImage(Scaling.scale(originalImage, newWidth, newHeight), offsetX, offsetY, newWidth, newHeight, this); g.drawImage(scaledImage, offsetX, offsetY, newWidth, newHeight, this); } } else { // draw border and background if (drawBorder) { g.setColor(Color.BLACK); g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1); if (getParent().isOpaque()) { g.setColor(getParent().getBackground()); g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2); } } // calculate diagonal int diagonalSize = (int) Math .sqrt(this.getWidth() * this.getWidth() + this.getHeight() * this.getHeight()); // draw text String text = ""; if (alternativeText != null) { text = alternativeText; } else { text = BUNDLE.getString("image.nonefound"); //$NON-NLS-1$ } if (!getParent().isOpaque()) { text = ""; } Graphics2D g2 = (Graphics2D) g.create(); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); AffineTransform orig = g2.getTransform(); AffineTransform at = new AffineTransform(orig); at.translate(0, this.getHeight()); at.rotate(this.getWidth(), -this.getHeight()); g2.setTransform(at); g2.setColor(Color.BLACK); g2.setFont(FONT); FontMetrics fm = g2.getFontMetrics(); int x = (diagonalSize - fm.stringWidth(text)) / 2; int y = (fm.getAscent() - fm.getDescent()) / 2; g2.drawString(text, x, y); // g2.drawLine(0, 0, diagonalSize, 0); at.translate(0, -this.getHeight()); g2.setTransform(orig); } }
From source file:org.tros.logo.swing.LogoPanel.java
/** * Paint./*w w w.j av a2 s .c o m*/ * * @param g */ @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; turtleState.width = getWidth(); turtleState.height = getHeight(); draw(g2d, turtleState); if (turtleState.showTurtle && turtle != null) { double x = turtleState.penX - (turtle.getWidth() / 2.0); double y = turtleState.penY - (turtle.getHeight() / 2.0); AffineTransform translateInstance = AffineTransform .getRotateInstance(turtleState.angle + (Math.PI / 2.0), turtleState.penX, turtleState.penY); AffineTransform saveXform = g2d.getTransform(); g2d.transform(translateInstance); g2d.drawImage(turtle, (int) x, (int) y, null); g2d.setTransform(saveXform); } }
From source file:org.tros.logo.swing.LogoPanel.java
@Override public void drawString(final String message) { Drawable command = new Drawable() { @Override/* w w w . j a va 2 s. co m*/ public void draw(Graphics2D g2, TurtleState turtleState) { if (!turtleState.penup) { AffineTransform saveXform = g2.getTransform(); //double offsetAngle = (Math.PI / 2.0); double offsetAngle = 0; g2.setTransform(AffineTransform.getRotateInstance(turtleState.angle + offsetAngle, turtleState.penX, turtleState.penY)); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); g2.drawString(message, (int) turtleState.penX, (int) turtleState.penY); g2.setTransform(saveXform); } } @Override public void addListener(DrawListener listener) { } @Override public void removeListener(DrawListener listener) { } @Override public Drawable cloneDrawable() { return this; } }; submitCommand(command); }
From source file:org.uva.itast.blended.omr.pages.PageImage.java
/** * Scaled graphics for drawing on a small version of the page. * Transform is set for drawing in pixels refered to the original scanned image * @return/*from w ww .ja v a2 s . co m*/ */ public Graphics2D getReportingGraphics() { // TODO: Crear una transformacion equivalente a la de la pgina para la imagen reducida BufferedImage reportingImage = this.getReportingImage(); Graphics2D g = reportingImage.createGraphics(); // AffineTransform origTransf=getAllignmentInfo(); // // AffineTransform trans=(AffineTransform) origTransf.clone(); // trans.scale(reportingImage.getWidth()/(getImage().getWidth()), // reportingImage.getHeight()/(getImage().getHeight())); // AffineTransform trans=g.getTransform(); // trans.scale(reportingImage.getWidth()/(getPreferredHorizontalResolution()*PageImage.a4width), // reportingImage.getHeight()/(getPreferredVerticalResolution()*PageImage.a4height)); double scaleX = ((double) reportingImage.getWidth()) / (getImage().getWidth()); double scaleY = ((double) reportingImage.getHeight()) / (getImage().getHeight()); AffineTransform trans = g.getTransform(); trans.scale(scaleX, scaleY); g.setTransform(trans); return g; }
From source file:statechum.analysis.learning.Visualiser.java
protected static PluggableRenderer constructRenderer(Graph g, final LayoutOptions options) { final LayoutOptions graphLayoutOptions = options != null ? options : new LayoutOptions(); PluggableRenderer r = new PluggableRenderer() { /**//from ww w.ja va 2s.com * Draws the edge <code>e</code>, whose endpoints are at <code>(x1,y1)</code> * and <code>(x2,y2)</code>, on the graphics context <code>g</code>. * The <code>Shape</code> provided by the <code>EdgeShapeFunction</code> instance * is scaled in the x-direction so that its width is equal to the distance between * <code>(x1,y1)</code> and <code>(x2,y2)</code>. */ @Override protected void drawSimpleEdge(Graphics2D g2d, Edge e, int x1, int y1, int x2, int y2) { final Vertex v1 = (Vertex) e.getEndpoints().getFirst(); final Vertex v2 = (Vertex) e.getEndpoints().getSecond(); boolean isLoop = v1.equals(v2); final Shape s2 = vertexShapeFunction.getShape(v2); Shape edgeShape = edgeShapeFunction.getShape(e); final double dx = x2 - x1; final double dy = y2 - y1; boolean edgeHit = true; boolean arrowHit = true; Rectangle deviceRectangle = null; if (screenDevice != null) { Dimension d = screenDevice.getSize(); if (d.width <= 0 || d.height <= 0) { d = screenDevice.getPreferredSize(); } deviceRectangle = new Rectangle(0, 0, d.width, d.height); } String label = edgeStringer.getLabel(e); assert (label != null); Component labelComponent = prepareRenderer(graphLabelRenderer, label, isPicked(e), e); Dimension d = labelComponent.getPreferredSize(); Rectangle2D EdgeShapeBoundaries = edgeShape.getBounds2D(); AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1); double yMin = 0, yMax = 0; double thetaRadians = 0; if (isLoop) { // this is a self-loop. scale it is larger than the vertex // it decorates and translate it so that its nadir is // at the center of the vertex. int edgeIndex = ParallelEdgeIndexSingleton.getInstance().getIndex(e); Rectangle2D s2Bounds = s2.getBounds2D(); double scaleBy = 1 + (graphLayoutOptions.scaleLines - 1) * 1. / 3.; double translation = s2Bounds.getHeight() * (1. / 4. + edgeIndex / 4.); xform.translate(0, -scaleBy * translation); xform.scale(scaleBy * s2Bounds.getWidth(), scaleBy * s2Bounds.getHeight()); yMin = scaleBy * (EdgeShapeBoundaries.getMinY() * s2Bounds.getHeight()) - translation; yMax = scaleBy * (EdgeShapeBoundaries.getMaxY() * s2Bounds.getHeight()) - translation; } else { // this is a normal edge. Rotate it to the angle between // vertex endpoints, then scale it to the distance between // the vertices thetaRadians = Math.atan2(dy, dx); double dist = Math.sqrt(dx * dx + dy * dy); xform.rotate(thetaRadians); xform.scale(dist, 1.0); yMin = EdgeShapeBoundaries.getMinY(); yMax = EdgeShapeBoundaries.getMaxY(); } edgeShape = xform.createTransformedShape(edgeShape); // Debug code /* if (!isLoop) { g2d.setPaint(new Color( 250, 250, 0)); AffineTransform rect = AffineTransform.getTranslateInstance(x1, y1+yMin); rect.rotate(thetaRadians); g2d.fill(rect.createTransformedShape( new Rectangle(0,0,(int)Math.sqrt(dx*dx + dy*dy),(int)(yMax-yMin)))); } else { g2d.setPaint(new Color( 100, 250, 0)); AffineTransform rect = AffineTransform.getTranslateInstance(x1-s2.getBounds2D().getWidth()/2, y1+yMin); rect.rotate(thetaRadians); g2d.fill(rect.createTransformedShape( new Rectangle(0,0,(int)s2.getBounds2D().getWidth(),(int)(yMax-yMin)))); }*/ edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle); if (edgeHit == true) { Paint oldPaint = g2d.getPaint(); // get Paints for filling and drawing // (filling is done first so that drawing and label use same Paint) Paint fill_paint = edgePaintFunction.getFillPaint(e); if (fill_paint != null) { g2d.setPaint(fill_paint); g2d.fill(edgeShape); } Paint draw_paint = edgePaintFunction.getDrawPaint(e); if (draw_paint != null) { g2d.setPaint(draw_paint); g2d.draw(edgeShape); } double scalex = g2d.getTransform().getScaleX(); double scaley = g2d.getTransform().getScaleY(); // see if arrows are too small to bother drawing if (scalex < .3 || scaley < .3) return; if (edgeArrowPredicate.evaluate(e)) { Shape destVertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getSecond()); AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2); destVertexShape = xf.createTransformedShape(destVertexShape); arrowHit = viewTransformer.transform(destVertexShape).intersects(deviceRectangle); if (arrowHit) { AffineTransform at; if (edgeShape instanceof GeneralPath) at = getArrowTransform((GeneralPath) edgeShape, destVertexShape); else at = getArrowTransform(new GeneralPath(edgeShape), destVertexShape); if (at == null) return; Shape arrow = edgeArrowFunction.getArrow(e); arrow = at.createTransformedShape(arrow); // note that arrows implicitly use the edge's draw paint g2d.fill(arrow); } assert !(e instanceof UndirectedEdge); } // For difference visualisation only boolean labelBelow = false; if (graphLayoutOptions.showDIFF && (draw_paint == null || draw_paint instanceof Color && ((Color) draw_paint).equals(Color.BLACK))) labelBelow = true; // Now draw the label. double xLabel = 0, yLabel = 0, xa = 0, ya = 0, rotation = thetaRadians; if (isLoop) { double displacementY = labelBelow ? -yMin + d.height : -yMin + d.height, displacementX = d.width / 2; xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians); ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians); xLabel = xa - displacementX * Math.cos(thetaRadians); yLabel = ya - displacementX * Math.sin(thetaRadians); } else if (dx < 0) { double displacementY = labelBelow ? yMax - d.height : (-yMax - d.height), displacementX = d.width / 2; xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians); ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians); xLabel = xa + displacementX * Math.cos(thetaRadians); yLabel = ya + displacementX * Math.sin(thetaRadians); rotation = thetaRadians + Math.PI; } else { double displacementY = labelBelow ? yMax : -yMax, displacementX = d.width / 2; xa = x1 + dx / 2 + displacementY * Math.sin(thetaRadians); ya = y1 + dy / 2 - displacementY * Math.cos(thetaRadians); xLabel = xa - displacementX * Math.cos(thetaRadians); yLabel = ya - displacementX * Math.sin(thetaRadians); } AffineTransform old = g2d.getTransform(); AffineTransform labelTransform = new AffineTransform(); // Debug code: //g2d.drawLine((int)(x1+dx/2), (int)(y1+dy/2), (int)(xa), (int)(ya));g2d.drawLine((int)(xa), (int)(ya), (int)(xLabel), (int)(yLabel)); labelTransform.translate(xLabel, yLabel); labelTransform.rotate(rotation); g2d.setTransform(labelTransform); rendererPane.paintComponent(g2d, labelComponent, screenDevice, 0, 0, d.width, d.height, true); g2d.setTransform(old); // restore old paint g2d.setPaint(oldPaint); } // if edgeHit == true } }; r = labelEdges(g, r, graphLayoutOptions); r = labelVertices(r, g, graphLayoutOptions); r.setVertexIncludePredicate(new Predicate() { @Override public boolean evaluate(Object object) { if (!graphLayoutOptions.showIgnored && graphLayoutOptions.ignoredStates != null && graphLayoutOptions.ignoredStates.contains(object.toString())) return false; if (graphLayoutOptions.showNegatives) return true; else return DeterministicDirectedSparseGraph.isAccept((Vertex) object); } }); return r; }