List of usage examples for java.awt.geom AffineTransform translate
public void translate(double tx, double ty)
From source file:ScalingMethods.java
/** * Draws the picture five times, using the five different scaling * approaches described in the book. All five look the same, since * all are using default (nearest neighbor) filtering during the * scale operation./* w ww . j a v a 2s . c om*/ */ public void paintComponent(Graphics g) { int x = PADDING; int y = PADDING; // Simplest approach g.drawImage(picture, x, y, scaleW, scaleH, null); // Subregion approach x += scaleW + PADDING; g.drawImage(picture, x, y, x + scaleW, y + scaleH, 0, 0, picture.getWidth(), picture.getHeight(), null); // Graphics2D.scale approach x += scaleW + PADDING; Graphics2D g2d = (Graphics2D) g.create(); g2d.translate(x, y); g2d.scale(SCALE_FACTOR, SCALE_FACTOR); g2d.drawImage(picture, 0, 0, null); g2d.dispose(); // AffineTransform.scale approach x += scaleW + PADDING; g2d = (Graphics2D) g.create(); AffineTransform at = new AffineTransform(); at.translate(x, y); at.scale(SCALE_FACTOR, SCALE_FACTOR); g2d.drawImage(picture, at, null); g2d.dispose(); // getScaledInstance() approach x += scaleW + PADDING; Image scaledImg = picture.getScaledInstance(scaleW, scaleH, Image.SCALE_DEFAULT); g.drawImage(scaledImg, x, y, null); }
From source file:TransformersRotationTranslation.java
public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; // Use antialiasing. g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // Move the origin to 75, 75. AffineTransform at = AffineTransform.getTranslateInstance(75, 75); g2.transform(at);/*from w ww . ja v a2 s. c om*/ // Draw the shapes in their original locations. g2.setPaint(Color.black); g2.draw(axes); g2.draw(shape); // Transform the Graphics2D. AffineTransform rat = new AffineTransform(); rat.setToRotation(Math.PI / 6); rat.translate(100, 100); g2.transform(rat); // Draw the "new" shapes in dashed. Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 3, 1 }, 0); g2.setStroke(stroke); g2.draw(axes); g2.draw(shape); }
From source file:com.jakemadethis.graphite.visualization.renderers.HyperedgeLabelRenderer.java
public void labelEdge(RenderContext<V, E> rc, Layout<V, E> layout, E e, String label) { if (label == null || label.length() == 0) return;//www. j ava2 s .co m Graph<V, E> g = layout.getGraph(); EdgeLayout<E> edgeLayout = EdgeLayout$.MODULE$.apply(layout); Hypergraph<V, E> hg = g; if (!drawPredicate.evaluate(Context.<Hypergraph<V, E>, E>getInstance(hg, e))) return; if (!rc.getEdgeIncludePredicate().evaluate(Context.<Graph<V, E>, E>getInstance(g, e))) return; Point2D edgePos = edgeLayout.getEdgeLocation(e); edgePos = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, edgePos); GraphicsDecorator gd = rc.getGraphicsContext(); Component component = prepareRenderer(rc, rc.getEdgeLabelRenderer(), label, rc.getPickedEdgeState().isPicked(e), e); Dimension d = component.getPreferredSize(); AffineTransform old = gd.getTransform(); AffineTransform xform = new AffineTransform(old); xform.translate(edgePos.getX(), edgePos.getY()); gd.setTransform(xform); gd.setStroke(new BasicStroke(1)); Rectangle rect = new Rectangle(-10, -10, 20, 20); gd.setPaint(Color.WHITE); gd.fill(rect); gd.setPaint(Color.GRAY); gd.draw(rect); xform.translate(-d.width / 2, -d.height / 2); gd.setTransform(xform); gd.draw(component, rc.getRendererPane(), 0, 0, d.width, d.height, true); gd.setTransform(old); }
From source file:BufferedImageThread.java
public void paintComponent(Graphics g) { super.paintComponent(g); Dimension d = getSize();//w w w. ja va 2 s . co m bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); Graphics2D big = bi.createGraphics(); step(d.width, d.height); AffineTransform at = new AffineTransform(); at.setToIdentity(); at.translate(x, y); at.rotate(Math.toRadians(rotate)); at.scale(scale, scale); big.drawImage(image, at, this); Graphics2D g2D = (Graphics2D) g; g2D.drawImage(bi, 0, 0, null); big.dispose(); }
From source file:FontPaint.java
public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.white);//from w w w . ja v a 2 s .c o m int width = getSize().width; int height = getSize().height; Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("Helvetica", 1, 60); String s = new String("Java Source and Support."); TextLayout textTl = new TextLayout(s, f, frc); AffineTransform transform = new AffineTransform(); Shape outline = textTl.getOutline(null); Rectangle outlineBounds = outline.getBounds(); transform = g2.getTransform(); transform.translate(width / 2 - (outlineBounds.width / 2), height / 2 + (outlineBounds.height / 2)); g2.transform(transform); g2.setColor(Color.blue); g2.draw(outline); g2.setClip(outline); }
From source file:MainClass.java
public void paintToPDF(JTextPane ta) { try {//from ww w . j ava 2 s. com ta.setBounds(0, 0, (int) convertToPixels(612 - 58), (int) convertToPixels(792 - 60)); Document document = new Document(); FileOutputStream fos = new FileOutputStream("2.pdf"); PdfWriter writer = PdfWriter.getInstance(document, fos); document.setPageSize(new com.lowagie.text.Rectangle(612, 792)); document.open(); PdfContentByte cb = writer.getDirectContent(); cb.saveState(); cb.concatCTM(1, 0, 0, 1, 0, 0); DefaultFontMapper mapper = new DefaultFontMapper(); mapper.insertDirectory("c:/windows/fonts"); Graphics2D g2 = cb.createGraphics(612, 792, mapper, true, .95f); AffineTransform at = new AffineTransform(); at.translate(convertToPixels(20), convertToPixels(20)); at.scale(pixelToPoint, pixelToPoint); g2.transform(at); g2.setColor(Color.WHITE); g2.fill(ta.getBounds()); Rectangle alloc = getVisibleEditorRect(ta); ta.getUI().getRootView(ta).paint(g2, alloc); g2.setColor(Color.BLACK); g2.draw(ta.getBounds()); g2.dispose(); cb.restoreState(); document.close(); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Starry.java
public void paintComponent(Graphics g) { super.paintComponent(g); setBackground(Color.white);/* w w w. j a v a2 s . c o m*/ w = getSize().width; h = getSize().height; Graphics2D g2; g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); FontRenderContext frc = g2.getFontRenderContext(); Font f = new Font("Helvetica", 1, w / 10); String s = new String("The Starry Night"); TextLayout textTl = new TextLayout(s, f, frc); AffineTransform transform = new AffineTransform(); Shape outline = textTl.getOutline(null); Rectangle r = outline.getBounds(); transform = g2.getTransform(); transform.translate(w / 2 - (r.width / 2), h / 2 + (r.height / 2)); g2.transform(transform); g2.setColor(Color.blue); g2.draw(outline); g2.setClip(outline); g2.drawImage(img, r.x, r.y, r.width, r.height, this); }
From source file:edu.uci.ics.jung.visualization.util.VertexShapeFactory.java
/** * Returns a regular <code>num_sides</code>-sided * <code>Polygon</code> whose bounding * box's width and height are defined by this instance's size and * aspect ratio functions for this vertex. * @param num_sides the number of sides of the polygon; must be >= 3. *///from w w w . jav a2s . com public Shape getRegularPolygon(V v, int num_sides) { if (num_sides < 3) throw new IllegalArgumentException("Number of sides must be >= 3"); Rectangle2D frame = getRectangle(v); float width = (float) frame.getWidth(); float height = (float) frame.getHeight(); // generate coordinates double angle = 0; thePolygon.reset(); thePolygon.moveTo(0, 0); thePolygon.lineTo(width, 0); double theta = (2 * Math.PI) / num_sides; for (int i = 2; i < num_sides; i++) { angle -= theta; float delta_x = (float) (width * Math.cos(angle)); float delta_y = (float) (width * Math.sin(angle)); Point2D prev = thePolygon.getCurrentPoint(); thePolygon.lineTo((float) prev.getX() + delta_x, (float) prev.getY() + delta_y); } thePolygon.closePath(); // scale polygon to be right size, translate to center at (0,0) Rectangle2D r = thePolygon.getBounds2D(); double scale_x = width / r.getWidth(); double scale_y = height / r.getHeight(); float translationX = (float) (r.getMinX() + r.getWidth() / 2); float translationY = (float) (r.getMinY() + r.getHeight() / 2); AffineTransform at = AffineTransform.getScaleInstance(scale_x, scale_y); at.translate(-translationX, -translationY); Shape shape = at.createTransformedShape(thePolygon); return shape; }
From source file:edu.uci.ics.jung.visualization.util.VertexShapeFactory.java
/** * Returns a regular <code>Polygon</code> of <code>num_points</code> * points whose bounding /*from w ww . ja va 2 s. c om*/ * box's width and height are defined by this instance's size and * aspect ratio functions for this vertex. * @param num_points the number of points of the polygon; must be >= 5. */ public Shape getRegularStar(V v, int num_points) { if (num_points < 5) throw new IllegalArgumentException("Number of sides must be >= 5"); Rectangle2D frame = getRectangle(v); float width = (float) frame.getWidth(); float height = (float) frame.getHeight(); // generate coordinates double theta = (2 * Math.PI) / num_points; double angle = -theta / 2; thePolygon.reset(); thePolygon.moveTo(0, 0); float delta_x = width * (float) Math.cos(angle); float delta_y = width * (float) Math.sin(angle); Point2D prev = thePolygon.getCurrentPoint(); thePolygon.lineTo((float) prev.getX() + delta_x, (float) prev.getY() + delta_y); for (int i = 1; i < num_points; i++) { angle += theta; delta_x = width * (float) Math.cos(angle); delta_y = width * (float) Math.sin(angle); prev = thePolygon.getCurrentPoint(); thePolygon.lineTo((float) prev.getX() + delta_x, (float) prev.getY() + delta_y); angle -= theta * 2; delta_x = width * (float) Math.cos(angle); delta_y = width * (float) Math.sin(angle); prev = thePolygon.getCurrentPoint(); thePolygon.lineTo((float) prev.getX() + delta_x, (float) prev.getY() + delta_y); } thePolygon.closePath(); // scale polygon to be right size, translate to center at (0,0) Rectangle2D r = thePolygon.getBounds2D(); double scale_x = width / r.getWidth(); double scale_y = height / r.getHeight(); float translationX = (float) (r.getMinX() + r.getWidth() / 2); float translationY = (float) (r.getMinY() + r.getHeight() / 2); AffineTransform at = AffineTransform.getScaleInstance(scale_x, scale_y); at.translate(-translationX, -translationY); Shape shape = at.createTransformedShape(thePolygon); return shape; }
From source file:edu.umn.cs.spatialHadoop.operations.GeometricPlot.java
private static <S extends Shape> void plotLocal(Path inFile, Path outFile, OperationsParams params) throws IOException { int width = params.getInt("width", 1000); int height = params.getInt("height", 1000); Color strokeColor = params.getColor("color", Color.BLACK); int color = strokeColor.getRGB(); String hdfDataset = (String) params.get("dataset"); Shape shape = hdfDataset != null ? new NASARectangle() : (Shape) params.getShape("shape", null); Shape plotRange = params.getShape("rect", null); boolean keepAspectRatio = params.is("keep-ratio", true); String valueRangeStr = (String) params.get("valuerange"); MinMax valueRange;/*from ww w . j a v a 2 s. c o m*/ if (valueRangeStr == null) { valueRange = null; } else { String[] parts = valueRangeStr.split(","); valueRange = new MinMax(Integer.parseInt(parts[0]), Integer.parseInt(parts[1])); } InputSplit[] splits; FileSystem inFs = inFile.getFileSystem(params); FileStatus inFStatus = inFs.getFileStatus(inFile); if (inFStatus != null && !inFStatus.isDir()) { // One file, retrieve it immediately. // This is useful if the input is a hidden file which is automatically // skipped by FileInputFormat. We need to plot a hidden file for the case // of plotting partition boundaries of a spatial index splits = new InputSplit[] { new FileSplit(inFile, 0, inFStatus.getLen(), new String[0]) }; } else { JobConf job = new JobConf(params); ShapeInputFormat<Shape> inputFormat = new ShapeInputFormat<Shape>(); ShapeInputFormat.addInputPath(job, inFile); splits = inputFormat.getSplits(job, 1); } boolean vflip = params.is("vflip"); Rectangle fileMbr; if (plotRange != null) { fileMbr = plotRange.getMBR(); } else if (hdfDataset != null) { // Plotting a NASA file fileMbr = new Rectangle(-180, -90, 180, 90); } else { fileMbr = FileMBR.fileMBR(inFile, params); } if (keepAspectRatio) { // Adjust width and height to maintain aspect ratio if (fileMbr.getWidth() / fileMbr.getHeight() > (double) width / height) { // Fix width and change height height = (int) (fileMbr.getHeight() * width / fileMbr.getWidth()); } else { width = (int) (fileMbr.getWidth() * height / fileMbr.getHeight()); } } boolean adaptiveSample = shape instanceof Point && params.getBoolean("sample", false); float adaptiveSampleRatio = 0.0f; if (adaptiveSample) { // Calculate the sample ratio long recordCount = FileMBR.fileMBR(inFile, params).recordCount; adaptiveSampleRatio = params.getFloat(AdaptiveSampleFactor, 1.0f) * width * height / recordCount; } boolean gradualFade = !(shape instanceof Point) && params.getBoolean("fade", false); if (hdfDataset != null) { // Collects some stats about the HDF file if (valueRange == null) valueRange = Aggregate.aggregate(new Path[] { inFile }, params); NASAPoint.minValue = valueRange.minValue; NASAPoint.maxValue = valueRange.maxValue; NASAPoint.setColor1(params.getColor("color1", Color.BLUE)); NASAPoint.setColor2(params.getColor("color2", Color.RED)); NASAPoint.gradientType = params.getGradientType("gradient", NASAPoint.GradientType.GT_HUE); } double scale2 = (double) width * height / (fileMbr.getWidth() * fileMbr.getHeight()); double scale = Math.sqrt(scale2); // Create an image BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D graphics = image.createGraphics(); Color bg_color = params.getColor("bgcolor", new Color(0, 0, 0, 0)); graphics.setBackground(bg_color); graphics.clearRect(0, 0, width, height); graphics.setColor(strokeColor); for (InputSplit split : splits) { if (hdfDataset != null) { // Read points from the HDF file RecordReader<NASADataset, NASAShape> reader = new HDFRecordReader(params, (FileSplit) split, hdfDataset, true); NASADataset dataset = reader.createKey(); while (reader.next(dataset, (NASAShape) shape)) { // Skip with a fixed ratio if adaptive sample is set if (adaptiveSample && Math.random() > adaptiveSampleRatio) continue; if (plotRange == null || shape.isIntersected(shape)) { shape.draw(graphics, fileMbr, width, height, 0.0); } } reader.close(); } else { RecordReader<Rectangle, Shape> reader = new ShapeRecordReader<Shape>(params, (FileSplit) split); Rectangle cell = reader.createKey(); while (reader.next(cell, shape)) { // Skip with a fixed ratio if adaptive sample is set if (adaptiveSample && Math.random() > adaptiveSampleRatio) continue; Rectangle shapeMBR = shape.getMBR(); if (shapeMBR != null) { if (plotRange == null || shapeMBR.isIntersected(plotRange)) { if (gradualFade) { double sizeInPixels = (shapeMBR.getWidth() + shapeMBR.getHeight()) * scale; if (sizeInPixels < 1.0 && Math.round(sizeInPixels * 255) < 1.0) { // This shape can be safely skipped as it is too small to be plotted continue; } else { int alpha = (int) Math.round(sizeInPixels * 255); graphics.setColor(new Color((alpha << 24) | color, true)); } } shape.draw(graphics, fileMbr, width, height, scale2); } } } reader.close(); } } // Write image to output graphics.dispose(); if (vflip) { AffineTransform tx = AffineTransform.getScaleInstance(1, -1); tx.translate(0, -image.getHeight()); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); image = op.filter(image, null); } FileSystem outFs = outFile.getFileSystem(params); OutputStream out = outFs.create(outFile, true); ImageIO.write(image, "png", out); out.close(); }