0

I have a strange white stripe (see below) appearing on top of my background image. The code is quite simple. How to get rid of the white stripe?

enter image description here

//Graphics side of the game
public class GUI extends JFrame {

    private final int larghezza = 1280;
    private final int altezza = 720;
    private final String name = "Sette e Mezzo";

    private final ImageIcon backgroundImage;
    private JLabel bgImageLabel;
    private JPanel backgroundPanel, borderLayoutPanel, topGridLayout, botGridLayout;

    public GUI () {
        backgroundImage = new ImageIcon ("assets/background.png");

        bgImageLabel = new JLabel (backgroundImage);

        //Panels
        borderLayoutPanel = new JPanel (new BorderLayout ());
        topGridLayout = new JPanel (new GridLayout (1, 3));
        botGridLayout = new JPanel (new GridLayout (1, 3));
        backgroundPanel = new JPanel ();
        backgroundPanel.add (bgImageLabel);

        //Frame
        this.setName (name);
        this.setPreferredSize (new Dimension(larghezza, altezza));
        this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

        //Adding to frame and panels
        borderLayoutPanel.add (topGridLayout, BorderLayout.NORTH);
        borderLayoutPanel.add (botGridLayout, BorderLayout.SOUTH);

        this.add (borderLayoutPanel);
        this.add (backgroundPanel);

        this.pack ();
        this.setLocationRelativeTo (null);  
        this.setVisible (true);
    } 
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Alex Leonardi
  • 71
  • 1
  • 12
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). (For example, to make it complete the code above needs imports and a main method) 2) *"`new ImageIcon ("assets/background.png");`"* 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). – Andrew Thompson May 13 '17 at 09:38
  • .. 3) BTW - that's not white, it is RGB (255,250,254). – Andrew Thompson May 13 '17 at 09:43

1 Answers1

1

Don't use setPreferredSize() when you really mean to override getPreferredSize(). In this case, the specified Dimension probably doesn't quite match the size of "assets/background.png". This allows some portion of another panel to show, perhaps backgroundPanel.

In the example below,

  • The default layout of JPanel is FlowLayout, which has a "default 5-unit horizontal and vertical gap." A touch of Color.blue makes the gap stand out; resize the enclosing frame to see the behavior.

  • As the default layout of JFrame is BorderLayout, you may not need borderLayoutPanel at all.

  • Because the two GridLayout panels have no content, they remain invisible. Add content to each or override getPreferredSize() in each to see the effect.

  • Construct and manipulate Swing GUI objects only on the event dispatch thread.

image

import java.awt.*;
import java.net.URL;
import javax.swing.*;

public class GUI {

    private static final String TITLE = "Title";
    private static ImageIcon IMAGE_ICON;

    private void display() {
        //Panels
        JPanel topGridLayout = new JPanel(new GridLayout(1, 3));
        JPanel botGridLayout = new JPanel(new GridLayout(1, 3));
        JPanel backgroundPanel = new JPanel();
        backgroundPanel.setBackground(Color.blue);
        backgroundPanel.add(new JLabel(IMAGE_ICON));

        //Frame
        JFrame f = new JFrame(TITLE);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add components
        f.add(topGridLayout, BorderLayout.NORTH);
        f.add(backgroundPanel);
        f.add(botGridLayout, BorderLayout.SOUTH);

        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) throws Exception {
        IMAGE_ICON = new ImageIcon(new URL("http://i.imgur.com/mowekvC.jpg"));
        EventQueue.invokeLater(new GUI()::display);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045