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:OptimalPrimitives.java

protected void paintComponent(Graphics g) {
    Graphics2D g2d = (Graphics2D) g;
    long startTime, endTime, totalTime;

    g.setColor(Color.WHITE);//ww  w  . ja  v a  2  s .co m
    g.fillRect(0, 0, getWidth(), getHeight());
    g.setColor(Color.BLACK);

    g.drawString("Bad vs. Good Primitive Rendering", 50, 20);
    g.drawString("(" + ITERATIONS + " iterations)", 100, 35);
    g.drawString("Bad: ", 10, BAD_Y + 30);
    g.drawString("Good: ", 10, GOOD_Y + 30);

    // Bad line
    Shape line = new Line2D.Double(LINE_X, BAD_Y, LINE_X + 50, BAD_Y + 50);
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g2d.draw(line);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("bad line = " + totalTime);
    g.drawString(totalTime + " ms", LINE_X, BAD_Y + 70);

    // Good line
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g.drawLine(LINE_X, GOOD_Y, LINE_X + 50, GOOD_Y + 50);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("good line = " + totalTime);
    g.drawString(totalTime + " ms", LINE_X, GOOD_Y + 70);

    // Bad rect
    Shape rect = new Rectangle(RECT_X, BAD_Y, 50, 50);
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g2d.fill(rect);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("bad rect = " + totalTime);
    g.drawString(totalTime + " ms", RECT_X, BAD_Y + 70);

    // Good rect
    startTime = System.nanoTime();
    for (int i = 0; i < ITERATIONS; ++i) {
        g.fillRect(RECT_X, GOOD_Y, 50, 50);
    }
    endTime = System.nanoTime();
    totalTime = (endTime - startTime) / 1000000;
    System.out.println("good rect = " + totalTime);
    g.drawString(totalTime + " ms", RECT_X, GOOD_Y + 70);
}

From source file:rod_design_compute.ShowPanel.java

private void drawBasePoint(Point point, Graphics g) {
    int x = toScreenX(point.X);
    int y = toScreenY(point.Y);

    int lengthTri = 4 * radiusBase;
    int x1 = (int) (x - lengthTri * Math.sin(Math.toRadians(30)));
    int y1 = (int) (y + lengthTri * Math.cos(Math.toRadians(30)));
    int x2 = (int) (x + lengthTri * Math.sin(Math.toRadians(30)));
    int y2 = (int) (y + lengthTri * Math.cos(Math.toRadians(30)));

    g.drawLine(x, y, x1, y1);
    g.drawLine(x, y, x2, y2);// w  w  w.j a  v a 2  s  .  co  m
    g.drawLine(x1 - radiusBase, y1, x2 + radiusBase, y2);

    g.drawLine(x1, y1, x1 - radiusBase, y1 + radiusBase);
    g.drawLine(x1 + radiusBase, y1, x1 + radiusBase - radiusBase, y1 + radiusBase);
    g.drawLine(x1 + 2 * radiusBase, y1, x1 + 2 * radiusBase - radiusBase, y1 + radiusBase);
    g.drawLine(x1 + 3 * radiusBase, y1, x1 + 3 * radiusBase - radiusBase, y1 + radiusBase);
    g.drawLine(x1 + 4 * radiusBase, y1, x1 + 4 * radiusBase - radiusBase, y1 + radiusBase);

    g.setColor(Color.white);
    g.fillOval(x - radiusBase, y - radiusBase, radiusBase * 2, radiusBase * 2);
    g.setColor(Color.black);
    g.drawOval(x - radiusBase, y - radiusBase, radiusBase * 2, radiusBase * 2);
}

From source file:net.sf.taverna.t2.workbench.ui.impl.UserRegistrationForm.java

/**
 * Adds a light gray or etched border to the top or bottom of a JComponent.
 * /*from w w  w  .ja va2 s .c om*/
 * @author David Withers
 * @param component
 */
protected void addDivider(JComponent component, final int position, final boolean etched) {
    component.setBorder(new Border() {
        private final Color borderColor = new Color(.6f, .6f, .6f);

        @Override
        public Insets getBorderInsets(Component c) {
            if (position == TOP)
                return new Insets(5, 0, 0, 0);
            else
                return new Insets(0, 0, 5, 0);
        }

        @Override
        public boolean isBorderOpaque() {
            return false;
        }

        @Override
        public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
            if (position == TOP) {
                if (etched) {
                    g.setColor(borderColor);
                    g.drawLine(x, y, x + width, y);
                    g.setColor(WHITE);
                    g.drawLine(x, y + 1, x + width, y + 1);
                } else {
                    g.setColor(LIGHT_GRAY);
                    g.drawLine(x, y, x + width, y);
                }
            } else {
                if (etched) {
                    g.setColor(borderColor);
                    g.drawLine(x, y + height - 2, x + width, y + height - 2);
                    g.setColor(WHITE);
                    g.drawLine(x, y + height - 1, x + width, y + height - 1);
                } else {
                    g.setColor(LIGHT_GRAY);
                    g.drawLine(x, y + height - 1, x + width, y + height - 1);
                }
            }
        }
    });
}

From source file:Clock.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Color colorRetainer = g.getColor();

    g.setColor(getBackground());/*from   www.j a va2  s.  c  o m*/
    g.fillRect(0, 0, getWidth(), getHeight());
    getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight());

    calendar.setTime(new Date()); // get current time
    int hrs = calendar.get(Calendar.HOUR_OF_DAY);
    int min = calendar.get(Calendar.MINUTE);

    g.setColor(getForeground());
    if (isDigital) {
        String time = "" + hrs + ":" + min;
        g.setFont(getFont());
        FontMetrics fm = g.getFontMetrics();
        int y = (getHeight() + fm.getAscent()) / 2;
        int x = (getWidth() - fm.stringWidth(time)) / 2;
        g.drawString(time, x, y);
    } else {
        int x = getWidth() / 2;
        int y = getHeight() / 2;
        int rh = getHeight() / 4;
        int rm = getHeight() / 3;

        double ah = ((double) hrs + min / 60.0) / 6.0 * Math.PI;
        double am = min / 30.0 * Math.PI;

        g.drawLine(x, y, (int) (x + rh * Math.sin(ah)), (int) (y - rh * Math.cos(ah)));
        g.drawLine(x, y, (int) (x + rm * Math.sin(am)), (int) (y - rm * Math.cos(am)));
    }

    g.setColor(colorRetainer);
}

From source file:edu.ku.brc.stats.StatGroupTable.java

/**
 * Overrides paint to draw in name at top with separator AND the Label
 *//*from w w w.j  av  a  2 s.  c o  m*/
public void paint(Graphics g) {
    super.paint(g);

    if (!useSeparator) {
        Dimension dim = getSize();

        FontMetrics fm = g.getFontMetrics();
        int strW = fm.stringWidth(name);

        int x = (dim.width - strW) / 2;
        Insets ins = getBorder().getBorderInsets(this);
        int y = 2 + fm.getAscent();

        int lineW = dim.width - ins.left - ins.right;
        g.setColor(Color.BLUE.darker());
        g.drawString(name, x, y);
        x = ins.left;
        y += fm.getDescent() + fm.getLeading();

        g.setColor(Color.LIGHT_GRAY.brighter());
        g.drawLine(x, y, x + lineW, y);
        y++;
        x++;
        g.setColor(Color.LIGHT_GRAY);
        g.drawLine(x, y, x + lineW, y);
    }
}

From source file:Clock.java

public void paint(Graphics g) {
    int xhour, yhour, xminute, yminute, xsecond, ysecond, second, minute, hour;
    drawStructure(g);//from  w  w w  .  j  a va2s .  c om

    currentDate = new Date();

    formatter.applyPattern("s");
    second = Integer.parseInt(formatter.format(currentDate));
    formatter.applyPattern("m");
    minute = Integer.parseInt(formatter.format(currentDate));

    formatter.applyPattern("h");
    hour = Integer.parseInt(formatter.format(currentDate));

    xsecond = (int) (Math.cos(second * 3.14f / 30 - 3.14f / 2) * 45 + xcenter);
    ysecond = (int) (Math.sin(second * 3.14f / 30 - 3.14f / 2) * 45 + ycenter);
    xminute = (int) (Math.cos(minute * 3.14f / 30 - 3.14f / 2) * 40 + xcenter);
    yminute = (int) (Math.sin(minute * 3.14f / 30 - 3.14f / 2) * 40 + ycenter);
    xhour = (int) (Math.cos((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 30 + xcenter);
    yhour = (int) (Math.sin((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 30 + ycenter);

    // Erase if necessary, and redraw
    g.setColor(Color.lightGray);
    if (xsecond != lastxs || ysecond != lastys) {
        g.drawLine(xcenter, ycenter, lastxs, lastys);
    }
    if (xminute != lastxm || yminute != lastym) {
        g.drawLine(xcenter, ycenter - 1, lastxm, lastym);
        g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
    }
    if (xhour != lastxh || yhour != lastyh) {
        g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
        g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
    }

    g.setColor(Color.darkGray);
    g.drawLine(xcenter, ycenter, xsecond, ysecond);

    g.setColor(Color.red);
    g.drawLine(xcenter, ycenter - 1, xminute, yminute);
    g.drawLine(xcenter - 1, ycenter, xminute, yminute);
    g.drawLine(xcenter, ycenter - 1, xhour, yhour);
    g.drawLine(xcenter - 1, ycenter, xhour, yhour);
    lastxs = xsecond;
    lastys = ysecond;
    lastxm = xminute;
    lastym = yminute;
    lastxh = xhour;
    lastyh = yhour;
}

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

private void paintData(final Graphics g) {
    for (int channel = 0; channel < uiInstanceConfiguration.getNumTotalChannels(); ++channel) {
        if (!signalVisibility[channel])
            continue;

        g.setColor(visColours[channel]);

        int previousMinY = -1;
        int previousMaxY = -1;
        for (int x = 0; x < magsWidth; ++x) {
            final int minY = channelValues[channel][x * 2];
            final int maxY = channelValues[channel][(x * 2) + 1];

            int drawMinY = minY;
            int drawMaxY = maxY;
            if (previousMinY != -1) {
                if (previousMinY > drawMaxY) {
                    drawMaxY = previousMinY;
                }/*from   www . j  a va 2s  . c  o m*/

                if (previousMaxY < drawMinY) {
                    drawMinY = previousMaxY;
                }
            }

            g.drawLine(x, magsHeight - drawMinY, x, magsHeight - drawMaxY);

            previousMinY = minY;
            previousMaxY = maxY;
        }
    }
}

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

@Override
public void actionPerformed(ActionEvent e) {
    AttackTableTab activeTab = getActiveTab();
    int idx = jAttackTabPane.getSelectedIndex();
    if (e.getActionCommand() != null && activeTab != null) {
        if (e.getActionCommand().equals("TimeChange")) {
            activeTab.fireChangeTimeEvent();
        } else if (e.getActionCommand().equals("UnitChange")) {
            activeTab.fireChangeUnitEvent();
        } else if (e.getActionCommand().equals("Recolor")) {
            activeTab.updateSortHighlighter();
        } else if (e.getActionCommand().equals("ExportScript")) {
            activeTab.fireExportScriptEvent();
        } else if (e.getActionCommand().equals("Copy")) {
            activeTab.transferSelection(AttackTableTab.TRANSFER_TYPE.COPY_TO_INTERNAL_CLIPBOARD);
        } else if (e.getActionCommand().equals("BBCopy")) {
            activeTab.transferSelection(AttackTableTab.TRANSFER_TYPE.CLIPBOARD_BB);
        } else if (e.getActionCommand().equals("Cut")) {
            activeTab.transferSelection(AttackTableTab.TRANSFER_TYPE.CUT_TO_INTERNAL_CLIPBOARD);
            jAttackTabPane.setSelectedIndex(idx);
        } else if (e.getActionCommand().equals("Paste")) {
            activeTab.transferSelection(AttackTableTab.TRANSFER_TYPE.FROM_INTERNAL_CLIPBOARD);
            jAttackTabPane.setSelectedIndex(idx);
        } else if (e.getActionCommand().equals("Delete")) {
            activeTab.deleteSelection(true);
        } else 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();/*  w w  w. ja va 2 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.getAttackTable().getColumnCount(); i++) {
                TableColumnExt col = activeTab.getAttackTable().getColumnExt(i);
                if (col.isVisible()) {
                    if (!col.getTitle().equals("Einheit") && !col.getTitle().equals("Typ")
                            && !col.getTitle().equals("Sonstiges") && !col.getTitle().equals("Abschickzeit")
                            && !col.getTitle().equals("Ankunftzeit") && !col.getTitle().equals("Verbleibend")) {
                        model.addElement(col.getTitle());
                    }
                }
            }
            jXColumnList.setModel(model);
            jXColumnList.setSelectedIndex(0);
            jxSearchPane.setVisible(true);
        }
    }
}

From source file:tilt.image.page.Line.java

/**
 * Draw the line on the image for debugging
 * @param g the graphics environment to draw in
 * @param n the number of the line/*from w ww  . ja  v  a  2s .c  om*/
 */
public void draw(Graphics g, int n) {
    Point p1, p2 = null;
    for (int i = 0; i < points.size(); i++) {
        p1 = p2;
        p2 = points.get(i);
        if (p1 != null) {
            int x1 = Math.round(p1.x);
            int y1 = Math.round(p1.y);
            int x2 = Math.round(p2.x);
            int y2 = Math.round(p2.y);
            if (!rogue) {
                g.drawLine(x1, y1, x2, y2);
            } else {
                Graphics2D g2 = (Graphics2D) g;
                g2.setStroke(new BasicStroke(3));
                g2.draw(new Line2D.Float(x1, y1, x2, y2));
                g2.setStroke(new BasicStroke(1));
            }
        }
    }
    Rectangle bounds = getPointsBounds();
    String num = Integer.toString(n);
    g.drawString(num, bounds.x - 30, (bounds.y * 2 + bounds.height) / 2);
    //System.out.println("width of line"+n+": "+bounds.width);
}

From source file:rod_design_compute.ShowPanel.java

private void drawSlider(Rod rod, Point point, Graphics g) {
    int[] x = new int[3];
    int[] y = new int[3];
    int l = lengthSlider;
    double theta = rod.getAngle();

    x[0] = (int) (Math.cos(theta) * l / 2);
    y[0] = (int) (Math.sin(theta) * l / 2);
    x[1] = -x[0];/*  w w  w  .  j  av a2s. co m*/
    y[1] = -y[0];

    theta += Math.toRadians(45);
    x[2] = (int) (Math.cos(theta) * l / 10);
    y[2] = (int) (Math.sin(theta) * l / 10);

    for (int i = 0; i < 2; i++) {
        x[i] = toScreenX(point.X) + x[i];
        y[i] = toScreenY(point.Y) - y[i];
    }

    g.drawLine(x[0], y[0], x[1], y[1]);
    g.drawLine((int) (0.9 * x[0] + 0.1 * x[1]), (int) (0.9 * y[0] + 0.1 * y[1]),
            (int) (0.9 * x[0] + 0.1 * x[1] + x[2]), (int) (0.9 * y[0] + 0.1 * y[1] + y[2]));
    g.drawLine((int) (0.8 * x[0] + 0.2 * x[1]), (int) (0.8 * y[0] + 0.2 * y[1]),
            (int) (0.8 * x[0] + 0.2 * x[1] + x[2]), (int) (0.8 * y[0] + 0.2 * y[1] + y[2]));
    g.drawLine((int) (0.1 * x[0] + 0.9 * x[1]), (int) (0.1 * y[0] + 0.9 * y[1]),
            (int) (0.1 * x[0] + 0.9 * x[1] + x[2]), (int) (0.1 * y[0] + 0.9 * y[1] + y[2]));
    g.drawLine((int) (0.2 * x[0] + 0.8 * x[1]), (int) (0.2 * y[0] + 0.8 * y[1]),
            (int) (0.2 * x[0] + 0.8 * x[1] + x[2]), (int) (0.2 * y[0] + 0.8 * y[1] + y[2]));
}