Should all graphical objects inherit from JPanel?

I have a problem with Java Swing and JPanel. I could say that I am an inexperienced Java programmer, but not entirely new to programming.

My problem is that I have a class called RandomParticle ,

 public class RandomParticle extends Ellipse2D.Double { private Ellipse2D.Double circle; public RandomParticle(double xPos , double yPos,double rad) { setCircle(new Ellipse2D.Double(xPos, yPos, rad, rad)); } public Ellipse2D.Double getCircle() { return circle; } public void setCircle(Ellipse2D.Double circle) { this.circle = circle; } } 

which I want to move to the screen (of course, in a JFrame). A particle is the default object to simply raise graphics. However, in many places it is said, for example, that in order to draw a circle, he must override the paintComponent method in JPanel. It really makes sense that in order to draw something in a JFrame, it must be a JPanel. However, this can cause problems because Java does not support multiple inheritance.

In this case, I do not need RandomParticle be a child of ellipse2D, but as my classes become more complex, at some point I will most likely need some other class inherent. However, I felt that this question level has a good level for SO, since it does not require any specific knowledge about my program. So I wonder: Is it possible to draw a circle without the class being a child of JPanel?

+4
source share
1 answer

Is it possible to draw a circle without the class being a child of JPanel?

Kind, but this was due to problems. I will try to explain

If you want to do custom paint in Swing, you must override the paintComponent method of any component that extends from JComponent , JPanel is usually the easiest solution, since working with pictures

See Painting in AWT and Swing and Performing Custom Painting for more details on how painting works.

Generally speaking, it is impractical to try to draw directly into a top-level container, such as a JFrame , since it contains a number of other child components that are drawn on top of it ( JRootPane , contentPane , glassPane , menuBar , etc.) that can be painted regardless of the frame. cause some strange artifacts of painting.

A more detailed idea of ​​what a JFrame looks JFrame ...

Rootpane

JFrame also not a double buffer, which means that if you try to draw it directly, trying to quickly update it, it will cause flickering. JComponent , and its decients are duplicated by default

Swing itself uses a passive rendering mechanism, which means that you actually do not control the drawing process, you are simply notified when your component should be painted (using the paintComponent method). Swing can choose to draw child components without having to notify the parent container, which is one of the reasons paintComponent recommended. It can also combine multiple paint requests with as few calls as possible to optimize the process.

All this is done without your knowledge or control.

If you want more control over the drawing process, you can consider using BufferStrategy , which gives you direct (double buffered) control of the drawing process, so you can decide when and what should be painted. But even this will require you to use java.awt.Canvas as a base with which you can create your own output.

As a rule, it is usually not recommended to spread from JFrame (or most top-level containers) and instead use one of the container classes as the base class. This way you get much more freedom about how they should be displayed or reused, adding them to what has ever been another container.

+8
source

All Articles