My question is WHY is the same swing-custom-painting routine nearly 16 times faster when drawing on a JPanel compared to directly on the JFrame? Is it just double-buffering? It can't be, surely?
Background: I had a problem with the custom-painting not being refreshed when the JFrame was unobscured (especially having been only partially obscured). After searching SO I decided to bite-the-bullet and figure-out how to wire a subclass of JPanel into a bluddy-NetBeans-form-designer form.
For anyone in the same situation: In NetBeans you need to create a new standard class (NOT a JPanel form) that just happens to extend JPanel, and manually code everything there-in (no GUI designer, like the good-ole-days, sigh). Then you add a standard JPanel to your form, set it's size; then right-click and select "Customize code" and select "custom creation" in the combo-box... where it creates a new javax.swing.JPanel substitute your subclass thereof.
So... This allowed me to "do it properly" and paint on a component, instead of directly on the form. Also, the panels-key-listener a much neater solution than hi-jacking the frames key-event-dispatcher.
Anyway, now the profiler says that EXACTLY the same custom-painting-code executes nearly 16 times faster in JPanel's paintComponent() as apposed JFrame's paint()... and I was wondering if anybody could please explain why.
Thank you in advance. Keith.
EDIT: This question is based on MISINTERPRETED METRICS. The profiler does not include/report the JPanel's paintComponent() method in the AWT-EventQueue thread, where-as my baseline profile does include JFrame's paint(). I should have looked more carefully before asking a silly question. My bad.
1. JPanel serves as a general purpose container, while JFrame is a window commonly used for stand-alone applications, like a warning window, or a notification window. 2. JPanel represents an area used for more complex operations or applications.
An explanation: To write or draw in a frame, we need to override the paintComponent() method in JPanel. To override means we have to inherit from JPanel.
With the exception of top-level containers, all Swing components whose names begin with "J" descend from the JComponent class. For example, JPanel , JScrollPane , JButton , and JTable all inherit from JComponent . However, JFrame and JDialog don't because they implement top-level containers.
These classes are imported and then used in many GUI applications. We use the last, Graphics, to render visuals (e.g., figures, pictures, and even text) in a JPanel that contains the graphics part of a JFrame.
JFrame is a top level container extending aw.Frame that needs native resources for drawing whereas a JPanel is a swing component that is rendered by the UI thread itself.
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