Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you increase line thickness when using Java Graphics for an applet? I don't believe that BasicStroke works [duplicate]

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???
    }
}
like image 385
user2465406 Avatar asked Jun 08 '13 02:06

user2465406


People also ask

What is the difference between graphics and Graphics2D?

Graphics is the parameter of the paint method and a Graphics2D object may be created from a Graphics object.

What is setStroke?

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.

Which Java package contains the graphics class used to draw shapes in an applet?

The java. awt package is used to create the graphics used in the applet windows.

How do you draw a line graphic in Java?

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.


1 Answers

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.

like image 192
Edwin Buck Avatar answered Sep 20 '22 18:09

Edwin Buck