List of usage examples for java.awt.geom AffineTransform getTranslateInstance
public static AffineTransform getTranslateInstance(double tx, double ty)
From source file:edu.uci.ics.jung.visualization.picking.ShapePickSupport.java
/** * Retrieves the shape template for <code>e</code> and * transforms it according to the positions of its endpoints * in <code>layout</code>.//from w ww . j a va 2 s. com * @param layout the <code>Layout</code> which specifies * <code>e</code>'s endpoints' positions * @param e the edge whose shape is to be returned * @return */ private Shape getTransformedEdgeShape(Layout<V, E> layout, E e) { Pair<V> pair = layout.getGraph().getEndpoints(e); V v1 = pair.getFirst(); V v2 = pair.getSecond(); boolean isLoop = v1.equals(v2); Point2D p1 = vv.getRenderContext().getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v1)); Point2D p2 = vv.getRenderContext().getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v2)); if (p1 == null || p2 == null) return null; float x1 = (float) p1.getX(); float y1 = (float) p1.getY(); float x2 = (float) p2.getX(); float y2 = (float) p2.getY(); // translate the edge to the starting vertex AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1); Shape edgeShape = vv.getRenderContext().getEdgeShapeTransformer() .transform(Context.<Graph<V, E>, E>getInstance(vv.getGraphLayout().getGraph(), e)); if (isLoop) { // make the loops proportional to the size of the vertex Shape s2 = vv.getRenderContext().getVertexShapeTransformer().transform(v2); Rectangle2D s2Bounds = s2.getBounds2D(); xform.scale(s2Bounds.getWidth(), s2Bounds.getHeight()); // move the loop so that the nadir is centered in the vertex xform.translate(0, -edgeShape.getBounds2D().getHeight() / 2); } else { float dx = x2 - x1; float dy = y2 - y1; // rotate the edge to the angle between the vertices double theta = Math.atan2(dy, dx); xform.rotate(theta); // stretch the edge to span the distance between the vertices float dist = (float) Math.sqrt(dx * dx + dy * dy); xform.scale(dist, 1.0f); } // transform the edge to its location and dimensions edgeShape = xform.createTransformedShape(edgeShape); return edgeShape; }
From source file:com.t_oster.visicut.misc.Helper.java
/** * Returns an AffineTransform, which transformes src to dest * and constists of a scale and translate component * @param src//from ww w . j a v a2s . c o m * @param dest * @return */ public static AffineTransform getTransform(Rectangle2D src, Rectangle2D dest) { AffineTransform scale = AffineTransform.getScaleInstance(dest.getWidth() / src.getWidth(), dest.getHeight() / src.getHeight()); Point2D scaled = scale.transform(new Point.Double(src.getX(), src.getY()), null); AffineTransform result = AffineTransform.getTranslateInstance(dest.getX() - scaled.getX(), dest.getY() - scaled.getY()); result.concatenate(scale); return result; }
From source file:graph.eventhandlers.MyEditingGraphMousePlugin.java
/** * code lifted from PluggableRenderer to move an edge shape into an * arbitrary position// w ww . j a v a 2s. c om */ private void transformEdgeShape(Point2D down, Point2D out) { float x1 = (float) down.getX(); float y1 = (float) down.getY(); float x2 = (float) out.getX(); float y2 = (float) out.getY(); AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1); float dx = x2 - x1; float dy = y2 - y1; float thetaRadians = (float) Math.atan2(dy, dx); xform.rotate(thetaRadians); float dist = (float) Math.sqrt(dx * dx + dy * dy); xform.scale(dist / rawEdge.getBounds().getWidth(), 1.0); edgeShape = xform.createTransformedShape(rawEdge); }
From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java
/** * Flip the image horizontal/*from w ww . j a va2 s .c o m*/ * * @param bufferedImage original image * @return horizontally flipped image */ private static BufferedImage flipHorizontal(BufferedImage bufferedImage) { AffineTransform at = AffineTransform.getTranslateInstance(bufferedImage.getWidth(), 0); at.scale(-1.0, 1.0); BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType()); return biOp.filter(bufferedImage, imgRes); }
From source file:graph.eventhandlers.MyEditingGraphMousePlugin.java
private void transformArrowShape(Point2D down, Point2D out) { float x1 = (float) down.getX(); float y1 = (float) down.getY(); float x2 = (float) out.getX(); float y2 = (float) out.getY(); AffineTransform xform = AffineTransform.getTranslateInstance(x2, y2); float dx = x2 - x1; float dy = y2 - y1; float thetaRadians = (float) Math.atan2(dy, dx); xform.rotate(thetaRadians);//from w w w . j a v a2 s .c o m arrowShape = xform.createTransformedShape(rawArrowShape); }
From source file:com.flexive.shared.media.impl.FxMediaNativeEngine.java
/** * Flip the image horizontal/* w w w . j a v a 2 s . c o m*/ * * @param bufferedImage original image * @return horizontally flipped image */ private static BufferedImage flipVertical(BufferedImage bufferedImage) { AffineTransform at = AffineTransform.getTranslateInstance(0, bufferedImage.getHeight()); at.scale(1.0, -1.0); BufferedImageOp biOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); BufferedImage imgRes = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getType()); return biOp.filter(bufferedImage, imgRes); }
From source file:business.ImageManager.java
public ImageIcon getArrow(double angle) { BufferedImage img = new BufferedImage(40, 40, BufferedImage.TRANSLUCENT); Graphics2D big = img.createGraphics(); //setup para os rastros big.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); big.setStroke(// ww w. j a v a2 s. c o m new BasicStroke(2f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 1f, new float[] { 5f }, 0f)); big.setColor(Color.red); int cx = this.getYellowBall().getIconWidth() / 2; int cy = this.getYellowBall().getIconHeight() / 2; AffineTransform at = AffineTransform.getTranslateInstance(cx, cy); at.rotate(Math.toRadians(angle)); // at.scale(2.0, 2.0); Shape shape = at.createTransformedShape(createArrow()); big.setPaint(Color.red); big.draw(shape); ImageIcon ret = new ImageIcon(img); return (ret); // tenta com o icone...angle. }
From source file:com.github.lindenb.jvarkit.tools.misc.BamCmpCoverage.java
@Override public Collection<Throwable> call() throws Exception { if (getOutputFile() == null) { return wrapException("output image file not defined"); }//from w w w . j av a2 s .co m if (this.imgageSize < 1) { return wrapException("Bad image size:" + this.imgageSize); } if (this.minDepth < 0) { return wrapException("Bad min depth : " + this.minDepth); } if (this.minDepth >= this.maxDepth) { return wrapException("Bad min<max depth : " + this.minDepth + "<" + this.maxDepth); } if (this.getBedFile() != null) { readBedFile(this.getBedFile()); } if (regionStr != null && this.intervals != null) { return wrapException("bed and interval both defined."); } final SamRecordFilter filter = new SamRecordFilter() { @Override public boolean filterOut(SAMRecord first, SAMRecord second) { return filterOut(first); } @Override public boolean filterOut(SAMRecord rec) { if (rec.getReadUnmappedFlag()) return true; if (rec.isSecondaryOrSupplementary()) return true; if (rec.getDuplicateReadFlag()) return true; if (rec.getNotPrimaryAlignmentFlag()) return true; if (rec.getReadFailsVendorQualityCheckFlag()) return true; if (rec.getMappingQuality() == 0) return true; /* ignore non-overlapping BED, already checked with QuertInterval if( intervals!=null && ! intervals.containsOverlapping( new Interval(rec.getReferenceName(), rec.getAlignmentStart(), rec.getAlignmentEnd())) ) { return true; } */ return false; } }; Set<File> files = new HashSet<File>(); try { SamReaderFactory srf = SamReaderFactory.makeDefault().validationStringency(ValidationStringency.SILENT); srf.disable(SamReaderFactory.Option.EAGERLY_DECODE); srf.disable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS); srf.disable(SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS); final List<String> args = this.getInputFiles(); for (String arg : args) { File f = new File(arg); if (f.getName().endsWith(".list")) { LOG.info("Reading BAM list from " + f); BufferedReader in = IOUtils.openFileForBufferedReading(f); String line; while ((line = in.readLine()) != null) { if (line.trim().isEmpty() || line.startsWith("#")) continue; files.add(new File(line)); } in.close(); } else { files.add(f); } } if (files.isEmpty()) { return wrapException("No BAM defined"); } Comparator<SAMRecord> comparator = new Comparator<SAMRecord>() { @Override public int compare(SAMRecord samRecord1, SAMRecord samRecord2) { final int refIndex1 = samRecord1.getReferenceIndex(); final int refIndex2 = samRecord2.getReferenceIndex(); if (refIndex1 == -1) { return (refIndex2 == -1 ? 0 : 1); } else if (refIndex2 == -1) { return -1; } final int cmp = refIndex1 - refIndex2; if (cmp != 0) { return cmp; } return samRecord1.getAlignmentStart() - samRecord2.getAlignmentStart(); } }; List<SamReader> readers = new ArrayList<SamReader>(files.size()); List<CloseableIterator<SAMRecord>> iterators = new ArrayList<CloseableIterator<SAMRecord>>( files.size()); Set<String> samples = new TreeSet<String>(); SAMSequenceDictionary dict = null; /* will be initialized below once, if needed */ QueryInterval queryIntervalArray[] = null; //scan samples names for (File bamFile : files) { SamReader r = srf.open(bamFile); readers.add(r); SAMFileHeader h = r.getFileHeader(); if (h.getSortOrder() != SortOrder.coordinate) { r.close(); return wrapException("file " + bamFile + " not sorted on coordinate"); } if (dict == null) { dict = h.getSequenceDictionary(); } else if (!SequenceUtil.areSequenceDictionariesEqual(dict, h.getSequenceDictionary())) { return wrapException("Found more than one dictint sequence dictionary"); } //fill query interval once List<QueryInterval> queryIntervals = new ArrayList<>(); if (regionStr != null && queryIntervalArray == null) { int colon = regionStr.indexOf(':'); String chrom; int chromStart; int chromEnd; if (colon == -1) { chrom = regionStr; } else { chrom = regionStr.substring(0, colon); } SAMSequenceRecord ssr = dict.getSequence(chrom); if (ssr == null) { return wrapException("Chromosome " + chrom + " not present in dictionary"); } int hyphen = regionStr.indexOf('-', colon + 1); if (hyphen != -1) { chromStart = Integer.parseInt(regionStr.substring(colon + 1, hyphen)); chromEnd = Integer.parseInt(regionStr.substring(hyphen + 1)); } else { chromStart = 0; chromEnd = ssr.getSequenceLength() - 1; } if (chromStart < 0 || chromEnd < chromStart) { return wrapException("bad position in " + regionStr); } queryIntervals.add(new QueryInterval(ssr.getSequenceIndex(), chromStart, chromEnd)); } if (this.intervals != null && queryIntervalArray == null) { for (Interval interval : this.intervals.keySet()) { SAMSequenceRecord ssr = dict.getSequence(interval.getContig()); if (ssr == null) { return wrapException( "Chromosome " + interval.getContig() + " not present in dictionary"); } queryIntervals.add( new QueryInterval(ssr.getSequenceIndex(), interval.getStart(), interval.getEnd())); } } if (!queryIntervals.isEmpty() && queryIntervalArray == null) { Collections.sort(queryIntervals); queryIntervalArray = queryIntervals.toArray(new QueryInterval[queryIntervals.size()]); } for (SAMReadGroupRecord rg : h.getReadGroups()) { String sample = rg.getSample(); if (sample == null) continue; samples.add(sample); } CloseableIterator<SAMRecord> reciterator = null; if (queryIntervalArray == null) { reciterator = r.iterator(); } else { reciterator = r.query(queryIntervalArray, false); } reciterator = new FilteringIterator(reciterator, filter); iterators.add(reciterator); } //free GC queryIntervalArray = null; LOG.info("Samples:" + samples.size()); for (String sample : samples) { this.sample2column.put(sample, this.sample2column.size()); } //create merging sam-reader MergingSamRecordIterator iter = new MergingSamRecordIterator(comparator, iterators); //create image LOG.info("Creating image " + this.imgageSize + "x" + this.imgageSize); this.image = new BufferedImage(this.imgageSize, this.imgageSize, BufferedImage.TYPE_INT_RGB); Graphics2D g = this.image.createGraphics(); this.marginWidth = this.imgageSize * 0.05; double drawingWidth = (this.imgageSize - 1) - marginWidth; this.sampleWidth = drawingWidth / samples.size(); //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setColor(Color.WHITE); g.fillRect(0, 0, this.imgageSize, this.imgageSize); g.setColor(Color.BLACK); Hershey hershey = new Hershey(); for (String sample_x : samples) { double labelHeight = marginWidth; if (labelHeight > 50) labelHeight = 50; g.setColor(Color.BLACK); hershey.paint(g, sample_x, marginWidth + sample2column.get(sample_x) * sampleWidth, marginWidth - labelHeight, sampleWidth * 0.9, labelHeight * 0.9); AffineTransform old = g.getTransform(); AffineTransform tr = AffineTransform.getTranslateInstance(marginWidth, marginWidth + sample2column.get(sample_x) * sampleWidth); tr.rotate(Math.PI / 2); g.setTransform(tr); hershey.paint(g, sample_x, 0.0, 0.0, sampleWidth * 0.9, labelHeight * 0.9); //g.drawString(this.tabixFile.getFile().getName(),0,0); g.setTransform(old); for (String sample_y : samples) { Rectangle2D rect = new Rectangle2D.Double( marginWidth + sample2column.get(sample_x) * sampleWidth, marginWidth + sample2column.get(sample_y) * sampleWidth, sampleWidth, sampleWidth); g.setColor(Color.BLUE); g.draw(new Line2D.Double(rect.getMinX(), rect.getMinY(), rect.getMaxX(), rect.getMaxY())); g.setColor(Color.BLACK); g.draw(rect); } } //ceate bit-array BitSampleMatrix bitMatrix = new BitSampleMatrix(samples.size()); //preivous chrom //int prev_tid=-1; BufferedList<Depth> depthList = new BufferedList<Depth>(); g.setColor(Color.BLACK); SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(dict); LOG.info("Scanning bams..."); while (iter.hasNext()) { SAMRecord rec = iter.next(); if (filter.filterOut(rec)) continue; progress.watch(rec); SAMReadGroupRecord gr = rec.getReadGroup(); if (gr == null) continue; String sample = gr.getSample(); if (sample == null) continue; int sample_id = this.sample2column.get(sample); Cigar cigar = rec.getCigar(); if (cigar == null) continue; int refPos = rec.getAlignmentStart(); /* cleanup front pos */ while (!depthList.isEmpty()) { Depth front = depthList.getFirst(); if (front.tid != rec.getReferenceIndex().intValue() || front.pos < refPos) { paint(bitMatrix, front); depthList.removeFirst(); continue; } else { break; } } for (CigarElement ce : cigar.getCigarElements()) { CigarOperator op = ce.getOperator(); if (!op.consumesReferenceBases()) continue; if (op.consumesReadBases()) { for (int i = 0; i < ce.getLength(); ++i) { Depth depth = null; int pos = refPos + i; //ignore non-overlapping BED if (this.intervals != null && !this.intervals .containsOverlapping(new Interval(rec.getReferenceName(), pos, pos))) { continue; } else if (depthList.isEmpty()) { depth = new Depth(); depth.pos = pos; depth.tid = rec.getReferenceIndex(); depthList.add(depth); } else if (depthList.getLast().pos < pos) { Depth prev = depthList.getLast(); while (prev.pos < pos) { depth = new Depth(); depth.pos = prev.pos + 1; depth.tid = rec.getReferenceIndex(); depthList.add(depth); prev = depth; } depth = prev; } else { int lastPos = depthList.get(depthList.size() - 1).pos; int distance = lastPos - pos; int indexInList = (depthList.size() - 1) - (distance); if (indexInList < 0) { //can appen when BED declared and partially overlap the read continue; } depth = depthList.get((depthList.size() - 1) - (distance)); if (depth.pos != pos) { return wrapException(" " + pos + " vs " + depth.pos + " " + lastPos); } } depth.depths[sample_id]++; } } refPos += ce.getLength(); } } while (!depthList.isEmpty()) { //paint(g,depthList.remove(0)); paint(bitMatrix, depthList.remove(0)); } progress.finish(); iter.close(); //paint bitset for (int x = 0; x < bitMatrix.n_samples; ++x) { for (int y = 0; y < bitMatrix.n_samples; ++y) { LOG.info("Painting...(" + x + "/" + y + ")"); paint(g, bitMatrix.get(x, y)); } } g.dispose(); //close readers for (SamReader r : readers) r.close(); //save file LOG.info("saving " + getOutputFile()); if (getOutputFile().getName().toLowerCase().endsWith(".png")) { ImageIO.write(this.image, "PNG", getOutputFile()); } else { ImageIO.write(this.image, "JPG", getOutputFile()); } return Collections.emptyList(); } catch (Exception err) { return wrapException(err); } finally { } }
From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java
/** * 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>. *///w w w . j a v a 2 s.c o m protected void drawSimpleEdge(Graphics2D g, Edge e, int x1, int y1, int x2, int y2) { Pair endpoints = e.getEndpoints(); Vertex v1 = (Vertex) endpoints.getFirst(); Vertex v2 = (Vertex) endpoints.getSecond(); boolean isLoop = v1.equals(v2); Shape s2 = vertexShapeFunction.getShape(v2); Shape edgeShape = edgeShapeFunction.getShape(e); 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); } AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1); 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. Rectangle2D s2Bounds = s2.getBounds2D(); xform.scale(s2Bounds.getWidth(), s2Bounds.getHeight()); xform.translate(0, -edgeShape.getBounds2D().getWidth() / 2); } else { // this is a normal edge. Rotate it to the angle between // vertex endpoints, then scale it to the distance between // the vertices float dx = x2 - x1; float dy = y2 - y1; float thetaRadians = (float) Math.atan2(dy, dx); xform.rotate(thetaRadians); float dist = (float) Math.sqrt(dx * dx + dy * dy); xform.scale(dist, 1.0); } edgeShape = xform.createTransformedShape(edgeShape); edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle); if (edgeHit == true) { Paint oldPaint = g.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) { g.setPaint(fill_paint); g.fill(edgeShape); } Paint draw_paint = edgePaintFunction.getDrawPaint(e); if (draw_paint != null) { g.setPaint(draw_paint); g.draw(edgeShape); } float scalex = (float) g.getTransform().getScaleX(); float scaley = (float) g.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 g.fill(arrow); } if (e instanceof UndirectedEdge) { Shape vertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getFirst()); xf = AffineTransform.getTranslateInstance(x1, y1); vertexShape = xf.createTransformedShape(vertexShape); arrowHit = viewTransformer.transform(vertexShape).intersects(deviceRectangle); if (arrowHit) { AffineTransform at; if (edgeShape instanceof GeneralPath) at = getReverseArrowTransform((GeneralPath) edgeShape, vertexShape, !isLoop); else at = getReverseArrowTransform(new GeneralPath(edgeShape), vertexShape, !isLoop); if (at == null) return; Shape arrow = edgeArrowFunction.getArrow(e); arrow = at.createTransformedShape(arrow); g.fill(arrow); } } } // use existing paint for text if no draw paint specified if (draw_paint == null) g.setPaint(oldPaint); String label = edgeStringer.getLabel(e); if (label != null) { labelEdge(g, e, label, x1, x2, y1, y2); } // restore old paint g.setPaint(oldPaint); } }
From source file:com.lizardtech.expresszip.model.Job.java
private BufferedImage mattCroppedImage(final BufferedImage source, GridCoverage2D cropped, int bufferType) { int height = source.getHeight(); int width = source.getWidth(); BufferedImage image = new BufferedImage(width, height, bufferType); Graphics2D gr = image.createGraphics(); // the way getRenderedImage works, this image should be drawn at 0, 0, not at based // on where the coverage's tile is AffineTransform at = AffineTransform.getTranslateInstance(0, 0); gr.drawRenderedImage(cropped.getRenderedImage(), at); return image; }