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.squidy.designer.zoom.impl.PortShape.java

/**
 * @param paintContext//from w ww .  j  ava  2  s. c  o  m
 */
@Override
protected void paintShape(PPaintContext paintContext) {
    super.paintShape(paintContext);

    Graphics2D g = paintContext.getGraphics();

    PBounds bounds = getBoundsReference();

    int x = (int) bounds.getX();
    int y = (int) bounds.getY();
    int width = (int) bounds.getWidth();
    int height = (int) bounds.getHeight();

    g.setColor(innerColor);
    g.fillOval(x, y, width, width);

    Stroke defaultStroke = g.getStroke();

    g.setStroke(STROKE_PORT);
    g.setColor(Color.BLACK);
    g.drawOval(x, y, width, width);

    g.drawLine(x + 20, height - 20, x + width - 20, height - 20);
    g.drawLine(x + width - 50, height - 40, x + width - 20, height - 20);
    g.drawLine(x + width - 50, height + 0, x + width - 20, height - 20);

    g.setStroke(defaultStroke);

    // //////////////////

    if (isCreatingEdge) {
        Graphics2D g2d = paintContext.getGraphics();

        g2d.setColor(Color.GRAY);

        // // Paint label.
        // if (showLabel) {
        // paintLabel(paintContext);

        // Paint edge if is creating edge.
        if (isCreatingEdge) {
            paintCreatingEdge(paintContext);
        }
    }
}

From source file:costumetrade.common.verify.ImageVerification.java

public Pair<String, BufferedImage> create() {
    int x = 0, fontHeight = 0, codeY = 0;
    int red = 0, green = 0, blue = 0;

    x = width / (codeCount + 2);//?
    fontHeight = height - 2;//
    codeY = height - 4;/*from w  ww. j  a v a  2s .co  m*/

    // ?buffer
    BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = buffImg.createGraphics();
    // ?
    g.setColor(Color.WHITE);
    g.fillRect(0, 0, width, height);
    // 
    g.setFont(new Font("Arial", Font.PLAIN, fontHeight));

    for (int i = 0; i < lineCount; i++) {

        int xs = RandomUtils.nextInt(0, width);
        int ys = RandomUtils.nextInt(0, height);
        int xe = xs + RandomUtils.nextInt(0, width / 8);
        int ye = ys + RandomUtils.nextInt(0, height / 8);
        red = RandomUtils.nextInt(0, 255);
        green = RandomUtils.nextInt(0, 255);
        blue = RandomUtils.nextInt(0, 255);
        g.setColor(new Color(red, green, blue));
        g.drawLine(xs, ys, xe, ye);
    }

    // randomCode???
    StringBuilder randomCode = new StringBuilder();
    // ?codeCount??
    for (int i = 0; i < codeCount; i++) {
        String strRand = String.valueOf(codeSequence[RandomUtils.nextInt(0, codeSequence.length)]);
        // ????
        red = RandomUtils.nextInt(0, 255);
        green = RandomUtils.nextInt(0, 255);
        blue = RandomUtils.nextInt(0, 255);
        g.setColor(new Color(red, green, blue));
        g.drawString(strRand, (i + 1) * x, codeY);
        // ??
        randomCode.append(strRand);
    }
    // ????Session
    return new Pair<String, BufferedImage>(randomCode.toString(), buffImg);
}

From source file:org.knime.knip.core.ui.imgviewer.panels.HistogramBC.java

private XYAnnotation slopeLine() {
    return new XYAnnotation() {

        private double x1, y1, x2, y2;

        @Override/*from   w  ww . j  a  v  a2s .c  om*/
        public void removeChangeListener(final AnnotationChangeListener listener) {
            // ignore
        }

        @Override
        public void addChangeListener(final AnnotationChangeListener listener) {
            // ignore
        }

        @Override
        public void draw(final Graphics2D g2, final XYPlot plot, final Rectangle2D dataArea,
                final ValueAxis domainAxis, final ValueAxis rangeAxis, final int rendererIndex,
                final PlotRenderingInfo info) {
            calcLineCoords(dataArea);
            drawLine(g2);
        }

        private void drawLine(final Graphics2D g2) {
            final Color origColor = g2.getColor();
            g2.setColor(Color.black);
            g2.drawLine((int) x1, (int) y1, (int) x2, (int) y2);
            g2.setColor(Color.lightGray);
            g2.drawLine((int) x1, 0, (int) x1, 192);
            g2.drawLine((int) x2, 0, (int) x2, 192);
            g2.setColor(origColor);
        }

        @SuppressWarnings("synthetic-access")
        private void calcLineCoords(final Rectangle2D rect) {
            // offset necessary since chart is not drawn on whole rectangle
            int offset = 12;
            final double x = rect.getMinX() + offset;
            final double y = rect.getMinY();
            final double w = rect.getWidth() - 2 * offset;
            final double h = rect.getHeight();
            final double min = bundle.getTheoreticalMin();
            final double max = bundle.getTheoreticalMax();
            final double defaultMin = bundle.getDataMin();
            final double defaultMax = bundle.getDataMax();
            final double scale = w / (defaultMax - defaultMin);
            double slope = 0.0;
            if (max != min) {
                slope = h / (max - min);
            }
            if (min >= defaultMin) {
                x1 = scale * (min - defaultMin);
                y1 = h;
            } else {
                x1 = 0;
                if (max > min) {
                    y1 = h - ((defaultMin - min) * slope);
                } else {
                    y1 = h;
                }
            }
            if (max <= defaultMax) {
                x2 = (scale * (max - defaultMin));
                y2 = 0;
            } else {
                x2 = w;
                if (max > min) {
                    y2 = h - ((defaultMax - min) * slope);
                } else {
                    y2 = 0;
                }
            }
            x1 += x;
            x2 += x;
            y1 += y;
            y2 += y;
        }
    };
}

From source file:savant.view.tracks.TrackRenderer.java

/**
 * Simplest kind of legend is just a list of coloured lines with names next to them.
 *///from w  ww. j ava  2  s  .  com
protected void drawSimpleLegend(Graphics2D g2, int x, int y, ColourKey... keys) {
    ColourScheme cs = (ColourScheme) instructions.get(DrawingInstruction.COLOUR_SCHEME);

    g2.setFont(LEGEND_FONT);

    for (ColourKey k : keys) {
        String legendString = k.getName();
        g2.setColor(cs.getColor(k));
        g2.setStroke(TWO_STROKE);
        Rectangle2D stringRect = LEGEND_FONT.getStringBounds(legendString, g2.getFontRenderContext());
        g2.drawLine(x - 25, y - (int) stringRect.getHeight() / 2, x - 5, y - (int) stringRect.getHeight() / 2);
        g2.setColor(cs.getColor(ColourKey.INTERVAL_LINE));
        g2.setStroke(ONE_STROKE);
        g2.drawString(legendString, x, y);

        y += LEGEND_LINE_HEIGHT;
    }
}

From source file:uk.co.modularaudio.mads.base.waveroller.ui.WaveRollerBufferSampleFactory.java

private void fillInMinMaxLine(final Graphics2D g, final int pixelX, final float minValue,
        final float maxValue) {
    final float multiplier = (displayBounds.height / 2.0f);

    g.setColor(WaveRollerColours.DISPLAY_VALUE_COLOUR);
    int yMinVal = (int) (-minValue * multiplier * maxDbScaleMultiplier);
    int yMaxVal = (int) (-maxValue * multiplier * maxDbScaleMultiplier);

    yMinVal = (int) ((yMinVal * valueScaleForMargins) + multiplier);
    yMaxVal = (int) ((yMaxVal * valueScaleForMargins) + multiplier);
    g.drawLine(pixelX, yMinVal, pixelX, yMaxVal);
}

From source file:com.joliciel.jochre.graphics.VectorizerImpl.java

public BufferedImage drawArrayLists(JochreImage jochreImage) {
    long startTime = (new Date()).getTime();
    BufferedImage vectorizedImage = new BufferedImage(jochreImage.getWidth(), jochreImage.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics2D = vectorizedImage.createGraphics();
    graphics2D.setStroke(new BasicStroke(1));
    graphics2D.setPaint(Color.BLACK);

    for (Paragraph paragraph : jochreImage.getParagraphs()) {
        for (RowOfShapes row : paragraph.getRows()) {
            for (GroupOfShapes group : row.getGroups()) {
                for (Shape shape : group.getShapes()) {
                    List<LineSegment> lines = this.vectorize(shape);

                    for (LineSegment line : lines)
                        graphics2D.drawLine(shape.getLeft() + line.getStartX(),
                                shape.getTop() + line.getStartY(), shape.getLeft() + line.getEndX(),
                                shape.getTop() + line.getEndY());
                }//www.  j  ava 2 s. co m
            }
        }
    }

    if (LOG.isDebugEnabled()) {
        long endTime = (new Date()).getTime();
        long diff = endTime - startTime;
        LOG.debug("Time elapsed: " + ((double) diff / 1000));
    }
    return vectorizedImage;
}

From source file:net.team2xh.crt.gui.editor.EditorTextPane.java

/**
 * Paints the background, the margin background and the margin line.
 *
 * @param g/*from   ww w .j  a  v  a2 s .  c o  m*/
 */
@Override
public void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    int m = marginSize;
    int w = getWidth(), h = getHeight();
    // Background
    g2d.setPaint(Theme.getTheme().COLOR_02);
    g2d.fillRect(0, 0, m, h);
    // Margin background
    if (m < w) {
        g2d.setColor(Theme.getTheme().COLOR_01);
        g2d.fillRect(m, 0, w - m, h);
    }
    // Margin line
    g2d.setColor(Theme.getTheme().COLOR_04);
    g2d.drawLine(m, 0, m, h);
    // Draw the rest
    super.paintComponent(g);
}

From source file:de.fhg.igd.iva.explorer.main.JStatBar.java

@Override
protected void paintComponent(Graphics g1) {
    super.paintComponent(g1);

    Graphics2D g = (Graphics2D) g1;

    int width = getWidth() - 1;
    int height = getHeight() - 1;

    int whiskerSize = 6;

    // fill background rect
    g.setColor(Color.WHITE);//ww  w  .j a v  a  2 s.c o m
    g.fillRect(insetX, insetY, width - 2 * insetX, height - 2 * insetY);

    int q10X = mapXToScreen(stats.getPercentile(10.0), width);
    int q25X = mapXToScreen(stats.getPercentile(25.0), width);
    int q50X = mapXToScreen(stats.getPercentile(50.0), width);
    int q75X = mapXToScreen(stats.getPercentile(75.0), width);
    int q90X = mapXToScreen(stats.getPercentile(90.0), width);

    g.setColor(Color.PINK);
    int leftX = Math.min(q25X, q75X);
    int rightX = Math.max(q25X, q75X);

    g.fillRect(insetX + leftX, insetY + 1, rightX - leftX, height - 2 * insetY - 1);
    g.drawLine(insetX + q10X, height / 2, insetX + q90X, height / 2);
    g.drawLine(insetX + q10X, (height - whiskerSize) / 2, insetX + q10X, (height + whiskerSize) / 2);
    g.drawLine(insetX + q10X, height / 2, insetX + q90X, height / 2);
    g.drawLine(insetX + q90X, (height - whiskerSize) / 2, insetX + q90X, (height + whiskerSize) / 2);

    g.setColor(Color.RED);
    g.drawLine(insetX + q50X, insetY + 1, insetX + q50X, height - insetY - 1);

    // draw outline border rect
    g.setColor(Color.BLACK);
    g.drawRect(insetX, insetY, width - 2 * insetX, height - 2 * insetY);

    int buttonX = mapXToScreen(quality, width);
    drawButton(g, buttonX, height);
}

From source file:com.us.servlet.AuthCode.java

protected void service(HttpServletRequest request, HttpServletResponse response) {
    final CodeAuth bean = AppHelper.CODE_AUTH;
    int width = NumberUtils.toInt(request.getParameter("width"), bean.getWidth());
    int height = NumberUtils.toInt(request.getParameter("height"), bean.getHeight());
    int x = width / (bean.getLength() + 1);
    int codeY = height - 4;
    int fontHeight = height - 2;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D graphics = image.createGraphics();
    graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    if (StringUtil.hasText(request.getParameter("bgcolor"))) {
        graphics.setBackground(ColorHelper.hex2RGB(request.getParameter("bgcolor")));
    }//  w  w  w.  java 2s  .  co m
    graphics.fillRect(0, 0, width, height);
    graphics.setFont(new Font(bean.getFont(), Font.BOLD, fontHeight));
    graphics.drawRect(0, 0, width - 1, height - 1);
    // 
    if (bean.isBreakLine()) {
        for (int i = 0; i < 15; i++) {
            int x1 = RandomUtils.nextInt(width);
            int y1 = RandomUtils.nextInt(height);
            int x2 = RandomUtils.nextInt(12);
            int y2 = RandomUtils.nextInt(12);
            graphics.drawLine(x1, y1, x + x2, y1 + y2);
        }
    }
    char[] CHARSET_AREA = null;
    if (bean.getType().charAt(0) == '1') {
        CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, BIG_LETTERS);
    }
    if (bean.getType().charAt(1) == '1') {
        CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, SMALL_LETTER);
    }
    if (bean.getType().charAt(2) == '1') {
        CHARSET_AREA = ArrayUtils.addAll(CHARSET_AREA, NUMBERS);
    }
    StringBuilder randomCode = new StringBuilder();
    for (int i = 0; i < bean.getLength(); i++) {
        String rand = String.valueOf(CHARSET_AREA[RandomUtils.nextInt(CHARSET_AREA.length)]);
        graphics.setColor(ColorHelper.color(RandomUtils.nextInt(255), RandomUtils.nextInt(255),
                RandomUtils.nextInt(255)));
        graphics.drawString(rand, (i + 1) * x, codeY);
        randomCode.append(rand);
    }
    HttpSession session = request.getSession();
    session.setAttribute(bean.getSessionKey(), randomCode.toString());
    // ?
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setContentType("image/png");
    try {
        // Servlet?
        ServletOutputStream sos = response.getOutputStream();
        ImageIO.write(image, "png", sos);
        sos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.squidy.designer.zoom.ContainerShape.java

@Override
protected void paintShapeZoomedIn(PPaintContext paintContext) {
    super.paintShapeZoomedIn(paintContext);

    if (gridVisible) {
        Graphics2D g = paintContext.getGraphics();
        PBounds bounds = getBoundsReference();
        double width = bounds.getWidth();
        double height = bounds.getHeight();

        g.setColor(ALPHA_GRAY_COLOR);/*from   w  w w  .jav a 2  s.  co m*/
        double widthStep = width / (double) GRIDS;
        for (int i = 1; i <= GRIDS; i++) {
            g.setStroke((i % GRID_SPACING == 0) ? GRID_STROKE_THICK : GRID_STROKE_THIN);
            g.drawLine((int) widthStep * i, 0, (int) widthStep * i, (int) height);
        }
        double heightStep = height / (double) GRIDS;
        for (int i = 1; i < GRIDS - 1; i++) {
            g.setStroke((i % GRID_SPACING == 0) ? GRID_STROKE_THICK : GRID_STROKE_THIN);
            g.drawLine(0, (int) heightStep * i, (int) width, (int) heightStep * i);
        }
    }
}