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:MainClass.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  www.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:SortItem.java

/**
 * Paint the array of numbers as a list of horizontal lines of varying
 * lenghts./*from  ww  w  .  ja v a2  s  . c o m*/
 */
public void paint(Graphics g) {
    int[] a = arr;
    int y = size().height - 1;

    // Erase old lines
    g.setColor(Color.lightGray);
    for (int i = a.length; --i >= 0; y -= 2) {
        g.drawLine(arr[i], y, size().width, y);
    }

    // Draw new lines
    g.setColor(Color.black);
    y = size().height - 1;
    for (int i = a.length; --i >= 0; y -= 2) {
        g.drawLine(0, y, arr[i], y);
    }

    if (h1 >= 0) {
        g.setColor(Color.red);
        y = h1 * 2 + 1;
        g.drawLine(0, y, size().width, y);
    }
    if (h2 >= 0) {
        g.setColor(Color.blue);
        y = h2 * 2 + 1;
        g.drawLine(0, y, size().width, y);
    }
}

From source file:de.tor.tribes.ui.views.DSWorkbenchRankFrame.java

@Override
public void actionPerformed(ActionEvent e) {
    RankTableTab activeTab = getActiveTab();
    if (e.getActionCommand() != null && activeTab != null) {
        if (e.getActionCommand().equals("Find")) {
            BufferedImage back = ImageUtils.createCompatibleBufferedImage(3, 3, BufferedImage.TRANSLUCENT);
            Graphics g = back.getGraphics();
            g.setColor(new Color(120, 120, 120, 120));
            g.fillRect(0, 0, back.getWidth(), back.getHeight());
            g.setColor(new Color(120, 120, 120));
            g.drawLine(0, 0, 3, 3);
            g.dispose();/*from w  w  w.jav  a2 s  . c o m*/
            TexturePaint paint = new TexturePaint(back,
                    new Rectangle2D.Double(0, 0, back.getWidth(), back.getHeight()));
            jxSearchPane.setBackgroundPainter(new MattePainter(paint));
            DefaultListModel model = new DefaultListModel();

            for (int i = 0; i < activeTab.getRankTable().getColumnCount(); i++) {
                TableColumnExt col = activeTab.getRankTable().getColumnExt(i);
                if (col.isVisible()) {
                    if (col.getTitle().equals("Name") || col.getTitle().equals("Tag")
                            || col.getTitle().equals("Stamm")) {
                        model.addElement(col.getTitle());
                    }

                }
            }
            jXColumnList.setModel(model);
            jXColumnList.setSelectedIndex(0);
            jxSearchPane.setVisible(true);
        }
    }

}

From source file:Main.java

public void paint(Graphics g) {

    int fontSize = 20;

    Font font = new Font("TimesRoman", Font.PLAIN, fontSize);
    g.setFont(font);//from ww w .  ja  v a 2  s .  c o  m

    FontMetrics fm = g.getFontMetrics(font);

    String s = "www.java2s.com";

    int stringWidth = fm.stringWidth(s);

    int w = 200;
    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:rod_design_compute.ShowPanel.java

@Override
public void paint(Graphics g) {
    super.paint(g);

    g.drawLine(originX, originY, originX + 50, originY);
    g.drawLine(originX + 50, originY, originX + 40, originY + 10);
    g.drawLine(originX + 50, originY, originX + 40, originY - 10);
    g.drawLine(originX, originY, originX, originY - 50);
    g.drawLine(originX, originY - 50, originX + 10, originY - 40);
    g.drawLine(originX, originY - 50, originX - 10, originY - 40);

    for (int i = 0; i < parent.arrayRodGroup.size(); i++) {
        switch (parent.arrayRodGroup.get(i).getType()) {
        case RodGroup.SR:
            paintSR((SR) parent.arrayRodGroup.get(i), g);
            break;
        case RodGroup.RRR:
            paintRRR((RRR) parent.arrayRodGroup.get(i), g);
            break;
        case RodGroup.RRP:
            paintRRP((RRP) parent.arrayRodGroup.get(i), g);
            break;
        default://from   w  w w  . j  a  v a  2  s. c  o m
            break;
        }
    }

    if (choosePoint != null) {
        drawChoosePoint(choosePoint, g);
    } else if (chooseRod != null) {
        drawChooseRod(chooseRodPoint1, chooseRodPoint2, g);
    }
}

From source file:com.lixiaocong.service.ImageCodeService.java

public BufferedImage getImage(HttpSession session) {
    //background//from   www  .  ja v a 2s. c om
    BufferedImage bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics graphics = bi.getGraphics();
    graphics.setColor(randomColor());
    graphics.fillRect(0, 0, WIDTH, HEIGHT);

    //content
    StringBuilder sb = new StringBuilder();
    int len = ch.length;
    Random random = new Random();
    for (int i = 0; i < 4; i++) {
        int index = random.nextInt(len);
        graphics.setColor(randomColor());
        graphics.setFont(new Font(null, Font.BOLD + Font.ITALIC, 30));
        graphics.drawString(ch[index] + "", i * 15 + 5, 25);
        sb.append(ch[index]);
    }
    session.setAttribute("imagecode", sb.toString());

    //lines
    for (int i = 0; i < 4; i++) {
        graphics.setColor(randomColor());
        graphics.drawLine(random.nextInt(WIDTH), random.nextInt(HEIGHT), random.nextInt(WIDTH),
                random.nextInt(HEIGHT));
    }

    return bi;
}

From source file:rod_design_compute.ShowPanel.java

private void drawChooseRod(Point a, Point b, Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    g2d.setStroke(new BasicStroke(2f));

    g.setColor(Color.blue);/*  ww  w .ja v  a  2 s  .c om*/
    g.drawLine(toScreenX(a.X), toScreenY(a.Y), toScreenX(b.X), toScreenY(b.Y));
}

From source file:edu.ku.brc.af.prefs.PrefsToolbar.java

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

    Color base = getBackground();
    Dimension size = getSize();/*  w ww .  j a  va 2s . co m*/

    Color grad_top = base;
    Color grad_bot = UIHelper.makeDarker(base, UIHelper.isMacOS() ? 0.15 : 0.1);
    GradientPaint bg = new GradientPaint(new Point(0, 0), grad_top, new Point(0, size.height), grad_bot);
    Graphics2D g2 = (Graphics2D) g;
    g2.setPaint(bg);
    g2.fillRect(0, 0, size.width, size.height);

    g.setColor(UIHelper.makeDarker(base, 0.5));
    g.drawLine(0, size.height - 1, size.width, size.height - 1);
}

From source file:edu.umn.cs.spatialHadoop.osm.OSMEdge.java

@Override
public void draw(Graphics g, double xscale, double yscale) {
    int x1 = (int) Math.round(this.lon1 * xscale);
    int y1 = (int) Math.round(this.lat1 * yscale);
    int x2 = (int) Math.round(this.lon2 * xscale);
    int y2 = (int) Math.round(this.lat2 * yscale);
    Color shape_color = g.getColor();
    // Compute alpha to use based on edge length and image scale
    double geom_alpha = this.getLength() * (xscale + yscale) / 2.0;
    int color_alpha = geom_alpha > 1.0 ? 255 : (int) Math.round(geom_alpha * 255);
    if (color_alpha == 0)
        return;/*from w w w .j  a v  a 2  s  .com*/

    g.setColor(new Color((shape_color.getRGB() & 0x00FFFFFF) | (color_alpha << 24), true));
    g.drawLine(x1, y1, x2, y2);
}

From source file:TreeLinkTest.java

void paintLines(Graphics g, Color dk, Color br) {
    if (comp.isVisible()) {
        Rectangle b = comp.getBounds();
        int x1off = b.x + b.width / 2;
        int x2off = b.x + b.width;
        int y1off = b.y;
        for (Enumeration e = children.elements(); e.hasMoreElements();) {
            TreeNode tn = (TreeNode) (e.nextElement());
            if (tn.comp.isVisible()) {
                Rectangle bn = tn.comp.getBounds();
                int y2off = bn.y + bn.height / 2;
                g.setColor(dk);/*from   w  w w.  ja  v  a2s .c o m*/
                g.drawLine(x1off, y1off, x1off, y2off);
                g.drawLine(x1off, y2off - 1, x2off, y2off - 1);
                g.setColor(br);
                g.drawLine(x1off + 1, y1off, x1off + 1, y2off);
                g.drawLine(x1off, y2off, x2off, y2off);
                tn.paintLines(g, dk, br);
            }
        }
    }
}