public class Points extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine(60, 20, 80, 90);
}
}
I'm not really sure what's the Graphics2D g2d = (Graphics2D) g;
supposed to do.
It's just a plain JPanel that's later added onto a JFrame.
It would be really helpfull if anyone could give me some advice as I'm stuck at this line of the code for a long time now.
The statement
Graphics2D g2d = (Graphics2D) g;
just casts the Graphics
object to a Graphics2D
. It's used to access the methods provided by Graphics2D
. In this case it is unnecessary as Graphics
also has a drawLine
method so if you don't have a requirement for the more advanced methods such as rotate
and translate
, you can use
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawLine(60, 20, 80, 90);
}