Example usage for java.awt Graphics2D drawLine

List of usage examples for java.awt Graphics2D drawLine

Introduction

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

Prototype

public abstract void drawLine(int x1, int y1, int x2, int y2);

Source Link

Document

Draws a line, using the current color, between the points (x1, y1) and (x2, y2) in this graphics context's coordinate system.

Usage

From source file:org.broad.igv.variant.VariantTrack.java

private void drawLineIfVisible(Graphics2D g2D, Rectangle visibleRectangle, Color color, int yLoc, int left,
        int right) {
    if (yLoc >= visibleRectangle.y && yLoc <= visibleRectangle.getMaxY()) {
        if (color != null)
            g2D.setColor(color);/*w ww  .  ja  v  a2 s.  c om*/
        g2D.drawLine(left, yLoc, right, yLoc);
    }
}

From source file:org.executequery.gui.erd.ErdTable.java

protected void drawTable(Graphics2D g, int offsetX, int offsetY) {

    if (parent == null) {

        return;/* w  w  w.j  a  va 2s  .com*/
    }

    Font tableNameFont = parent.getTableNameFont();
    Font columnNameFont = parent.getColumnNameFont();

    // set the table value background
    g.setColor(TITLE_BAR_BG_COLOR);
    g.fillRect(offsetX, offsetY, FINAL_WIDTH - 1, TITLE_BAR_HEIGHT);

    // set the table value
    FontMetrics fm = g.getFontMetrics(tableNameFont);
    int lineHeight = fm.getHeight();
    int titleXPosn = (FINAL_WIDTH / 2) - (fm.stringWidth(tableName) / 2) + offsetX;

    g.setColor(Color.BLACK);
    g.setFont(tableNameFont);
    g.drawString(tableName, titleXPosn, lineHeight + offsetY);

    // draw the line separator
    lineHeight = TITLE_BAR_HEIGHT + offsetY - 1;
    g.drawLine(offsetX, lineHeight, offsetX + FINAL_WIDTH - 1, lineHeight);

    // fill the white background
    g.setColor(tableBackground);
    g.fillRect(offsetX, TITLE_BAR_HEIGHT + offsetY, FINAL_WIDTH - 1, FINAL_HEIGHT - TITLE_BAR_HEIGHT - 1);

    // add the column names
    fm = g.getFontMetrics(columnNameFont);
    int heightPlusSep = 1 + TITLE_BAR_HEIGHT + offsetY;
    int leftMargin = 5 + offsetX;

    lineHeight = fm.getHeight();
    g.setColor(Color.BLACK);
    g.setFont(columnNameFont);

    int drawCount = 0;
    String value = null;
    if (ArrayUtils.isNotEmpty(columns)) {

        for (int i = 0; i < columns.length; i++) {
            ColumnData column = columns[i];
            if (displayReferencedKeysOnly && !column.isKey()) {
                continue;
            }

            int y = (((drawCount++) + 1) * lineHeight) + heightPlusSep;
            int x = leftMargin;

            // draw the column value string
            value = column.getColumnName();
            g.drawString(value, x, y);

            // draw the data type and size string
            x = leftMargin + dataTypeOffset;
            value = column.getFormattedDataType();
            g.drawString(value, x, y);

            // draw the key label
            if (column.isKey()) {

                if (column.isPrimaryKey() && column.isForeignKey()) {

                    value = PRIMARY + FOREIGN;

                } else if (column.isPrimaryKey()) {

                    value = PRIMARY;

                } else if (column.isForeignKey()) {

                    value = FOREIGN;
                }

                x = leftMargin + dataTypeOffset + keyLabelOffset;
                g.drawString(value, x, y);
            }

        }

    }

    // draw the rectangle border
    double scale = g.getTransform().getScaleX();

    if (selected && scale != ErdPrintable.PRINT_SCALE) {
        g.setStroke(focusBorderStroke);
        g.setColor(Color.BLUE);
    } else {
        g.setColor(Color.BLACK);
    }

    g.drawRect(offsetX, offsetY, FINAL_WIDTH - 1, FINAL_HEIGHT - 1);
    //    g.setColor(Color.DARK_GRAY);
    //    g.draw3DRect(offsetX, offsetY, FINAL_WIDTH - 2, FINAL_HEIGHT - 2, true);
}

From source file:edworld.pdfreader4humans.PDFReader.java

private void draw(Component component, Graphics2D graphics, Color inkColor, Color backgroundColor,
        boolean showStructure, Map<String, Font> fonts) {
    for (Component child : component.getChildren())
        draw(child, graphics, inkColor, backgroundColor, showStructure, fonts);
    if (component instanceof BoxComponent && showStructure) {
        graphics.setColor(boxColor(backgroundColor));
        graphics.drawRect((int) component.getFromX(), (int) component.getFromY(), (int) component.getWidth(),
                (int) component.getHeight());
        graphics.setColor(inkColor);//from www  .j  av  a2 s .c o  m
    } else if (component instanceof GroupComponent && showStructure) {
        graphics.setColor(groupColor(backgroundColor));
        graphics.drawRect(Math.round(component.getFromX()), Math.round(component.getFromY()),
                Math.round(component.getWidth()), Math.round(component.getHeight()));
        graphics.setColor(inkColor);
    } else if (component instanceof MarginComponent && showStructure) {
        graphics.setColor(marginColor(backgroundColor));
        graphics.drawRect(Math.round(component.getFromX()), Math.round(component.getFromY()),
                Math.round(component.getWidth()), Math.round(component.getHeight()));
        graphics.setColor(inkColor);
    } else if (component.getType().equals("line"))
        graphics.drawLine(Math.round(component.getFromX()), Math.round(component.getFromY()),
                Math.round(component.getToX()), Math.round(component.getToY()));
    else if (component.getType().equals("rect"))
        graphics.drawRect(Math.round(component.getFromX()), Math.round(component.getFromY()),
                Math.round(component.getWidth()), Math.round(component.getHeight()));
    else if (component instanceof TextComponent) {
        graphics.setFont(font((TextComponent) component, fonts));
        graphics.drawString(((TextComponent) component).getText(), component.getFromX(), component.getToY());
    }
}

From source file:peakml.util.jfreechart.FastSpectrumPlot.java

@Override
public void draw(Graphics2D g2, Rectangle2D area, Point2D anchor, PlotState parentState,
        PlotRenderingInfo info) {/*from   www  .  j a  va2 s.c  o  m*/
    // add the plot area to the info (used amongst other by the axis for zooming)
    if (info != null)
        info.setPlotArea(area);

    // add the insets (if any)
    RectangleInsets insets = getInsets();
    insets.trim(area);

    // draw the axis and add the dataArea to the info (used amongst other by the axis for zooming)
    AxisSpace space = new AxisSpace();
    space = xaxis.reserveSpace(g2, this, area, RectangleEdge.BOTTOM, space);
    space = yaxis.reserveSpace(g2, this, area, RectangleEdge.LEFT, space);

    Rectangle2D dataArea = space.shrink(area, null);
    if (info != null)
        info.setDataArea(dataArea);

    // flood fill the whole area with the background color
    drawBackground(g2, dataArea);

    // draw the axis
    xaxis.draw(g2, dataArea.getMaxY(), area, dataArea, RectangleEdge.BOTTOM, info);
    yaxis.draw(g2, dataArea.getMinX(), area, dataArea, RectangleEdge.LEFT, info);

    // sanity check
    if (dataseries.size() == 0)
        return;

    // clip the draw area
    Shape originalclip = g2.getClip();
    g2.clip(dataArea);

    // draw all the values
    for (Data data : dataseries) {
        int xpos = (int) xaxis.valueToJava2D(data.mass, dataArea, RectangleEdge.BOTTOM);
        int ypos = (int) yaxis.valueToJava2D(data.intensity, dataArea, RectangleEdge.LEFT);
        g2.drawLine(xpos, (int) yaxis.valueToJava2D(0, dataArea, RectangleEdge.LEFT), xpos, ypos);

        // draw the label
        if (data.description != null && data.description.length() != 0) {
            g2.setColor(Color.RED);
            g2.drawLine(xpos + 2, ypos - 2, xpos + 15, ypos - 15);
            g2.setColor(Color.BLACK);
            g2.drawString(data.description, xpos + 17, ypos - 17);
        }
    }

    // reset
    g2.setClip(originalclip);
}

From source file:org.tsho.dmc2.core.chart.CowebRenderer.java

public void render(final Graphics2D g2, final Rectangle2D dataArea, final PlotRenderingInfo info) {

    state = STATE_RUNNING;/*from w  w  w  .j  av  a2 s .  co  m*/

    if (plot.isAlpha()) {
        g2.setComposite(AlphaComposite.SrcOver);
    }

    Stepper.Point2D result = stepper.getCurrentPoint2D();

    int transX, transY;

    double start = (int) dataArea.getMinX();
    double end = (int) dataArea.getMaxX();

    double[] value = new double[1];

    int prevY = 0;
    boolean flagOld = false;
    boolean flagNew = false;

    label: for (double i = start; i <= end; i += 1) {
        value[0] = this.domainAxis.java2DToValue(i, dataArea, RectangleEdge.BOTTOM);

        stepper.setInitialValue(value);
        stepper.initialize();

        for (int j = 0; j < power; j++) {
            stepper.step();
        }

        result = stepper.getCurrentPoint2D();

        transX = (int) i;
        transY = (int) rangeAxis.valueToJava2D(result.getX(), dataArea, RectangleEdge.LEFT);
        flagNew = Double.isNaN(result.getX());

        if (bigDots) {
            g2.fillRect(transX - 1, transY - 1, 3, 3);
        } else {
            g2.fillRect(transX, transY, 1, 1);
        }

        if (connectWithLines) {
            if (i > start) {
                if (!flagOld && !flagNew)
                    g2.drawLine(transX, transY, transX - 1, prevY);
            }

            prevY = transY;
            flagOld = flagNew;
        }

        if (stopped) {
            state = STATE_STOPPED;
            return;
        }

    }

    if (animate) {
        animateCowebPlot(g2, dataArea);
    }

    state = STATE_FINISHED;
}

From source file:org.cruk.mga.CreateReport.java

/**
 * Draws the x-axis for the number of sequences and the legend.
 *
 * @param g2//from w  w  w  . j a  v  a 2  s.  c  o m
 * @param x0
 * @param y
 * @param tickIntervals
 * @param maxSequenceCount
 * @return
 */
private int drawAxisAndLegend(Graphics2D g2, int x0, int y, int tickIntervals, long maxSequenceCount) {
    g2.setColor(Color.BLACK);
    g2.setFont(axisFont);

    boolean millions = maxSequenceCount / tickIntervals >= 1000000;
    long largestTickValue = maxSequenceCount;
    if (millions)
        largestTickValue /= 1000000;
    int w = g2.getFontMetrics().stringWidth(Long.toString(largestTickValue));
    int x1 = plotWidth - (w / 2) - gapSize;
    g2.drawLine(x0, y, x1, y);

    int tickFontHeight = g2.getFontMetrics().getAscent();
    int tickHeight = tickFontHeight / 2;
    for (int i = 0; i <= tickIntervals; i++) {
        int x = x0 + i * (x1 - x0) / tickIntervals;
        g2.drawLine(x, y, x, y + tickHeight);
        long tickValue = i * maxSequenceCount / tickIntervals;
        if (millions)
            tickValue /= 1000000;
        String s = Long.toString(tickValue);
        int xs = x - g2.getFontMetrics().stringWidth(s) / 2 + 1;
        int ys = y + tickHeight + tickFontHeight + 1;
        g2.drawString(s, xs, ys);
    }

    g2.setFont(font);
    int fontHeight = g2.getFontMetrics().getAscent();
    String s = "Number of sequences";
    if (millions)
        s += " (millions)";
    int xs = x0 + (x1 - x0 - g2.getFontMetrics().stringWidth(s)) / 2;
    int ys = y + tickHeight + tickFontHeight + fontHeight + fontHeight / 3;
    g2.drawString(s, xs, ys);

    int yl = ys + fontHeight * 2;
    int xl = x0;

    int barHeight = (int) (fontHeight * 0.7f);
    int barWidth = 3 * barHeight;
    int yb = yl + (int) (fontHeight * 0.3f);
    int gap = (int) (fontHeight * 0.4f);

    g2.setColor(Color.GREEN);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    g2.setFont(axisFont);
    String label = "Sequenced species/genome";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(Color.ORANGE);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Control";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(Color.RED);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Contaminant";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(ADAPTER_COLOR);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Adapter";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Unmapped";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);
    xl += g2.getFontMetrics().stringWidth(label) + gap * 3;

    g2.setColor(Color.GRAY);
    g2.fillRect(xl, yb, barWidth, barHeight);
    g2.setColor(Color.BLACK);
    g2.drawRect(xl, yb, barWidth, barHeight);
    label = "Unknown";
    xl += barWidth + gap;
    g2.drawString(label, xl, yl + fontHeight);

    return x1;
}

From source file:com.att.aro.diagnostics.GraphPanel.java

/**
 * Creating DchTail and FachTail Cross Hatch
 * /* w  ww. j  a v a2s.  c om*/
 * @return Paint The Tail state paint object
 */
private static Paint getTailPaint(Color color) {

    BufferedImage bufferedImage = new BufferedImage(5, 5, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = bufferedImage.createGraphics();
    g2.setColor(Color.white);
    g2.fillRect(0, 0, 5, 5);
    g2.setColor(color);
    g2.drawLine(0, 0, 5, 5);
    g2.drawLine(5, 5, 0, 0);
    g2.drawLine(0, 5, 5, 0);
    Rectangle2D rect = new Rectangle2D.Double(0, 0, 5, 5);
    return new TexturePaint(bufferedImage, rect);
}

From source file:org.deegree.services.wps.provider.jrxml.contentprovider.map.MapContentProvider.java

private void prepareScaleBar(Map<String, Object> params, String scalebarKey, XMLAdapter jrxmlAdapter,
        List<OrderedDatasource<?>> datasources, String type, Envelope bbox, double mapWidth)
        throws ProcessletException {

    OMElement sbRep = jrxmlAdapter.getElement(jrxmlAdapter.getRootElement(), new XPath(
            ".//jasper:image[jasper:imageExpression/text()='$P{" + scalebarKey + "}']/jasper:reportElement",
            nsContext));/*  ww  w .  j  av a 2 s  .c  o  m*/

    if (sbRep != null) {
        // TODO: rework this!
        LOG.debug("Found scalebar with key '" + scalebarKey + "'.");
        int w = jrxmlAdapter.getRequiredNodeAsInteger(sbRep, new XPath("@width", nsContext));
        int h = jrxmlAdapter.getRequiredNodeAsInteger(sbRep, new XPath("@height", nsContext));
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = img.createGraphics();

        String fontName = null;
        int fontSize = 8;
        int desiredWidth = w - 30;
        // calculate scale bar max scale and size
        int length = 0;
        double lx = 0;
        double scale = 0;
        for (int i = 0; i < 100; i++) {
            double k = 0;
            double dec = 30 * Math.pow(10, i);
            for (int j = 0; j < 9; j++) {
                k += dec;
                double tx = -k * (mapWidth / bbox.getSpan0());
                if (Math.abs(tx - lx) < desiredWidth) {
                    length = (int) Math.round(Math.abs(tx - lx));
                    scale = k;
                } else {
                    break;
                }
            }
        }
        // draw scale bar base line
        g.setStroke(new BasicStroke((desiredWidth + 30) / 250));
        g.setColor(Color.black);
        g.drawLine(10, 30, length + 10, 30);
        double dx = length / 3d;
        double vdx = scale / 3;
        double div = 1;
        String uom = "m";
        if (scale > 1000) {
            div = 1000;
            uom = "km";
        }
        // draw scale bar scales
        if (fontName == null) {
            fontName = "SANS SERIF";
        }
        g.setFont(new Font(fontName, Font.PLAIN, fontSize));
        DecimalFormat df = new DecimalFormat("##.# ");
        DecimalFormat dfWithUom = new DecimalFormat("##.# " + uom);
        for (int i = 0; i < 4; i++) {
            String label = i < 3 ? df.format((vdx * i) / div) : dfWithUom.format((vdx * i) / div);
            g.drawString(label, (int) Math.round(10 + i * dx) - 8, 10);
            g.drawLine((int) Math.round(10 + i * dx), 30, (int) Math.round(10 + i * dx), 20);
        }
        for (int i = 0; i < 7; i++) {
            g.drawLine((int) Math.round(10 + i * dx / 2d), 30, (int) Math.round(10 + i * dx / 2d), 25);
        }
        g.dispose();
        params.put(scalebarKey, convertImageToReportFormat(type, img));
        LOG.debug("added scalebar");
    }
}

From source file:org.openaltimeter.desktopapp.annotations.XYVarioAnnotation.java

@Override
public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis,
        int rendererIndex, PlotRenderingInfo info) {
    // draw line/*from  www  . j a  v  a 2  s  . c  om*/
    super.draw(g2, plot, dataArea, domainAxis, rangeAxis, rendererIndex, info);

    // draw text
    PlotOrientation orientation = plot.getOrientation();
    RectangleEdge domainEdge = Plot.resolveDomainAxisLocation(plot.getDomainAxisLocation(), orientation);
    RectangleEdge rangeEdge = Plot.resolveRangeAxisLocation(plot.getRangeAxisLocation(), orientation);

    float anchorX = (float) domainAxis.valueToJava2D((x1 + x2) / 2, dataArea, domainEdge);
    float anchorY = (float) rangeAxis.valueToJava2D((y1 + y2) / 2, dataArea, rangeEdge);

    if (orientation == PlotOrientation.HORIZONTAL) {
        float tempAnchor = anchorX;
        anchorX = anchorY;
        anchorY = tempAnchor;
    }

    g2.setFont(getFont());
    g2.setPaint(getPaint());

    TextUtilities.drawRotatedString(getText(), g2, anchorX + this.offset, anchorY - OFFSET_SIZE,
            getTextAnchor(), getRotationAngle(), getRotationAnchor());

    g2.setPaint(Color.GRAY);
    int jx1 = (int) domainAxis.valueToJava2D(x1, dataArea, domainEdge);
    int jx2 = (int) domainAxis.valueToJava2D(x2, dataArea, domainEdge);
    int jy1 = (int) rangeAxis.valueToJava2D(y1, dataArea, rangeEdge);
    int jy2 = (int) rangeAxis.valueToJava2D(y2, dataArea, rangeEdge);

    TextUtilities.drawRotatedString(getText2(), g2, (jx1 + jx2) / 2, jy1 + LIMB_TEXT_OFFSET, getTextAnchor(),
            getRotationAngle(), getRotationAnchor());

    g2.drawLine(jx1, jy1, jx2, jy1);
    g2.drawLine(jx2, jy1, jx2, jy2);

}

From source file:ucar.unidata.idv.control.chart.WayPoint.java

/**
 * Draws the annotation.// w  ww.  jav a  2 s .  c o  m
 *
 * @param g2  the graphics device.
 * @param plot  the plot.
 * @param dataArea  the data area.
 * @param domainAxis  the domain axis.
 * @param rangeAxis  the range axis.
 * @param rendererIndex  the renderer index.
 * @param info  an optional info object that will be populated with
 *              entity information.
 */
public void draw(Graphics2D g2, XYPlot plot, Rectangle2D dataArea, ValueAxis domainAxis, ValueAxis rangeAxis,
        int rendererIndex, PlotRenderingInfo info) {
    super.setGraphicsState(g2);
    if (!getPlotWrapper().okToDraw(this)) {
        return;
    }

    g2.setStroke(new BasicStroke());
    if (false && getSelected()) {
        g2.setColor(COLOR_SELECTED);
    } else {
        g2.setColor(getColor());
    }
    x = getXFromValue(dataArea, domainAxis);

    int width2 = (int) (ANNOTATION_WIDTH / 2);
    int bottom = (int) (dataArea.getY() + dataArea.getHeight());
    y = bottom;
    int[] xs = { x - width2, x + width2, x, x - width2 };
    int[] ys = { bottom - ANNOTATION_WIDTH, bottom - ANNOTATION_WIDTH, bottom, bottom - ANNOTATION_WIDTH };
    g2.fillPolygon(xs, ys, xs.length);

    if ((getName() != null) && !isForAnimation) {
        FontMetrics fm = g2.getFontMetrics();
        int width = fm.stringWidth(getName());
        int textLeft = x - width / 2;
        g2.drawString(getName(), textLeft, bottom - ANNOTATION_WIDTH - 2);
    }

    if (getSelected()) {
        g2.setColor(COLOR_SELECTED);
        g2.drawPolygon(xs, ys, xs.length);
    }

    if (getPropertyListeners().hasListeners(PROP_WAYPOINTVALUE) || isForAnimation) {
        g2.setColor(Color.gray);
        g2.drawLine(x, y - ANNOTATION_WIDTH, x, (int) dataArea.getY());
    }

    boolean playSound = canPlaySound();

    if (isForAnimation) {
        if (clockImage == null) {
            clockImage = GuiUtils.getImage("/auxdata/ui/icons/clock.gif");
        }
        if (playSound) {
            g2.drawImage(clockImage, x - 8, (int) dataArea.getY() + 1, null);
        } else {
            g2.drawImage(clockImage, x - 8, (int) dataArea.getY() + 1, null);
        }
    }

    if (canPlaySound()) {
        if (noteImage == null) {
            noteImage = GuiUtils.getImage("/auxdata/ui/icons/note.gif");
        }
        if (isForAnimation) {
            g2.drawImage(noteImage, x + 8, (int) dataArea.getY() + 1, null);
        } else {
            g2.drawImage(noteImage, x, (int) dataArea.getY() + 1, null);
        }
    }

    if (minutesSpan > 0.0) {
        int left = (int) domainAxis.valueToJava2D(domainValue - (minutesSpan * 60000) / 2, dataArea,
                RectangleEdge.BOTTOM);
        int right = (int) domainAxis.valueToJava2D(domainValue + (minutesSpan * 60000) / 2, dataArea,
                RectangleEdge.BOTTOM);
        g2.setPaint(Color.black);
        g2.setStroke(new BasicStroke(2.0f));
        g2.drawLine(left, y, right, y);
    }

}