Net community,
I plan to write a program in Java in which you can set the background image. No errors occur and all "System.out" except "Painted" are output in the code. The background images are not displayed. Therefore I ask for your help. In addition, I originally planned to incorporate a background video (possibly as in Minecraft), but then found out that this is quite difficult to implement. If anyone has any idea about the video, feel free to write it down. My code:
Just looking:
Put these lines on comments and see what comes out.
mainPanel.setSize (getWidth (), getHeight () - getHeight () / 4);
mainPanel.setLocation (getLocation (). X, getLocation (). Y - getHeight () / 4);
You should take another look at the Swing documentation (Oracle Tutorials). Explicitly how to draw in swing.
1) Only call setVisible when all components have been assigned to the frame that is to be displayed. If new elements are added, the entire window must be redrawn. That should be avoided if possible.
2) The method of the base class in paint must be called first. Otherwise, what you draw will be painted over again.
Generally, if you want to draw your own components, you never overwrite the paint method. Instead, just overwriting paintComponent is enough.
Far from it, I list a few other things that I noticed spontaneously while flying over them that could be simplified.
1) Extend your Config class or create your own Util class that shows you these logics:
if (Boolean.getBoolean (c.getProperty ("MainFrameFullscreen" {
decreases for a conversion. In general, model and view are still closely related to you. It would make sense to separate the code according to tasks.
2) These calls:
getContentPane ()
you do not need. You can directly on the JFrame instance on getBackground etc. Access.
3) You do not need to explicitly define the layout for the frame. The BorderLayout is used by default for this component.
4) You could easily shorten your methods, thereby reducing code redundancies and deep nesting. For example, compare the lines from 87-108, 120-142 and 173-194. There's even a mistake: In block 2 you forget to add the new panel to the frame.
5) Don't set the panel size with setSize and also omit setLocation (doesn't change anything anyway). Instead, work with layouts and setPreferredSize.
I changed 1. And 2. And have now added the panels. Now "Painted" comes twice in the console. The problem is now, when I remove setSize and setLocation, "Painted" no longer comes.
Please post your current status again. I see no connection between the drawing and setSize / setLocation.
Since I forgot that in my answer, I add a supplement to:
In addition, I originally planned to incorporate a background video (…)
With http://capricasoftware.co.uk/projects/vlcj/ you can embed videos in a frame. An example implementation can be found here: https://stackoverflow.com/...java-swing
As soon as I see the "new JPanel () {…};" this set the size and location comes "Painted" if not then it does not come… But for both it is not drawn… That with the "setPrefferedSize ()" I do not know how and where. I've had a lot of problems with layouts lately because the sizes weren't right. I also tried WindowBuilder, but I couldn't attach everything to configuration data…
In your anonymous panel classes you add this method:
@Override
public dimension getPreferredSize () {
int height = MainFrame.this.getHeight ();
return new Dimension (MainFrame.this.getWidth (), height - height / 4);
}
and then you postpone setting the layout in the constructor
setLayout (new BorderLayout ());
further up, e.g. Right after setTitle.