Java2D: Increase the line width
You should use setStroke
to set a stroke of the Graphics2D
object.
The example at http://www.java2s.com gives you some code examples.
The following code produces the image below:
import java.awt.*;
import java.awt.geom.Line2D;
import javax.swing.*;
public class FrameTest {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
cp.add(new JComponent() {
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g2.draw(new Line2D.Float(30, 20, 80, 90));
}
});
jf.setSize(300, 200);
jf.setVisible(true);
}
}
(Note that the setStroke
method is not available in the Graphics
object. You have to cast it to a Graphics2D
object.)
This post has been rewritten as an article here.
What is Stroke
:
The BasicStroke class defines a basic set of rendering attributes for the outlines of graphics primitives, which are rendered with a Graphics2D object that has its Stroke attribute set to this BasicStroke.
https://docs.oracle.com/javase/7/docs/api/java/awt/BasicStroke.html
Note that the Stroke
setting:
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
is setting the line width,since BasicStroke(float width)
:
Constructs a solid BasicStroke with the specified line width and with default values for the cap and join styles.
And, it also effects other methods like Graphics2D.drawLine(int x1, int y1, int x2, int y2)
and Graphics2D.drawRect(int x, int y, int width, int height)
:
The methods of the Graphics2D interface that use the outline Shape returned by a Stroke object include draw and any other methods that are implemented in terms of that method, such as drawLine, drawRect, drawRoundRect, drawOval, drawArc, drawPolyline, and drawPolygon.