Drawing a line on java

How do I draw a line in Java 2D?

The following code snippet show you how to draw a simple line using Graphics2D.draw() method. This method take a parameter that implements the java.awt.Shape interface.

To draw a line we can use the Line2D.Double static-inner class. This class constructor takes four integers values that represent the start (x1, y1) and end (x2, y2) coordinate of the line.

package org.kodejava.awt.geom; import javax.swing.*; import java.awt.*; import java.awt.geom.Line2D; public class DrawLine extends JComponent < public static void main(String[] args) < JFrame frame = new JFrame("Draw Line"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawLine()); frame.pack(); frame.setSize(new Dimension(420, 440)); frame.setVisible(true); >@Override public void paint(Graphics g) < // Draw a simple line using the Graphics2D draw() method. Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(2f)); g2.setColor(Color.RED); g2.draw(new Line2D.Double(50, 150, 250, 350)); g2.setColor(Color.GREEN); g2.draw(new Line2D.Double(250, 350, 350, 250)); g2.setColor(Color.BLUE); g2.draw(new Line2D.Double(350, 250, 150, 50)); g2.setColor(Color.YELLOW); g2.draw(new Line2D.Double(150, 50, 50, 150)); g2.setColor(Color.BLACK); g2.draw(new Line2D.Double(0, 0, 400, 400)); >> 

When you run the snippet it will show you something like:

Draw 2D Line

A programmer, recreational runner and diver, live in the island of Bali, Indonesia. Programming in Java, Spring, Hibernate / JPA. You can support me working on this project, buy me a cup of coffee ☕ every little bit helps, thank you 🙏

Источник

Drawing lines examples with Java Graphics2D

A line is a graphics primitive that connects two points. In Java, to draw a line between two points (x1, y1) and (x2, y2) onto graphics context represented by a Graphics object, use the following method:

drawLine(int x1, int y1, int x2, int y2)

If a Graphics2D object is used, the following method is more object-oriented:

With two implementations of Line2D : Line2D.Double (with double coordinates) and Line2D.Float (with float coordinates).

Both methods draw a line using the current graphic attributes (paint color, stroke, rendering hints, etc). Here is a quick example (suppose that g2d is a reference of a Graphics2D object):

// draw a line in integer coordinates g2d.drawLine(20, 50, 460, 50); // draw a line in double coordinates g2d.draw(new Line2D.Double(21.5d, 199.8d, 459.5d, 199.8d)); // draw a line in float coordinates g2d.draw(new Line2D.Float(21.50f, 232.50f, 459.50f, 232.50f));

The following source code is a Swing program that draws three lines onto the graphics context of the JFrame window:

package net.codejava.graphics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Line2D; import javax.swing.JFrame; import javax.swing.SwingUtilities; /** * This program demonstrates how to draw lines using Graphics2D object. * @author www.codejava.net * */ public class LinesDrawingExample extends JFrame < public LinesDrawingExample() < super("Lines Drawing Demo"); setSize(480, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); >void drawLines(Graphics g) < Graphics2D g2d = (Graphics2D) g; g2d.drawLine(120, 50, 360, 50); g2d.draw(new Line2D.Double(59.2d, 99.8d, 419.1d, 99.8d)); g2d.draw(new Line2D.Float(21.50f, 132.50f, 459.50f, 132.50f)); >public void paint(Graphics g) < super.paint(g); drawLines(g); >public static void main(String[] args) < SwingUtilities.invokeLater(new Runnable() < @Override public void run() < new LinesDrawingExample().setVisible(true); >>); > >

Here, the key point is to override the paint() method from the superclass ( JFrame ) so that we can obtain the graphics context:

Читайте также:  Размеры изображения

public void paint(Graphics g)

lines drawing demo 1

Now, let’s see more complex examples in which we will alter the graphics attributes in order to decorate the lines drawn. Note that the graphics attributes must be set before drawing the lines.

Setting paint color and thickness for the lines

To specify a specific color for the line, call setColor(Color) method before drawing, for example:

// creates a solid stroke with line width is 2 Stroke stroke = new BasicStroke(2f);
g2d.setColor(Color.RED); // creates a solid stroke with line width is 2 Stroke stroke = new BasicStroke(2f); g2d.setStroke(stroke); g2d.drawLine(120, 50, 360, 50); g2d.setColor(Color.GREEN); g2d.setStroke(new BasicStroke(4f)); g2d.draw(new Line2D.Double(59.2d, 99.8d, 419.1d, 99.8d)); g2d.setColor(Color.BLUE); g2d.setStroke(new BasicStroke(6f)); g2d.draw(new Line2D.Float(21.50f, 132.50f, 459.50f, 132.50f));

lines drawing demo 2

Drawing dashed lines:

float[] dashingPattern1 = ; Stroke stroke1 = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dashingPattern1, 2.0f); g2d.setStroke(stroke1); g2d.drawLine(120, 50, 360, 50);

The dashing pattern is formed by alternating between opaque and transparent sections with the widths specified by the float array:

This pattern specifies that the first two pixels are opaque; the next two are transparent; the next two are opaque; and so on…. Here’s the result:

dashedl line 1

The original example is updated to draw dashed lines as follows:

g2d.setColor(Color.RED); float[] dashingPattern1 = ; Stroke stroke1 = new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dashingPattern1, 2.0f); g2d.setStroke(stroke1); g2d.drawLine(120, 50, 360, 50); g2d.setColor(Color.GREEN); float[] dashingPattern2 = ; Stroke stroke2 = new BasicStroke(4f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dashingPattern2, 0.0f); g2d.setStroke(stroke2); g2d.draw(new Line2D.Double(59.2d, 99.8d, 419.1d, 99.8d)); g2d.setColor(Color.BLUE); float[] dashingPattern3 = ; Stroke stroke3 = new BasicStroke(4f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 1.0f, dashingPattern3, 0.0f); g2d.setStroke(stroke3); g2d.draw(new Line2D.Float(21.50f, 132.50f, 459.50f, 132.50f));

dashed lines demo

Decorating line end caps

Caps are decorations applied at both ends of a line (solid, unclosed-path line) or dash segments in a dashed line. The BasicStroke class provides three cap styles: CAP_SQUARE (default), CAP_BUTT and CAP_ROUND . The following example creates three strokes with different cap styles:

// this stroke with default CAP_SQUARE and JOIN_MITER Stroke stroke1 = new BasicStroke(12f); // this stroke with CAP_BUTT Stroke stroke2 = new BasicStroke(12f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER); // this stroke with CAP_ROUND Stroke stroke3 = new BasicStroke(12f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
g2d.setStroke(stroke1); g2d.drawLine(30, 60, 450, 60); g2d.setStroke(stroke2); g2d.drawLine(30, 100, 450, 100); g2d.setStroke(stroke3); g2d.drawLine(30, 140, 450, 140);

line caps demo

Decorating line joins

Joins are decorations applied at intersections between lines. The BasicStroke class provides three join styles: JOIN_MITER (default), JOIN_BEVEL and JOIN_ROUND . The following example creates three strokes with different join styles:

// this stroke with default CAP_SQUARE and JOIN_MITER Stroke stroke1 = new BasicStroke(12f); // this stroke with JOIN_BEVEL Stroke stroke2 = new BasicStroke(12f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_BEVEL); // this stroke with JOIN_ROUND Stroke stroke3 = new BasicStroke(12f, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND);

And the following code snippet draws three rectangles using lines to demonstrate the concept of join decorations:

// draws the first rectangle with a stroke of JOIN_MITER g2d.setStroke(stroke1); g2d.drawLine(30, 60, 120, 60); g2d.drawLine(30, 60, 30, 150); g2d.drawLine(120, 60, 120, 150); g2d.drawLine(120, 60, 120, 150); g2d.drawLine(30, 150, 120, 150); // draws the second rectangle with a stroke of JOIN_BEVEL g2d.setStroke(stroke2); g2d.drawLine(30 + 120, 60, 120 + 120, 60); g2d.drawLine(30 + 120, 60, 30 + 120, 150); g2d.drawLine(120 + 120, 60, 120 + 120, 150); g2d.drawLine(120 + 120, 60, 120 + 120, 150); g2d.drawLine(30 + 120, 150, 120 + 120, 150); // draws the third rectangle with a stroke of JOIN_ROUND g2d.setStroke(stroke3); g2d.drawLine(30 + 240, 60, 120 + 240, 60); g2d.drawLine(30 + 240, 60, 30 + 240, 150); g2d.drawLine(120 + 240, 60, 120 + 240, 150); g2d.drawLine(120 + 240, 60, 120 + 240, 150); g2d.drawLine(30 + 240, 150, 120 + 240, 150);

line joins demo

API References:

Other Java Graphics Tutorials:

Читайте также:  Изображения

About the Author:

Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.

Источник

Draw a Line in Java

Draw a Line in Java

The Java.awt.Graphics class in Java forms the base for many such drawing and graphics functions. It is an incomprehensible class, as the actual drawing action depends on the system and depends on the device. We will draw a line in Java in this tutorial.

We will start the program by importing the necessary packages. We will import the java.applet.Applet , java.awt and java.awt.event package from the library.

The drawLine() method of the Graphics class is used to draw a line with the given color between two points.

import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class DrawLine extends Applet   public static void main(String[] args)    Frame drawLineApplet = new Frame("Draw Line in Applet Window");  drawLineApplet.setSize(500, 450);  Applet DrawLine = new DrawLine();  drawLineApplet.add(DrawLine);  drawLineApplet.setVisible(true);  drawLineApplet.addWindowListener(new WindowAdapter()   public void windowClosing(WindowEvent e) System.exit(0); >  >);  > public void paint(Graphics g)    g.setFont(new Font("Arial",Font.BOLD,12));  g.drawString("This is Draw Line Example", 100, 70);  g.setColor(Color.blue);  g.drawLine(90, 135, 90, 180);  g.setColor(Color.green);  g.drawLine(60, 4, 120, 120); > > 

draw line in java

In the above example, we created two lines and also displayed some text. We first declared a DrawLine class, which extends the Applet class (Parent class). Inside the class, we declared the main method. Here the Frame drawLineApplet = new Frame() statement creates the applet window for the output.

The drawLineApplet.setSize() function is used to set the size of the applet window, and the drawLineApplet.setVisible(true) function is used to make the frame appear on the screen. We use the system.exit(0) command to exit the applet frame.

The paint method here is used to set the color, font, and coordinates o the line to be drawn. We change the font using the setFont() function. The drawString() function here displays some text on the output frame. We alter the color of the first line using setColor() and then the x and y coordinates of the line in the drawLine() function. Similarly, we provide the coordinates and color for the second line.

Related Article — Java GUI

Copyright © 2023. All right reserved

Источник

Оцените статью