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

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Composite composite = new Composite(shell, SWT.EMBEDDED);

    Frame frame = SWT_AWT.new_Frame(composite);
    Canvas canvas = new Canvas() {
        public void paint(Graphics g) {
            Dimension d = getSize();
            g.drawLine(0, 0, d.width, d.height);
        }//from   w  w  w.  j a v  a 2  s.c  o  m
    };
    frame.add(canvas);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    BufferedImage bi;/* www.  j  a  va2  s. c  om*/
    bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.getGraphics();
    for (int i = 0; i < NUM_ITER; i++) {
        g.setColor(Color.RED);
        g.drawLine(1, 2, i, i + 1);
    }
    g.dispose();
    ImageIO.write(bi, "gif", new File("image.gif"));
}

From source file:org.eclipse.swt.snippets.Snippet155.java

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("Snippet 155");
    shell.setLayout(new FillLayout());
    Composite composite = new Composite(shell, SWT.EMBEDDED);

    /* Draw an X using AWT */
    Frame frame = SWT_AWT.new_Frame(composite);
    Canvas canvas = new Canvas() {
        @Override//from   w  w w . j  av  a2  s .  com
        public void paint(Graphics g) {
            Dimension d = getSize();
            g.drawLine(0, 0, d.width, d.height);
            g.drawLine(d.width, 0, 0, d.height);
        }
    };
    frame.add(canvas);

    shell.open();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:FullScreen.java

public static void main(String args[]) {
    GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
    DisplayMode originalDisplayMode = graphicsDevice.getDisplayMode();

    try {//from  w w  w .j  a  va2  s. co  m
        Frame frame = new Frame();
        frame.setUndecorated(true);
        frame.setIgnoreRepaint(true);
        graphicsDevice.setFullScreenWindow(frame);
        if (graphicsDevice.isDisplayChangeSupported()) {
            graphicsDevice.setDisplayMode(getBestDisplayMode(graphicsDevice));
        }
        frame.createBufferStrategy(2); // 2 buffers
        Rectangle bounds = frame.getBounds();
        BufferStrategy bufferStrategy = frame.getBufferStrategy();
        while (!done()) {
            Graphics g = null;
            try {
                g = bufferStrategy.getDrawGraphics();
                if ((counter <= 2)) { // 2 buffers
                    g.setColor(Color.CYAN);
                    g.fillRect(0, 0, bounds.width, bounds.height);
                }
                g.setColor(Color.RED);
                // redraw prior line, too, since 2 buffers
                if (counter != 1) {
                    g.drawLine(counter - 1, (counter - 1) * 5, bounds.width, bounds.height);
                }
                g.drawLine(counter, counter * 5, bounds.width, bounds.height);
                bufferStrategy.show();
            } finally {
                if (g != null) {
                    g.dispose();
                }
            }
            try {
                Thread.sleep(250);
            } catch (InterruptedException ignored) {
            }
        }
    } finally {
        graphicsDevice.setDisplayMode(originalDisplayMode);
        graphicsDevice.setFullScreenWindow(null);
    }
    System.exit(0);
}

From source file:Main.java

public static void main(String[] args) {
    final int width = 512;
    final int height = 512;
    BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics g = img.getGraphics();
    g.setColor(Color.black);//from w  w w.  j  a v  a 2s  . c o m
    g.fillRect(0, 0, width, height);
    g.setColor(Color.white);
    final double A = 8;
    final double B = 0.5;
    final double N = 4;
    final double scale = 128;
    final double zoom = 50;
    final double step = 1 / scale;
    Point last = null;
    final Point origin = new Point(width / 2, height / 2);

    for (double t = 0; t <= 2 * Math.PI; t += step) {
        final double r = zoom * polarFunction(t, A, B, N);
        final int x = (int) Math.round(r * Math.cos(t));
        final int y = (int) Math.round(r * Math.sin(t));
        Point next = new Point(x, y);
        if (last != null) {
            g.drawLine(origin.x + last.x, origin.y + last.y, origin.x + next.x, origin.y + next.y);
        }
        last = next;
    }

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JLabel(new ImageIcon(img)));
    frame.pack();
    frame.setVisible(true);
}

From source file:Main.java

/**
 * Draws the point (<b>x</b>, <b>y</b>) in the current color.
 *//*from w w w .jav a  2  s  .  com*/
static void drawPoint(Graphics g, int x, int y) {
    g.drawLine(x, y, x, y);
}

From source file:Main.java

/**
 * Fills a circle with the specified bounds. This is used instead of  
 * {@link Graphics#fillRect(int, int, int, int)} due to anti-aliasing 
 * issues.//from   w w w  .j a  v  a 2  s  .  c o m
 * 
 * @param g the graphics instance to use for painting
 * @param x the x position for the circle
 * @param y the y position for the circle
 * @param width the width of the circle
 * @param height the height of the circle
 */
public static void fillCircle(Graphics g, int x, int y, int width, int height) {
    g.fillRect(x + 1, y + 1, width - 1, height - 1);
    g.drawLine(x + 1, y, x + width - 1, y);
    g.drawLine(x + width, y + 1, x + width, y + height - 1);
    g.drawLine(x + 1, y + height, x + width - 1, y + height);
    g.drawLine(x, y + 1, x, y + height - 1);
}

From source file:GraphicsUtil.java

public static void drawOptimizedLine(Graphics g, int x1, int y1, int x2, int y2) {
    if (g.getColor().getAlpha() < 255 && (x1 == x2 || y1 == y2))
        g.fillRect(x1 < x2 ? x1 : x2, y1 < y2 ? y1 : y2, Math.abs(x2 - x1) + 1, Math.abs(y2 - y1) + 1);
    else// w  ww .ja v a  2  s .  c  om
        g.drawLine(x1, y1, x2, y2);
}

From source file:Main.java

public static void drawGroove(Graphics g, int x, int y, int w, int h, Color shadow, Color highlight) {
    Color oldColor = g.getColor(); // Make no net change to g
    g.translate(x, y);//www .  jav  a 2s .  c o  m

    g.setColor(shadow);
    g.drawRect(0, 0, w - 2, h - 2);

    g.setColor(highlight);
    g.drawLine(1, h - 3, 1, 1);
    g.drawLine(1, 1, w - 3, 1);

    g.drawLine(0, h - 1, w - 1, h - 1);
    g.drawLine(w - 1, h - 1, w - 1, 0);

    g.translate(-x, -y);
    g.setColor(oldColor);
}

From source file:XAxisDiffAlign.java

private static Container makeIt(String title, boolean more) {
    JPanel container = new JPanel() {
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Insets insets = getInsets();
            int width = getWidth();
            int height = getHeight() - insets.top - insets.bottom;
            int halfHeight = height / 2 + insets.top;
            g.drawLine(0, halfHeight, width, halfHeight);
        }//  w  w w  . j  a  v  a  2s .c  o  m
    };
    container.setBorder(BorderFactory.createTitledBorder(title));
    BoxLayout layout = new BoxLayout(container, BoxLayout.X_AXIS);
    container.setLayout(layout);

    JButton button;
    button = new JButton("0.0");
    button.setOpaque(false);
    button.setAlignmentY(Component.TOP_ALIGNMENT);
    container.add(button);
    if (more) {
        button = new JButton(".25");
        button.setOpaque(false);
        button.setAlignmentY(0.25f);
        container.add(button);
        button = new JButton(".5");
        button.setOpaque(false);
        button.setAlignmentY(Component.CENTER_ALIGNMENT);
        container.add(button);
        button = new JButton(".75");
        button.setOpaque(false);
        button.setAlignmentY(0.75f);
        container.add(button);
    }
    button = new JButton("1.0");
    button.setOpaque(false);
    button.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    container.add(button);

    return container;
}