0

I'm trying to make a visual novel using Java Swing, and so far it has been going smoothly. I'm using a JLabel that is attached to a JPanel to make the background image, and it starts good: image of a background behind a textbox + choice buttons, but when I try to update the image using Game.backgroundLabel.setIcon(newBackground);, it brings the background label to the very front: image of a background in front of the textbox, with only one of the three buttons showing. I'm new to the entirety of Java Swing, and mediocre at Java, but I'll try to include only the code that I believe to be relevant.

FROM THE MAIN CLASS (Game.java):

Container con;
JPanel backgroundLabel;
JLabel backgroundLabel;
// Creates background
backgroundPanel = new JPanel();
backgroundPanel.setBounds(0, 0, 800, 600);

backgroundLabel = new JLabel();

backgroundPanel.add(backgroundLabel);
con.add(backgroundPanel);

FROM A DIFFERENT CLASS (Story.java):

ImageIcon inCarBackground = new ImageIcon("C:\\Users\\kiwid\\eclipse-workspace\\FatuiBusiness\\backgrounds\\inCarBackground.png");
ImageIcon scaraSprite = new ImageIcon("C:\\Users\\kiwid\\eclipse-workspace\\FatuiBusiness\\sprites\\ScaraSprite.png");

This first update works, and the JPanel stays in the back where I added it in the Game.java class, as shown in the first image.

public void gameStart() {
position = "inCar00";
Game.nameLabel.setText("");
Game.mainTextArea.setText("My name is Lumine.  I’ve recently sided with the Fatui - a huge mafia\norganization - through Scaramouche, who I befriended in Teyvat\nUniversity.  I’m on a mission for the Fatui to steal a Blue Diamond\nring that once belonged to the Tsaritsa herself."); 
        
Game.backgroundLabel.setIcon(inCarBackground);
        
Game.choice1.setText("");
Game.choice2.setText(">");
Game.choice3.setText("");
}

However, this update seems to change the order of the JPanel, and brings it to the front of the screen.

public void inCar01() {
position = "inCar01";
Game.mainTextArea.setText("Here with me are Tartaglia, Scaramouche, and Mona.  Or, as I’m\nsupposed to call them here, Childe, Balladeer, and the Prophesizer.");
    
Game.backgroundLabel.setIcon(scaraSprite);
}

I've searched online for a solid 2 hours, but can't find anything good. I've read a little bit about JLayeredPane, but it seems like that revolves around user input, and when I try to use it, it says that I cannot add a JPanel to it.

Thank you all so much in advance! Please tell me if there's any other code I need to include, or if there's any code that I did not need to include (so I know for the next time I need help here).

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Kyo Lee
  • 1
  • 1
  • 2
    1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Aug 23 '21 at 20:26
  • 1
    .. 3) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have a keyboard shortcut specifically for formatting code. 4) `Game.mainTextArea.setText(..` implies either a poorly named instance of `Game` or a static `mainTextArea`. Don't do either. In the first case .. Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Aug 23 '21 at 20:27
  • 1
    You either use a background panel or a background label, but not both. If you want the label as a background then you need to set the layout of the label and add components to the label and get rid of the panel. If you want to use a background panel then you paint the image on the panel and add the components to the panel and get rid of the label. See: [Background Panel](https://tips4java.wordpress.com/2008/10/12/background-panel/) for more information. – camickr Aug 23 '21 at 21:11
  • Thank you for your comments - @AndrewThompson, my variables were static, and I've changed the code so that they aren't anymore. At the time of writing, it seemed like a simple way to get the variables over to another class - I've fixed this and learned why I can't just "static" any variable I want to directly manipulate in another class. I also do indent my code, I was just taking snippets of code, and it looks like the indentation didn't make its way through. – Kyo Lee Aug 23 '21 at 22:43
  • @camickr, Thank you for the Background Panel link! I had no idea that you wouldn't want to use both. – Kyo Lee Aug 23 '21 at 22:44

0 Answers0