Example usage for java.awt Graphics2D getTransform

List of usage examples for java.awt Graphics2D getTransform

Introduction

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

Prototype

public abstract AffineTransform getTransform();

Source Link

Document

Returns a copy of the current Transform in the Graphics2D context.

Usage

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.
 * //w w w .  j a  v a2 s.com
 * @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: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.
 *//*from  ww w.ja va  2s.  c o 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);
            }

        }

        // 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:ucar.unidata.idv.flythrough.Flythrough.java

/**
 * _more_/*ww  w .  j a  v  a 2s .c o  m*/
 *
 * @param g _more_
 * @param comp _more_
 */
public void paintDashboardAfter(Graphics g, JComponent comp) {

    Graphics2D g2 = (Graphics2D) g;
    AffineTransform oldTransform = g2.getTransform();
    Rectangle b = dashboardLbl.getBounds();
    int w = dashboardImage.getWidth(null);
    int h = dashboardImage.getHeight(null);
    Point ul = new Point(b.width / 2 - w / 2, b.height - h);
    int ptsIdx = 0;

    try {
        pipPanel.setPreferredSize(new Dimension(dialPts[ptsIdx][2], dialPts[ptsIdx][3]));
        pipFrame.setSize(dialPts[ptsIdx][2], dialPts[ptsIdx][3]);
        pipPanel.doLayout();
        pipPanel.validate();
        pipFrame.pack();
        pipPanel.resetDrawBounds();
        pipPanel.redraw();
    } catch (Exception ignore) {
    }

    JLabel locLbl = null;

    if (lastLocation != null) {
        try {
            locLbl = new JLabel("<html><table width=100%><tr><td align=right>&nbsp;Lat:</td></td>"
                    + getIdv().getDisplayConventions().formatLatLon(getLat(lastLocation)) + "</td></tr>"
                    + "<tr><td align=right>&nbsp;Lon:</td></td>"
                    + getIdv().getDisplayConventions().formatLatLon(getLon(lastLocation)) + "</td></tr>"
                    + "<tr><td align=right>&nbsp;Alt:</td></td>"
                    + getIdv().getDisplayConventions().formatDistance(getAlt(lastLocation)) + "</table>");
        } catch (Exception ignore) {
        }
    }
    if (locLbl == null) {
        locLbl = new JLabel(
                "<html><table width=100%><tr><td align=right>&nbsp;Lat:</td></td>N/A </td></tr><tr><td align=right>&nbsp;Lon:</td></td>N/A </td></tr><tr><td align=right>&nbsp;Alt:</td></td>N/A </table>");
    }
    locLbl.setOpaque(true);
    locLbl.setBackground(Color.white);

    DefaultValueDataset headingDataset = new DefaultValueDataset(new Double(currentHeading));

    CompassPlot plot = new CompassPlot(headingDataset);
    plot.setSeriesNeedle(0);
    plot.setSeriesPaint(0, Color.red);
    plot.setSeriesOutlinePaint(0, Color.red);
    JFreeChart chart = new JFreeChart("", plot);
    ChartPanel compassPanel = new ChartPanel(chart);

    plot.setBackgroundPaint(new Color(255, 255, 255, 0));
    plot.setBackgroundImageAlpha(0.0f);
    chart.setBackgroundPaint(new Color(255, 255, 255, 0));
    compassPanel.setBackground(new Color(255, 255, 255, 0));
    compassPanel.setPreferredSize(dialDimension);
    //        compassPanel.setSize(new Dimension(100,100));

    g2.setTransform(oldTransform);
    pipRect = drawDial(g2, pipPanel, ptsIdx++, ul);

    JFrame dummyFrame = new JFrame("");
    dummyFrame.setContentPane(compassPanel);
    dummyFrame.pack();

    g2.setTransform(oldTransform);
    drawDial(g2, compassPanel, ptsIdx++, ul);

    g2.setTransform(oldTransform);
    dummyFrame.setContentPane(locLbl);
    dummyFrame.pack();
    drawDial(g2, locLbl, ptsIdx++, ul);

    if (showReadout) {
        for (JComponent dial : dials) {
            dummyFrame.setContentPane(dial);
            dummyFrame.pack();

            g2.setTransform(oldTransform);
            drawDial(g2, dial, ptsIdx++, ul);
        }
    }

    g2.setTransform(oldTransform);

}

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>.
 *//*from w ww.j  av 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.rapidminer.gui.plotter.charts.AbstractChartPanel.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.
 * //  w  w  w. j a v a2 s. com
 * @param g
 *            the graphics device for drawing on.
 */

@Override
public void paintComponent(Graphics 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);
    // redrawing the chart every time...

    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);

    Iterator<Overlay> iterator = this.overlays.iterator();
    while (iterator.hasNext()) {
        Overlay overlay = iterator.next();
        overlay.paintOverlay(g2, this);
    }

    // redraw the zoom rectangle (if present) - if useBuffer is false,
    // we use XOR so we can XOR the rectangle away again without redrawing
    // the chart
    drawSelectionRectangle(g2);

    g2.dispose();

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

}

From source file:org.forester.archaeopteryx.TreePanel.java

final private void paintNodeDataUnrootedCirc(final Graphics2D g, final PhylogenyNode node, final boolean to_pdf,
        final boolean to_graphics_file, final boolean radial_labels, final double ur_angle,
        final boolean is_in_found_nodes) {
    if (isNodeDataInvisibleUnrootedCirc(node) && !to_graphics_file && !to_pdf) {
        return;//from  w ww .  ja v  a2  s  .c o  m
    }
    if ((to_pdf || to_graphics_file) && getOptions().isPrintBlackAndWhite()) {
        g.setColor(Color.BLACK);
    } else if (is_in_found_nodes) {
        g.setColor(getTreeColorSet().getFoundColor());
    } else if (getControlPanel().isColorAccordingToTaxonomy()) {
        g.setColor(getTaxonomyBasedColor(node));
    } else {
        g.setColor(getTreeColorSet().getSequenceColor());
    }
    _sb.setLength(0);
    _sb.append(" ");
    if (node.getNodeData().isHasTaxonomy()
            && (getControlPanel().isShowTaxonomyCode() || getControlPanel().isShowTaxonomyNames())) {
        final Taxonomy taxonomy = node.getNodeData().getTaxonomy();
        if (_control_panel.isShowTaxonomyCode() && !ForesterUtil.isEmpty(taxonomy.getTaxonomyCode())) {
            _sb.append(taxonomy.getTaxonomyCode());
            _sb.append(" ");
        }
        if (_control_panel.isShowTaxonomyNames()) {
            if (!ForesterUtil.isEmpty(taxonomy.getScientificName())
                    && !ForesterUtil.isEmpty(taxonomy.getCommonName())) {
                _sb.append(taxonomy.getScientificName());
                _sb.append(" (");
                _sb.append(taxonomy.getCommonName());
                _sb.append(") ");
            } else if (!ForesterUtil.isEmpty(taxonomy.getScientificName())) {
                _sb.append(taxonomy.getScientificName());
                _sb.append(" ");
            } else if (!ForesterUtil.isEmpty(taxonomy.getCommonName())) {
                _sb.append(taxonomy.getCommonName());
                _sb.append(" ");
            }
        }
    }
    if (node.isCollapse() && ((!node.isRoot() && !node.getParent().isCollapse()) || node.isRoot())) {
        _sb.append(" [");
        _sb.append(node.getAllExternalDescendants().size());
        _sb.append("]");
    }
    if (getControlPanel().isShowNodeNames() && (node.getNodeName().length() > 0)) {
        if (_sb.length() > 0) {
            _sb.append(" ");
        }
        _sb.append(node.getNodeName());
    }
    if (node.getNodeData().isHasSequence()) {
        if (getControlPanel().isShowSequenceAcc()
                && (node.getNodeData().getSequence().getAccession() != null)) {
            if (_sb.length() > 0) {
                _sb.append(" ");
            }
            if (!ForesterUtil.isEmpty(node.getNodeData().getSequence().getAccession().getSource())) {
                _sb.append(node.getNodeData().getSequence().getAccession().getSource());
                _sb.append(":");
            }
            _sb.append(node.getNodeData().getSequence().getAccession().getValue());
        }
        if (getControlPanel().isShowGeneNames() && (node.getNodeData().getSequence().getName().length() > 0)) {
            //                if ( _sb.length() > 0 ) {
            //                    _sb.append( " " );
            //                }
            //                _sb.append( node.getNodeData().getSequence().getName() );
            PhylogenyNode parent = node.getParent();
            double x1 = parent.getXcoord();
            double y1 = parent.getYcoord();
            double x2 = node.getXcoord();
            double y2 = node.getYcoord();
            double above_the_line = 1.0;

            if (getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.UNROOTED) {
                double center_of_branch_x = Math.abs(x1 + x2) / 2.0;
                double center_of_branch_y = Math.abs(y1 + y2) / 2.0;
                TreePanel.drawString(node.getNodeData().getSequence().getName(), center_of_branch_x,
                        center_of_branch_y - above_the_line, g);
            }
        }
    }
    g.setFont(getTreeFontSet().getLargeFont());
    if (is_in_found_nodes) {
        g.setFont(getTreeFontSet().getLargeFont().deriveFont(Font.BOLD));
    }
    if (_sb.length() > 1) {
        final String sb_str = _sb.toString();
        double m = 0;
        if (_graphics_type == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR) {
            m = _urt_nodeid_angle_map.get(node.getNodeId()) % TWO_PI;
        } else {
            m = (float) (ur_angle % TWO_PI);
        }
        _at = g.getTransform();
        boolean need_to_reset = false;
        final float x_coord = node.getXcoord();
        final float y_coord = node.getYcoord() + (getTreeFontSet()._fm_large.getAscent() / 3.0f);
        if (radial_labels) {
            need_to_reset = true;
            boolean left = false;
            if ((m > HALF_PI) && (m < ONEHALF_PI)) {
                m -= PI;
                left = true;
            }
            g.rotate(m, x_coord, node.getYcoord());
            if (left) {
                g.translate(-(getTreeFontSet()._fm_large.getStringBounds(sb_str, g).getWidth()), 0);
            }
        } else {
            if ((m > HALF_PI) && (m < ONEHALF_PI)) {
                need_to_reset = true;
                g.translate(-getTreeFontSet()._fm_large.getStringBounds(sb_str, g).getWidth(), 0);
            }
        }
        TreePanel.drawString(sb_str, x_coord, y_coord, g);
        if (need_to_reset) {
            g.setTransform(_at);
        }
    }
}

From source file:org.forester.archaeopteryx.TreePanel.java

final void paintPhylogeny(final Graphics2D g, final boolean to_pdf, final boolean to_graphics_file,
        final int graphics_file_width, final int graphics_file_height, final int graphics_file_x,
        final int graphics_file_y) {
    /* GUILHEM_BEG */
    _query_sequence = _control_panel.getSelectedQuerySequence();
    /* GUILHEM_END */
    // Color the background
    if (!to_pdf) {
        final Rectangle r = getVisibleRect();
        if (!getOptions().isBackgroundColorGradient() || getOptions().isPrintBlackAndWhite()) {
            g.setColor(getTreeColorSet().getBackgroundColor());
            if (!to_graphics_file) {
                g.fill(r);//from   ww  w . j  a v a  2  s.co m
            } else {
                if (getOptions().isPrintBlackAndWhite()) {
                    g.setColor(Color.WHITE);
                }
                g.fillRect(graphics_file_x, graphics_file_y, graphics_file_width, graphics_file_height);
            }
        } else {
            if (!to_graphics_file) {
                g.setPaint(new GradientPaint(r.x, r.y, getTreeColorSet().getBackgroundColor(), r.x,
                        r.y + r.height, getTreeColorSet().getBackgroundColorGradientBottom()));
                g.fill(r);
            } else {
                g.setPaint(new GradientPaint(graphics_file_x, graphics_file_y,
                        getTreeColorSet().getBackgroundColor(), graphics_file_x,
                        graphics_file_y + graphics_file_height,
                        getTreeColorSet().getBackgroundColorGradientBottom()));
                g.fillRect(graphics_file_x, graphics_file_y, graphics_file_width, graphics_file_height);
            }
        }
        g.setStroke(new BasicStroke(1));
    } else {
        g.setStroke(new BasicStroke(getOptions().getPrintLineWidth()));
    }
    if ((getPhylogenyGraphicsType() != PHYLOGENY_GRAPHICS_TYPE.UNROOTED)
            && (getPhylogenyGraphicsType() != PHYLOGENY_GRAPHICS_TYPE.CIRCULAR)) {
        _external_node_index = 0;
        // Position starting X of tree
        if (!_phylogeny.isRooted()) {
            _phylogeny.getRoot().setXcoord(TreePanel.MOVE);
        } else if ((_phylogeny.getRoot().getDistanceToParent() > 0.0) && getControlPanel().isDrawPhylogram()) {
            _phylogeny.getRoot().setXcoord((float) (TreePanel.MOVE
                    + (_phylogeny.getRoot().getDistanceToParent() * getXcorrectionFactor())));
        } else {
            _phylogeny.getRoot().setXcoord(TreePanel.MOVE + getXdistance());
        }
        // Position starting Y of tree
        _phylogeny.getRoot().setYcoord(
                (getYdistance() * _phylogeny.getRoot().getNumberOfExternalNodes()) + (TreePanel.MOVE / 2.0f));
        final int dynamic_hiding_factor = (int) (getTreeFontSet()._fm_large.getHeight()
                / (1.5 * getYdistance()));
        if (getControlPanel().isDynamicallyHideData()) {
            if (dynamic_hiding_factor > 1) {
                getControlPanel().setDynamicHidingIsOn(true);
            } else {
                getControlPanel().setDynamicHidingIsOn(false);
            }
        }
        final PhylogenyNodeIterator it;
        for (it = _phylogeny.iteratorPreorder(); it.hasNext();) {
            paintNodeRectangular(g, it.next(), to_pdf,
                    getControlPanel().isDynamicallyHideData() && (dynamic_hiding_factor > 1),
                    dynamic_hiding_factor, to_graphics_file);
        }
        if (getOptions().isShowScale()) {
            if (!(to_graphics_file || to_pdf)) {
                paintScale(g, getVisibleRect().x, getVisibleRect().y + getVisibleRect().height, to_pdf,
                        to_graphics_file);
            } else {
                paintScale(g, graphics_file_x, graphics_file_y + graphics_file_height, to_pdf,
                        to_graphics_file);
            }
        }
        if (getOptions().isShowOverview() && isOvOn() && !to_graphics_file && !to_pdf) {
            paintPhylogenyLite(g);
        }
    } else if (getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.UNROOTED) {
        if (getControlPanel().getDynamicallyHideData() != null) {
            getControlPanel().setDynamicHidingIsOn(false);
        }
        final double angle = getStartingAngle();
        final boolean radial_labels = getOptions().getNodeLabelDirection() == NODE_LABEL_DIRECTION.RADIAL;
        _dynamic_hiding_factor = 0;
        if (getControlPanel().isDynamicallyHideData()) {
            _dynamic_hiding_factor = (int) ((getTreeFontSet()._fm_large.getHeight() * 1.5
                    * getPhylogeny().getNumberOfExternalNodes()) / (TWO_PI * 10));
        }
        if (getControlPanel().getDynamicallyHideData() != null) {
            if (_dynamic_hiding_factor > 1) {
                getControlPanel().setDynamicHidingIsOn(true);
            } else {
                getControlPanel().setDynamicHidingIsOn(false);
            }
        }
        paintUnrooted(_phylogeny.getRoot(), angle, (float) (angle + 2 * Math.PI), radial_labels, g, to_pdf,
                to_graphics_file);
        if (getOptions().isShowScale()) {
            if (!(to_graphics_file || to_pdf)) {
                paintScale(g, getVisibleRect().x, getVisibleRect().y + getVisibleRect().height, to_pdf,
                        to_graphics_file);
            } else {
                paintScale(g, graphics_file_x, graphics_file_y + graphics_file_height, to_pdf,
                        to_graphics_file);
            }
        }
        if (getOptions().isShowOverview() && isOvOn() && !to_graphics_file && !to_pdf) {
            g.setColor(getTreeColorSet().getOvColor());
            paintUnrootedLite(_phylogeny.getRoot(), angle, angle + 2 * Math.PI, g,
                    (getUrtFactorOv() / (getVisibleRect().width / getOvMaxWidth())));
            paintOvRectangle(g);
        }
    } else if (getPhylogenyGraphicsType() == PHYLOGENY_GRAPHICS_TYPE.CIRCULAR) {
        final int radius = (int) ((Math.min(getPreferredSize().getWidth(), getPreferredSize().getHeight()) / 2)
                - (MOVE + getLongestExtNodeInfo()));
        final int d = radius + MOVE + getLongestExtNodeInfo();
        _dynamic_hiding_factor = 0;
        if (getControlPanel().isDynamicallyHideData() && (radius > 0)) {
            _dynamic_hiding_factor = (int) ((getTreeFontSet()._fm_large.getHeight() * 1.5
                    * getPhylogeny().getNumberOfExternalNodes()) / (TWO_PI * radius));
        }
        if (getControlPanel().getDynamicallyHideData() != null) {
            if (_dynamic_hiding_factor > 1) {
                getControlPanel().setDynamicHidingIsOn(true);
            } else {
                getControlPanel().setDynamicHidingIsOn(false);
            }
        }
        paintCircular(_phylogeny, getStartingAngle(), d, d, radius > 0 ? radius : 0, g, to_pdf,
                to_graphics_file);
        if (getOptions().isShowOverview() && isOvOn() && !to_graphics_file && !to_pdf) {
            final int radius_ov = (int) (getOvMaxHeight() < getOvMaxWidth() ? getOvMaxHeight() / 2
                    : getOvMaxWidth() / 2);
            double x_scale = 1.0;
            double y_scale = 1.0;
            int x_pos = getVisibleRect().x + getOvXPosition();
            int y_pos = getVisibleRect().y + getOvYPosition();
            if (getWidth() > getHeight()) {
                x_scale = (double) getHeight() / getWidth();
                x_pos = ForesterUtil.roundToInt(x_pos / x_scale);
            } else {
                y_scale = (double) getWidth() / getHeight();
                y_pos = ForesterUtil.roundToInt(y_pos / y_scale);
            }
            _at = g.getTransform();
            g.scale(x_scale, y_scale);
            paintCircularLite(_phylogeny, getStartingAngle(), x_pos + radius_ov, y_pos + radius_ov,
                    (int) (radius_ov - (getLongestExtNodeInfo()
                            / (getVisibleRect().width / getOvRectangle().getWidth()))),
                    g);
            g.setTransform(_at);
            paintOvRectangle(g);
        }
    }
}

From source file:org.apache.fop.render.java2d.Java2DPainter.java

/**
 * Special constructor for embedded use (when another painter uses Java2DPainter
 * to convert part of a document into a bitmap, for example).
 * @param g2d the target Graphics2D instance
 * @param context the IF context/* w  w  w  .j a  va  2 s .  co  m*/
 * @param fontInfo the font information
 * @param state the IF state object
 */
public Java2DPainter(Graphics2D g2d, IFContext context, FontInfo fontInfo, IFState state) {
    super();
    this.ifContext = context;
    if (state != null) {
        this.state = state.push();
    } else {
        this.state = IFState.create();
    }
    this.fontInfo = fontInfo;
    this.g2dState = new Java2DGraphicsState(g2d, fontInfo, g2d.getTransform());
    this.borderPainter = new Java2DBorderPainter(this);
}

From source file:org.apache.fop.svg.AbstractFOPTextPainter.java

/**
 * Paint a single text run on the Graphics2D at a given location.
 * @param run the text run to paint/*w w w  . j a  v  a 2 s .  c o m*/
 * @param g2d the Graphics2D to paint to
 * @param loc the current location of the "cursor"
 * @return the new location of the "cursor" after painting the text run
 */
protected Point2D paintTextRun(StrokingTextPainter.TextRun run, Graphics2D g2d, Point2D loc) {
    AttributedCharacterIterator aci = run.getACI();
    aci.first();

    updateLocationFromACI(aci, loc);
    AffineTransform at = g2d.getTransform();
    loc = at.transform(loc, null);

    // font
    Font font = getFont(aci);
    if (font != null) {
        nativeTextHandler.setOverrideFont(font);
    }

    // color
    TextPaintInfo tpi = (TextPaintInfo) aci
            .getAttribute(GVTAttributedCharacterIterator.TextAttribute.PAINT_INFO);
    if (tpi == null) {
        return loc;
    }
    Paint foreground = tpi.fillPaint;
    if (foreground instanceof Color) {
        Color col = (Color) foreground;
        g2d.setColor(col);
    }
    g2d.setPaint(foreground);

    // text anchor
    TextNode.Anchor anchor = (TextNode.Anchor) aci
            .getAttribute(GVTAttributedCharacterIterator.TextAttribute.ANCHOR_TYPE);

    // text
    String txt = getText(aci);
    float advance = getStringWidth(txt, font);
    float tx = 0;
    if (anchor != null) {
        switch (anchor.getType()) {
        case TextNode.Anchor.ANCHOR_MIDDLE:
            tx = -advance / 2;
            break;
        case TextNode.Anchor.ANCHOR_END:
            tx = -advance;
            break;
        default: //nop
        }
    }

    // draw string
    double x = loc.getX();
    double y = loc.getY();
    try {
        try {
            nativeTextHandler.drawString(g2d, txt, (float) x + tx, (float) y);
        } catch (IOException ioe) {
            if (g2d instanceof AFPGraphics2D) {
                ((AFPGraphics2D) g2d).handleIOException(ioe);
            }
        }
    } finally {
        nativeTextHandler.setOverrideFont(null);
    }
    loc.setLocation(loc.getX() + advance, loc.getY());
    return loc;
}

From source file:org.dwfa.ace.graph.AceGraphRenderer.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>.//from   www .  j  av a 2s.  c om
 * 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>.
 */
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);

    if (deviceRectangle == null) {
        edgeHit = false;
    } else {
        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);
    }
}