I am having trouble adjusting line thickness. Can I do that in Graphics or do i have to do it in Graphics2D? If so, how do I alter the program to make it run?
Thanks!
import java.applet.Applet;
import java.awt.*;
public class myAppletNumberOne extends Applet {
public void paint (Graphics page) {
//Something here???
}
}
Graphics is the parameter of the paint method and a Graphics2D object may be created from a Graphics object.
To set the stroke attribute, you create a BasicStroke object and pass it into the Graphics2D setStroke method. A BasicStroke object holds information about the line width, join style, end-cap style, and dash style. This information is used when a Shape is rendered with the draw method.
The java. awt package is used to create the graphics used in the applet windows.
Java Applet | Draw a line using drawLine() method x1 – It takes the first point's x coordinate. y1 – It takes first point's y coordinate. x2 – It takes second point's x coordinate. y2 – It takes second point's y coordinate.
Yes you have to do it in Graphics2D, but that's hardly an issue, as every Graphics in Swing is a Graphics2D object (it just keeps the old interface for compatibility reasons).
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(3));
g2.drawLine(...); //thick
...
}
As you can see, the g2.setStroke(...) allows you to change the stroke, and there's even a BasicStroke which provides for easy line width selection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With