Example usage for java.awt Graphics drawLine

List of usage examples for java.awt Graphics drawLine

Introduction

In this page you can find the example usage for java.awt Graphics 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:com.aistor.common.servlet.ValidateCodeServlet.java

private void createBackground(Graphics g) {
    // /*from  ww w. j  a  v  a  2s  .  c o m*/
    g.setColor(getRandColor(220, 250));
    g.fillRect(0, 0, w, h);
    // ?
    for (int i = 0; i < 10; i++) {
        g.setColor(getRandColor(40, 150));
        Random random = new Random();
        int x = random.nextInt(w);
        int y = random.nextInt(h);
        int x1 = random.nextInt(w);
        int y1 = random.nextInt(h);
        g.drawLine(x, y, x1, y1);
    }
}

From source file:apm.common.servlet.ValidateCodeServlet.java

private void createBackground(Graphics g) {
    // //from  ww  w .j a v  a 2  s . co m
    g.setColor(getRandColor(220, 250));
    g.fillRect(0, 0, w, h);
    // ?
    for (int i = 0; i < 8; i++) {
        g.setColor(getRandColor(40, 150));
        Random random = new Random();
        int x = random.nextInt(w);
        int y = random.nextInt(h);
        int x1 = random.nextInt(w);
        int y1 = random.nextInt(h);
        g.drawLine(x, y, x1, y1);
    }
}

From source file:SineDraw.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int maxWidth = getWidth();
    double hstep = (double) maxWidth / (double) points;
    int maxHeight = getHeight();
    pts = new int[points];
    for (int i = 0; i < points; i++)
        pts[i] = (int) (sines[i] * maxHeight / 2 * .95 + maxHeight / 2);
    g.setColor(Color.RED);//from w  ww  .  ja  va 2s  .  c  om
    for (int i = 1; i < points; i++) {
        int x1 = (int) ((i - 1) * hstep);
        int x2 = (int) (i * hstep);
        int y1 = pts[i - 1];
        int y2 = pts[i];
        g.drawLine(x1, y1, x2, y2);
    }
}

From source file:uk.co.modularaudio.util.audio.gui.patternsequencer.PatternSequenceNoteGrid.java

private void paintGrid(final Graphics g, final int startCol, final int endCol, final int startRow,
        final int endRow) {
    g.setColor(foregroundColour);/*from  w w w .j  a v  a2  s. c  om*/
    for (int i = startCol; i <= endCol; i++) {
        final int lineStartY = startRow * cellDimensions.height;
        final int lineX = i * cellDimensions.width;
        final int lineEndY = endRow * cellDimensions.height;
        g.drawLine(lineX, lineStartY, lineX, lineEndY);
    }
    for (int j = startRow; j <= endRow; j++) {
        final int lineStartX = startCol * cellDimensions.width;
        final int lineY = j * cellDimensions.height;
        final int lineEndX = endCol * cellDimensions.width;
        g.drawLine(lineStartX, lineY, lineEndX, lineY);
    }
}

From source file:ColorSwatch.java

/**
 * Paints this Icon into the provided graphics context.
 *//*from  w  w  w .ja v  a 2s. c  o  m*/
public void paintIcon(Component c, Graphics g, int x, int y) {
    if (ourSwatchIsVoid)
        return;

    Color oldColor = g.getColor();

    if (ourSwatchIsMultiColor) {
        g.setColor(Color.white);
        g.fillRect(x, y, ourSwatchSize, ourSwatchSize);
        g.setColor(ourBorderColor);
        for (int i = 0; i < ourSwatchSize; i += 2) {
            g.drawLine(x + i, y, x + i, y + ourSwatchSize);
        }
    }

    else if (ourSwatchColor != null) {
        g.setColor(ourSwatchColor);
        g.fillRect(x, y, ourSwatchSize, ourSwatchSize);
    }

    else {
        g.setColor(Color.white);
        g.fillRect(x, y, ourSwatchSize, ourSwatchSize);
        g.setColor(ourBorderColor);
        g.drawLine(x, y, x + ourSwatchSize, y + ourSwatchSize);
        g.drawLine(x, y + ourSwatchSize, x + ourSwatchSize, y);
    }

    if (ourBorderPainted) {
        g.setColor(ourBorderColor);
        g.drawRect(x, y, ourSwatchSize, ourSwatchSize);
    }

    g.setColor(oldColor);
}

From source file:uk.co.modularaudio.mads.base.scopen.ui.display.ScopeWaveDisplay.java

private void paintGridLines(final Graphics g) {
    g.setColor(ScopeNColours.SCOPE_AXIS_DETAIL);

    // Draw the axis lines
    for (int i = 0; i < numAmpMarkers; ++i) {
        final int lineY = (vertPixelsPerMarker * i);
        g.drawLine(0, lineY, magsWidth - 1, lineY);
    }//from   w  w w .  j  a  va2  s.  c om

    for (int j = 0; j < numTimeMarkers; ++j) {
        final int lineX = horizPixelsPerMarker * j;
        g.drawLine(lineX, 0, lineX, magsHeight);
    }
}

From source file:jtrace.Scene.java

/**
 * Render scene overlayed with wire frame representation of objects.
 * //  www .j a  v a  2 s  .  c  om
 * @param width
 * @param height
 * @param maxRecursionDepth
 * @return 
 */
public BufferedImage renderWireFrame(int width, int height, int maxRecursionDepth) {
    BufferedImage image = render(width, height, maxRecursionDepth);
    Graphics gr = image.getGraphics();
    gr.setColor(java.awt.Color.cyan);

    // Object wireframes
    for (SceneObject object : sceneObjects) {
        for (Vector3D[] edge : object.getWireFrame()) {
            int[] coord1 = camera.getPixel(width, height, edge[0]);
            int[] coord2 = camera.getPixel(width, height, edge[1]);

            gr.drawLine(coord1[0], coord1[1], coord2[0], coord2[1]);
        }

        // Render object axes
        renderAxes(image, object);
    }

    // Render scene axes
    renderAxes(image, null);

    return image;
}

From source file:com.synnex.saas.platform.core.servlet.CaptchaServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {/*from ww  w.  jav  a2s  .c o m*/
        int width = 50;
        int height = 18;
        String captchaCode = RandomStringUtils.random(4, true, true);
        HttpSession session = request.getSession(true);
        session.setAttribute("captchaCode", captchaCode);

        response.setContentType("images/jpeg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        ServletOutputStream out = response.getOutputStream();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        Font mFont = new Font("Times New Roman", Font.BOLD, 18);
        g.setFont(mFont);
        g.setColor(getRandColor(160, 200));
        Random random = new Random();
        for (int i = 0; i < 155; i++) {
            int x2 = random.nextInt(width);
            int y2 = random.nextInt(height);
            int x3 = random.nextInt(12);
            int y3 = random.nextInt(12);
            g.drawLine(x2, y2, x2 + x3, y2 + y3);
        }
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(captchaCode, 2, 16);
        g.dispose();
        ImageIO.write((BufferedImage) image, "JPEG", out);
        out.close();
    } catch (Exception e) {
        logger.error("Generate captcha failed.", e);
    }
}

From source file:Main.java

public void paint(Graphics g) {

    int fontSize = 20;

    g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize));

    FontMetrics fm = g.getFontMetrics();

    String s = "www.java2s.com";

    int stringWidth = fm.stringWidth(s);

    int w = 200;/*from   w w  w.j  ava  2  s  .c  om*/
    int h = 200;

    int x = (w - stringWidth) / 2;
    int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2;
    int ascent = fm.getMaxAscent();
    int descent = fm.getMaxDecent();
    int fontHeight = fm.getMaxAscent() + fm.getMaxDecent();

    g.setColor(Color.white);
    g.fillRect(x, baseline - ascent, stringWidth, fontHeight);

    g.setColor(Color.gray);
    g.drawLine(x, baseline, x + stringWidth, baseline);

    g.setColor(Color.red);
    g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent);

    g.setColor(Color.blue);
    g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent);
    g.setColor(Color.black);
    g.drawString(s, x, baseline);

}

From source file:com.cburch.draw.shapes.Line.java

@Override
public void paint(Graphics g, HandleGesture gesture) {
    if (setForStroke(g)) {
        int x0 = this.x0;
        int y0 = this.y0;
        int x1 = this.x1;
        int y1 = this.y1;
        Handle h = gesture.getHandle();//  w w  w. j a  va2s  . c o m
        if (h.isAt(x0, y0)) {
            x0 += gesture.getDeltaX();
            y0 += gesture.getDeltaY();
        }
        if (h.isAt(x1, y1)) {
            x1 += gesture.getDeltaX();
            y1 += gesture.getDeltaY();
        }
        g.drawLine(x0, y0, x1, y1);
    }
}