Josh
Josh

Reputation: 95

About positions a Rectangle on the screen

I have a JFrame and a class extending JPanel and overriding paintComponent() (named DrawingPanel). An instance of DrawingPanel is added to the JFrame.

This is the paintComponent() method of DrawingPanel:

public void paintComponent(Graphics g){
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    Rectangle rect = new Rectangle(80,180,50,50);
    g2d.fill(rect);
}

I ran the program, and it seems that the coordinats 80, 180 specified when creating rect, refer to the JFrame that contains the instance of DrawingPanel. I would expect that these coordinates would refer to the JPanel where the rectangle is actually painted.

1) Is it supposed to be like that?

2) Is it possible to change this?

Thanks

Upvotes: 0

Views: 49

Answers (1)

camickr
camickr

Reputation: 324157

I would expect that these coordinates would refer to the JPanel where the rectangle is actually painted.

They do refer to the JPanel, not the frame.

Why do you think they refer to the frame? Post your SSCCE that demonstrates the problem.

Upvotes: 1

Related Questions