Questions about the Graphics and Graphics2d classes in Java

Asked

Viewed 305 times

2

I am having many doubts regarding the graphic libraries of Java, which are the following:

  1. What is the difference between overwriting the method paint(Graphics) or the method paintComponent(Graphics)?

  2. What the class Graphics2D brings difference in relation to class Graphics?

  • I think there are a lot of questions for just one question. I recommend that I left only the 2 and 3 that are directly related.

  • In short, swing is an evolution of awt but he ends up inheriting many things from the awt, like the Paint method, which is responsible for the design of the components in the awt, but in swing, the mother class of all components was rewritten (Component in awt and Jcomponent in swing), and the paintcomponent’s drawing of the canvas was deleted. Because every class in the API inherits from Jcomponent, and this method is its core, it turns out it’s the best option for rewriting when we need to redesign a component.

  • The second question is partially answered in the previous comment, adding to the fact that Graphics2d is an evolution of Graphics, prepared to provide better tools for geometric drawings and shapes, colors and other details in the construction of elements. It provides a larger set of possibilities for drawing than Graphics.

  • @So when using swing elements (like a Jpanel for example) it is recommended to use paintComponent?

  • Yes, because all that J has in front of it is the swing API, and it inherits from Jcomponent, which is the class that serves as a sort of base for all classes of swing components. If I’m not mistaken, the class itself paint started calling the paintComponent, then it makes more sense to overwrite the last one so as not to break the general drawing than if doing.

  • I do not know if my explanation was good, so I did not answer, but if you know English, this link is your complete answer to question 1: https://docs.oracle.com/javase/tutorial/uiswing/painting/closer.html The other answers your question 2: https://docs.oracle.com/javase/tutorial/2d/overview/index.html

  • I understand your explanation, thank you very much :D

Show 2 more comments

1 answer

1


  1. The method paint(Graphics) in components lightweight (Swing) performs these three things, in that order:

    • Paints the content of the component itself when calling the method paintComponent(Graphics).

    • Paints subcomponents when calling the method paintChildren(Graphics).

    • Paint the edge of the component when calling the method paintBorder(Graphics).


    The method paint(Graphics) also does a few more things to determine which area is to be painted. It also handles the case you are printing the component (to the printer), then calling the methods print*(Graphics) instead of paint*(Graphics). See all this on corresponding source code.

    How you will hardly want to alter this standard functioning of paint(Graphics) (there is little reason for this), it would hardly make sense to write such a method. So, write over the method paintComponent(Graphics) that is who makes the design of the component of fact.

  2. The class Graphics2D is a subclass of Graphics. She has several more methods, just take a quick look at the her javadoc to realize this. AWT will always pass an instance of Graphics2D for your method paintComponent(Graphics), so it’s safe to do the cast. If I’m not mistaken, the reason for this is historical, and the ideal would be that the extra methods had been added to the class Graphics instead of in a subclass.

See more about this on oracle tutorial, although the information there is presented in a disorganized way.

I also recommend looking at the my specialization TCC which ended in 2007, where I brought this up.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.