Example usage for java.awt Graphics2D setTransform

List of usage examples for java.awt Graphics2D setTransform

Introduction

In this page you can find the example usage for java.awt Graphics2D setTransform.

Prototype

public abstract void setTransform(AffineTransform Tx);

Source Link

Document

Overwrites the Transform in the Graphics2D context.

Usage

From source file:org.gvsig.remotesensing.scatterplot.chart.ScatterPlotDiagram.java

/**
 * Paints the component by drawing the chart to fill the entire component,
 * but allowing for the insets (which will be non-zero if a border has been
 * set for this component).  To increase performance (at the expense of
 * memory), an off-screen buffer image can be used.
 *
 * @param g  the graphics device for drawing on.
 *///from   ww w . ja  v a 2  s .  co  m
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (this.chart == null) {
        return;
    }
    Graphics2D g2 = (Graphics2D) g.create();

    // first determine the size of the chart rendering area...
    Dimension size = getSize();
    Insets insets = getInsets();
    Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top,
            size.getWidth() - insets.left - insets.right, size.getHeight() - insets.top - insets.bottom);

    // work out if scaling is required...
    boolean scale = false;
    double drawWidth = available.getWidth();
    double drawHeight = available.getHeight();
    this.scaleX = 1.0;
    this.scaleY = 1.0;

    if (drawWidth < this.minimumDrawWidth) {
        this.scaleX = drawWidth / this.minimumDrawWidth;
        drawWidth = this.minimumDrawWidth;
        scale = true;
    } else if (drawWidth > this.maximumDrawWidth) {
        this.scaleX = drawWidth / this.maximumDrawWidth;
        drawWidth = this.maximumDrawWidth;
        scale = true;
    }
    if (drawHeight < this.minimumDrawHeight) {
        this.scaleY = drawHeight / this.minimumDrawHeight;
        drawHeight = this.minimumDrawHeight;
        scale = true;
    } else if (drawHeight > this.maximumDrawHeight) {
        this.scaleY = drawHeight / this.maximumDrawHeight;
        drawHeight = this.maximumDrawHeight;
        scale = true;
    }

    Rectangle2D chartArea = new Rectangle2D.Double(0.0, 0.0, drawWidth, drawHeight);

    // are we using the chart buffer?
    if (this.useBuffer) {

        // if buffer is being refreshed, it needs clearing unless it is
        // new - use the following flag to track this...
        boolean clearBuffer = true;

        // do we need to resize the buffer?
        if ((this.chartBuffer == null) || (this.chartBufferWidth != available.getWidth())
                || (this.chartBufferHeight != available.getHeight())) {
            this.chartBufferWidth = (int) available.getWidth();
            this.chartBufferHeight = (int) available.getHeight();
            this.chartBuffer = createImage(this.chartBufferWidth, this.chartBufferHeight);
            //                GraphicsConfiguration gc = g2.getDeviceConfiguration();
            //                this.chartBuffer = gc.createCompatibleImage(
            //                        this.chartBufferWidth, this.chartBufferHeight, 
            //                        Transparency.TRANSLUCENT);
            this.refreshBuffer = true;
            clearBuffer = false; // buffer is new, no clearing required
        }

        // do we need to redraw the buffer?
        if (this.refreshBuffer) {

            this.refreshBuffer = false; // clear the flag

            Rectangle2D bufferArea = new Rectangle2D.Double(0, 0, this.chartBufferWidth,
                    this.chartBufferHeight);

            Graphics2D bufferG2 = (Graphics2D) this.chartBuffer.getGraphics();
            if (clearBuffer) {
                bufferG2.clearRect(0, 0, this.chartBufferWidth, this.chartBufferHeight);
            }
            if (scale) {
                AffineTransform saved = bufferG2.getTransform();
                AffineTransform st = AffineTransform.getScaleInstance(this.scaleX, this.scaleY);
                bufferG2.transform(st);
                this.chart.draw(bufferG2, chartArea, this.anchor, this.info);
                bufferG2.setTransform(saved);
            } else {
                this.chart.draw(bufferG2, bufferArea, this.anchor, this.info);
            }
        }
        g2.drawImage(this.chartBuffer, insets.left, insets.top, this);
        g2.draw((Shape) activeROI.getShapesList().get(1));
        // Dibujar las rois que se encuentren activas

    }

    // or redrawing the chart every time...
    else {

        AffineTransform saved = g2.getTransform();
        g2.translate(insets.left, insets.top);
        if (scale) {
            AffineTransform st = AffineTransform.getScaleInstance(this.scaleX, this.scaleY);
            g2.transform(st);
        }
        this.chart.draw(g2, chartArea, this.anchor, this.info);
        g2.drawImage(this.chartBuffer, insets.left, insets.top, this);

        // Se pintan las Roi activa
        drawsROIs(g2);
        g2.setTransform(saved);
    }

    g2.dispose();
    this.anchor = null;
}

From source file:MWC.GUI.JFreeChart.StepperChartPanel.java

/**
 * Paints the component by drawing the chart to fill the entire component,
 * but allowing for the insets (which will be non-zero if a border has been
 * set for this component).  To increase performance (at the expense of
 * memory), an off-screen buffer image can be used.
 *
 * @param g  the graphics device for drawing on.
 *//*w ww  . j  av a 2s .c  om*/
public void paintWMFComponent(final Graphics g) {

    final Graphics2D g2 = (Graphics2D) g;

    // first determine the size of the chart rendering area...
    final Dimension size = getSize();
    final Insets insets = getInsets();
    available.setRect(insets.left, insets.top, size.getWidth() - insets.left - insets.right,
            size.getHeight() - insets.top - insets.bottom);

    // work out if scaling is required...
    boolean scale = false;
    double drawWidth = available.getWidth();
    double drawHeight = available.getHeight();
    double scaleX = 1.0;
    double scaleY = 1.0;

    if (drawWidth < this.getMinimumDrawWidth()) {
        scaleX = drawWidth / getMinimumDrawWidth();
        drawWidth = getMinimumDrawWidth();
        scale = true;
    } else if (drawWidth > this.getMaximumDrawWidth()) {
        scaleX = drawWidth / getMaximumDrawWidth();
        drawWidth = getMaximumDrawWidth();
        scale = true;
    }

    if (drawHeight < this.getMinimumDrawHeight()) {
        scaleY = drawHeight / getMinimumDrawHeight();
        drawHeight = getMinimumDrawHeight();
        scale = true;
    } else if (drawHeight > this.getMaximumDrawHeight()) {
        scaleY = drawHeight / getMaximumDrawHeight();
        drawHeight = getMaximumDrawHeight();
        scale = true;
    }

    chartArea.setRect(0.0, 0.0, drawWidth, drawHeight);

    final AffineTransform saved = g2.getTransform();
    g2.translate(insets.left, insets.right);
    if (scale) {
        final AffineTransform st = AffineTransform.getScaleInstance(scaleX, scaleY);
        g2.transform(st);
    }
    getChart().draw(g2, chartArea, this.getChartRenderingInfo());
    g2.setTransform(saved);

}

From source file:ClipImage.java

public void drawDemo(Graphics2D g2) {

    if (newBufferedImage) {
        x = Math.random() * w;/*from   w  w  w  .  ja va2  s  .  c om*/
        y = Math.random() * h;
        ew = (Math.random() * w) / 2;
        eh = (Math.random() * h) / 2;
    }
    x += ix;
    y += iy;
    ew += iw;
    eh += ih;
    if (ew > w / 2) {
        ew = w / 2;
        iw = Math.random() * -w / 16 - 1;
    }
    if (ew < w / 8) {
        ew = w / 8;
        iw = Math.random() * w / 16 + 1;
    }
    if (eh > h / 2) {
        eh = h / 2;
        ih = Math.random() * -h / 16 - 1;
    }
    if (eh < h / 8) {
        eh = h / 8;
        ih = Math.random() * h / 16 + 1;
    }
    if ((x + ew) > w) {
        x = (w - ew) - 1;
        ix = Math.random() * -w / 32 - 1;
    }
    if (x < 0) {
        x = 2;
        ix = Math.random() * w / 32 + 1;
    }
    if ((y + eh) > h) {
        y = (h - eh) - 2;
        iy = Math.random() * -h / 32 - 1;
    }
    if (y < 0) {
        y = 2;
        iy = Math.random() * h / 32 + 1;
    }

    ellipse.setFrame(x, y, ew, eh);
    g2.setClip(ellipse);

    rect.setRect(x + 5, y + 5, ew - 10, eh - 10);
    g2.clip(rect);

    g2.drawImage(img, 0, 0, w, h, this);

    p.reset();
    p.moveTo(-w / 2.0f, -h / 8.0f);
    p.lineTo(+w / 2.0f, -h / 8.0f);
    p.lineTo(-w / 4.0f, +h / 2.0f);
    p.lineTo(+0.0f, -h / 2.0f);
    p.lineTo(+w / 4.0f, +h / 2.0f);
    p.closePath();

    at.setToIdentity();
    at.translate(w * .5f, h * .5f);
    g2.transform(at);
    g2.setStroke(bs);
    g2.setPaint(redBlend);
    g2.draw(p);

    at.setToIdentity();
    g2.setTransform(at);

    g2.setPaint(greenBlend);

    for (int yy = 0; yy < h; yy += 50) {
        for (int xx = 0, i = 0; xx < w; i++, xx += 50) {
            switch (i) {
            case 0:
                arc.setArc(xx, yy, 25, 25, 45, 270, Arc2D.PIE);
                g2.fill(arc);
                break;
            case 1:
                ellipse.setFrame(xx, yy, 25, 25);
                g2.fill(ellipse);
                break;
            case 2:
                roundRect.setRoundRect(xx, yy, 25, 25, 4, 4);
                g2.fill(roundRect);
                break;
            case 3:
                rect.setRect(xx, yy, 25, 25);
                g2.fill(rect);
                i = -1;
            }

        }
    }
}

From source file:ClipImage.java

public void drawDemo(Graphics2D g2) {
    if (newBufferedImage) {
        x = Math.random() * w;/*from w  w  w  . jav a 2  s.  c  o  m*/
        y = Math.random() * h;
        ew = (Math.random() * w) / 2;
        eh = (Math.random() * h) / 2;
    }
    x += ix;
    y += iy;
    ew += iw;
    eh += ih;
    if (ew > w / 2) {
        ew = w / 2;
        iw = Math.random() * -w / 16 - 1;
    }
    if (ew < w / 8) {
        ew = w / 8;
        iw = Math.random() * w / 16 + 1;
    }
    if (eh > h / 2) {
        eh = h / 2;
        ih = Math.random() * -h / 16 - 1;
    }
    if (eh < h / 8) {
        eh = h / 8;
        ih = Math.random() * h / 16 + 1;
    }
    if ((x + ew) > w) {
        x = (w - ew) - 1;
        ix = Math.random() * -w / 32 - 1;
    }
    if (x < 0) {
        x = 2;
        ix = Math.random() * w / 32 + 1;
    }
    if ((y + eh) > h) {
        y = (h - eh) - 2;
        iy = Math.random() * -h / 32 - 1;
    }
    if (y < 0) {
        y = 2;
        iy = Math.random() * h / 32 + 1;
    }

    ellipse.setFrame(x, y, ew, eh);
    g2.setClip(ellipse);

    rect.setRect(x + 5, y + 5, ew - 10, eh - 10);
    g2.clip(rect);

    g2.drawImage(img, 0, 0, w, h, this);

    p.reset();
    p.moveTo(-w / 2.0f, -h / 8.0f);
    p.lineTo(+w / 2.0f, -h / 8.0f);
    p.lineTo(-w / 4.0f, +h / 2.0f);
    p.lineTo(+0.0f, -h / 2.0f);
    p.lineTo(+w / 4.0f, +h / 2.0f);
    p.closePath();

    at.setToIdentity();
    at.translate(w * .5f, h * .5f);
    g2.transform(at);
    g2.setStroke(bs);
    g2.setPaint(redBlend);
    g2.draw(p);

    at.setToIdentity();
    g2.setTransform(at);

    g2.setPaint(greenBlend);

    for (int yy = 0; yy < h; yy += 50) {
        for (int xx = 0, i = 0; xx < w; i++, xx += 50) {
            switch (i) {
            case 0:
                arc.setArc(xx, yy, 25, 25, 45, 270, Arc2D.PIE);
                g2.fill(arc);
                break;
            case 1:
                ellipse.setFrame(xx, yy, 25, 25);
                g2.fill(ellipse);
                break;
            case 2:
                roundRect.setRoundRect(xx, yy, 25, 25, 4, 4);
                g2.fill(roundRect);
                break;
            case 3:
                rect.setRect(xx, yy, 25, 25);
                g2.fill(rect);
                i = -1;
            }

        }
    }
}

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");
    }//w  ww.jav  a  2  s.c om

    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:cn.pholance.datamanager.common.components.JRViewer.java

protected void paintPageError(Graphics2D grx) {
    AffineTransform origTransform = grx.getTransform();

    AffineTransform transform = new AffineTransform();
    transform.translate(1, 1);/*from  w w w.  j  ava2 s  .  c  om*/
    transform.scale(realZoom, realZoom);
    grx.transform(transform);

    try {
        drawPageError(grx);
    } finally {
        grx.setTransform(origTransform);
    }
}

From source file:forseti.JUtil.java

public static synchronized Image generarImagenMensaje(String mensaje, String nombreFuente, int tamanioFuente) {
    Frame f = new Frame();
    f.addNotify();//from  w w w . j a v  a  2s. c om
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    env.getAvailableFontFamilyNames();
    Font fuente = new Font(nombreFuente, Font.PLAIN, tamanioFuente);
    FontMetrics medidas = f.getFontMetrics(fuente);
    int anchoMensaje = medidas.stringWidth(mensaje);
    int lineaBaseX = anchoMensaje / 10;
    int ancho = anchoMensaje + 2 * (lineaBaseX + tamanioFuente);
    int alto = tamanioFuente * 7 / 2;
    int lineaBaseY = alto * 8 / 10;
    Image imagenMensaje = f.createImage(ancho, alto);
    Graphics2D g2d = (Graphics2D) imagenMensaje.getGraphics();
    g2d.setFont(fuente);
    g2d.translate(lineaBaseX, lineaBaseY);
    g2d.setPaint(Color.lightGray);
    AffineTransform origTransform = g2d.getTransform();
    g2d.shear(-0.95, 0);
    g2d.scale(1, 3);
    g2d.drawString(mensaje, 0, 0);
    g2d.setTransform(origTransform);
    g2d.setPaint(Color.black);
    g2d.drawString(mensaje, 0, 0);

    return (imagenMensaje);
}

From source file:com.isti.traceview.common.TraceViewChartPanel.java

/**
 * Paints the component by drawing the chart to fill the entire component, but allowing for the
 * insets (which will be non-zero if a border has been set for this component). To increase
 * performance (at the expense of memory), an off-screen buffer image can be used.
 * //from w w w.j a  va 2  s  . c o  m
 * @param g
 *            the graphics device for drawing on.
 */
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (this.chart == null) {
        return;
    }
    Graphics2D g2 = (Graphics2D) g.create();

    // first determine the size of the chart rendering area...
    Dimension size = getSize();
    Insets insets = getInsets();
    Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top,
            size.getWidth() - insets.left - insets.right, size.getHeight() - insets.top - insets.bottom);

    // work out if scaling is required...
    boolean scale = false;
    double drawWidth = available.getWidth();
    double drawHeight = available.getHeight();
    this.scaleX = 1.0;
    this.scaleY = 1.0;

    if (drawWidth < this.minimumDrawWidth) {
        this.scaleX = drawWidth / this.minimumDrawWidth;
        drawWidth = this.minimumDrawWidth;
        scale = true;
    } else if (drawWidth > this.maximumDrawWidth) {
        this.scaleX = drawWidth / this.maximumDrawWidth;
        drawWidth = this.maximumDrawWidth;
        scale = true;
    }

    if (drawHeight < this.minimumDrawHeight) {
        this.scaleY = drawHeight / this.minimumDrawHeight;
        drawHeight = this.minimumDrawHeight;
        scale = true;
    } else if (drawHeight > this.maximumDrawHeight) {
        this.scaleY = drawHeight / this.maximumDrawHeight;
        drawHeight = this.maximumDrawHeight;
        scale = true;
    }

    Rectangle2D chartArea = new Rectangle2D.Double(0.0, 0.0, drawWidth, drawHeight);

    // are we using the chart buffer?
    if (this.useBuffer) {
        // do we need to resize the buffer?
        if ((this.chartBuffer == null) || (this.chartBufferWidth != available.getWidth())
                || (this.chartBufferHeight != available.getHeight())) {
            this.chartBufferWidth = (int) available.getWidth();
            this.chartBufferHeight = (int) available.getHeight();
            this.chartBuffer = new BufferedImage(this.chartBufferWidth, this.chartBufferHeight,
                    BufferedImage.TYPE_INT_RGB);
            //this.chartBuffer = createImage(this.chartBufferWidth, this.chartBufferHeight);  -by Max
            // GraphicsConfiguration gc = g2.getDeviceConfiguration();
            // this.chartBuffer = gc.createCompatibleImage(
            // this.chartBufferWidth, this.chartBufferHeight,
            // Transparency.TRANSLUCENT);
            this.refreshBuffer = true;
        }

        // do we need to redraw the buffer?
        if (this.refreshBuffer) {
            Rectangle2D bufferArea = new Rectangle2D.Double(0, 0, this.chartBufferWidth,
                    this.chartBufferHeight);
            Graphics2D bufferG2 = (Graphics2D) this.chartBuffer.getGraphics();
            if (scale) {
                AffineTransform saved = bufferG2.getTransform();
                AffineTransform st = AffineTransform.getScaleInstance(this.scaleX, this.scaleY);
                bufferG2.transform(st);
                this.chart.draw(bufferG2, chartArea, this.anchor, this.info);
                bufferG2.setTransform(saved);
            } else {
                this.chart.draw(bufferG2, bufferArea, this.anchor, this.info);
            }

            this.refreshBuffer = false;
        }

        // zap the buffer onto the panel...
        g2.drawImage(this.chartBuffer, insets.left, insets.top, this);

    }

    // or redrawing the chart every time...
    else {
        AffineTransform saved = g2.getTransform();
        g2.translate(insets.left, insets.top);
        if (scale) {
            AffineTransform st = AffineTransform.getScaleInstance(this.scaleX, this.scaleY);
            g2.transform(st);
        }
        this.chart.draw(g2, chartArea, this.anchor, this.info);
        g2.setTransform(saved);
    }

    // Redraw the zoom rectangle (if present)
    drawZoomRectangle(g2);

    g2.dispose();
    this.anchor = null;
    this.verticalTraceLine = null;
    this.horizontalTraceLine = null;
}

From source file:edu.uci.ics.jung.visualization.PluggableRenderer.java

/**
 * Labels the specified non-self-loop edge with the specified label.
 * Uses the font specified by this instance's 
 * <code>EdgeFontFunction</code>.  (If the font is unspecified, the existing
 * font for the graphics context is used.)  Positions the 
 * label between the endpoints according to the coefficient returned
 * by this instance's edge label closeness function.
 *///w  w  w . j  a  v  a  2  s  .c  o m
protected void labelEdge(Graphics2D g2d, Edge e, String label, int x1, int x2, int y1, int y2) {
    int distX = x2 - x1;
    int distY = y2 - y1;
    double totalLength = Math.sqrt(distX * distX + distY * distY);

    double closeness = edgeLabelClosenessFunction.getNumber(e).doubleValue();

    int posX = (int) (x1 + (closeness) * distX);
    int posY = (int) (y1 + (closeness) * distY);

    int xDisplacement = (int) (LABEL_OFFSET * (distY / totalLength));
    int yDisplacement = (int) (LABEL_OFFSET * (-distX / totalLength));

    Component component = prepareRenderer(graphLabelRenderer, label, isPicked(e), e);

    Dimension d = component.getPreferredSize();

    Shape edgeShape = edgeShapeFunction.getShape(e);

    double parallelOffset = 1;

    parallelOffset += parallelEdgeIndexFunction.getIndex(e);

    if (edgeShape instanceof Ellipse2D) {
        parallelOffset += edgeShape.getBounds().getHeight();
        parallelOffset = -parallelOffset;
    }

    parallelOffset *= d.height;

    AffineTransform old = g2d.getTransform();
    AffineTransform xform = new AffineTransform(old);
    xform.translate(posX + xDisplacement, posY + yDisplacement);
    double dx = x2 - x1;
    double dy = y2 - y1;
    if (graphLabelRenderer.isRotateEdgeLabels()) {
        double theta = Math.atan2(dy, dx);
        if (dx < 0) {
            theta += Math.PI;
        }
        xform.rotate(theta);
    }
    if (dx < 0) {
        parallelOffset = -parallelOffset;
    }

    xform.translate(-d.width / 2, -(d.height / 2 - parallelOffset));
    g2d.setTransform(xform);
    rendererPane.paintComponent(g2d, component, screenDevice, 0, 0, d.width, d.height, true);
    g2d.setTransform(old);
}

From source file:org.rdv.viz.chart.ChartPanel.java

/**
 * Paints the component by drawing the chart to fill the entire component,
 * but allowing for the insets (which will be non-zero if a border has been
 * set for this component).  To increase performance (at the expense of
 * memory), an off-screen buffer image can be used.
 *
 * @param g  the graphics device for drawing on.
 *//*w  ww  .j  a v  a2 s.c om*/
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (this.chart == null) {
        return;
    }
    Graphics2D g2 = (Graphics2D) g.create();

    // first determine the size of the chart rendering area...
    Dimension size = getSize();
    Insets insets = getInsets();
    Rectangle2D available = new Rectangle2D.Double(insets.left, insets.top,
            size.getWidth() - insets.left - insets.right, size.getHeight() - insets.top - insets.bottom);

    // work out if scaling is required...
    boolean scale = false;
    double drawWidth = available.getWidth();
    double drawHeight = available.getHeight();
    this.scaleX = 1.0;
    this.scaleY = 1.0;

    if (drawWidth < this.minimumDrawWidth) {
        this.scaleX = drawWidth / this.minimumDrawWidth;
        drawWidth = this.minimumDrawWidth;
        scale = true;
    } else if (drawWidth > this.maximumDrawWidth) {
        this.scaleX = drawWidth / this.maximumDrawWidth;
        drawWidth = this.maximumDrawWidth;
        scale = true;
    }

    if (drawHeight < this.minimumDrawHeight) {
        this.scaleY = drawHeight / this.minimumDrawHeight;
        drawHeight = this.minimumDrawHeight;
        scale = true;
    } else if (drawHeight > this.maximumDrawHeight) {
        this.scaleY = drawHeight / this.maximumDrawHeight;
        drawHeight = this.maximumDrawHeight;
        scale = true;
    }

    Rectangle2D chartArea = new Rectangle2D.Double(0.0, 0.0, drawWidth, drawHeight);

    // are we using the chart buffer?
    if (this.useBuffer) {

        // if buffer is being refreshed, it needs clearing unless it is
        // new - use the following flag to track this...
        boolean clearBuffer = true;

        // do we need to resize the buffer?
        if ((this.chartBuffer == null) || (this.chartBufferWidth != available.getWidth())
                || (this.chartBufferHeight != available.getHeight())) {
            this.chartBufferWidth = (int) available.getWidth();
            this.chartBufferHeight = (int) available.getHeight();
            this.chartBuffer = createImage(this.chartBufferWidth, this.chartBufferHeight);
            //                GraphicsConfiguration gc = g2.getDeviceConfiguration();
            //                this.chartBuffer = gc.createCompatibleImage(
            //                        this.chartBufferWidth, this.chartBufferHeight,
            //                        Transparency.TRANSLUCENT);
            this.refreshBuffer = true;
            clearBuffer = false; // buffer is new, no clearing required
        }

        // do we need to redraw the buffer?
        if (this.refreshBuffer) {

            this.refreshBuffer = false; // clear the flag

            Rectangle2D bufferArea = new Rectangle2D.Double(0, 0, this.chartBufferWidth,
                    this.chartBufferHeight);

            Graphics2D bufferG2 = (Graphics2D) this.chartBuffer.getGraphics();
            if (clearBuffer) {
                bufferG2.clearRect(0, 0, this.chartBufferWidth, this.chartBufferHeight);
            }
            if (scale) {
                AffineTransform saved = bufferG2.getTransform();
                AffineTransform st = AffineTransform.getScaleInstance(this.scaleX, this.scaleY);
                bufferG2.transform(st);
                this.chart.draw(bufferG2, chartArea, this.anchor, this.info);
                bufferG2.setTransform(saved);
            } else {
                this.chart.draw(bufferG2, bufferArea, this.anchor, this.info);
            }

        }

        // zap the buffer onto the panel...
        g2.drawImage(this.chartBuffer, insets.left, insets.top, this);

    }

    // or redrawing the chart every time...
    else {

        AffineTransform saved = g2.getTransform();
        g2.translate(insets.left, insets.top);
        if (scale) {
            AffineTransform st = AffineTransform.getScaleInstance(this.scaleX, this.scaleY);
            g2.transform(st);
        }
        this.chart.draw(g2, chartArea, this.anchor, this.info);
        g2.setTransform(saved);

    }

    // Redraw the zoom rectangle (if present)
    drawZoomRectangle(g2);

    g2.dispose();

    this.anchor = null;
    this.verticalTraceLine = null;
    this.horizontalTraceLine = null;

}