Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8203941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:43:59+00:00 2026-06-07T07:43:59+00:00

I have a JLayeredPane that adds three objects, the problem is the backgroundImage when

  • 0

imgI have a JLayeredPane that adds three objects, the problem is the backgroundImage when the app runs there is a gap between the jframe and the background image from the top which i set to (0,0) no matter what i change the gap is still there (I want to rid of the gap). If i copy the backgroundImage class and put it in another file/class the gap is not there. can anyone help?

public class NavigationMenu extends JPanel {

private ArrayList<String> Menu = new ArrayList();
private int MenuSize;
private int MenuChannel = 0;
private Font realFont;
private static Dimension screenSize = new Dimension(new ScreenDimensions().getScreenWidth(), new ScreenDimensions().getScreenHeight());
private static final int MENU_HEIGHT = (int)(new ScreenDimensions().getScreenHeight()*0.1);
private static final int MENU_WIDTH = new ScreenDimensions().getScreenWidth();
private static final int MENU_CENTER = (new ScreenDimensions().getScreenHeight() / 2) - (MENU_HEIGHT / 2);

public NavigationMenu() {

    //Add Menu Channels
    setPreferredSize(screenSize);
    setBounds(0,0,MENU_WIDTH,screenSize.height);
    this.Menu.add("MOVIES");
    this.Menu.add("MUSIC");
    this.Menu.add("PICTURES");
    this.Menu.add("VIDEOS");
    this.Menu.add("TV SHOWS");
    this.Menu.add("WEATHER");
    this.Menu.add("RADIO");
    this.Menu.add("SETTINGS");
    this.MenuSize = Menu.size() - 1;

    JLayeredPane pfinal = new JLayeredPane();
    JPanel _menuText = new menuText();
    JPanel _backgroundImage = new backgroundImage("Backgrounds/curtains.png");
    JPanel _bar = new bar();

    pfinal.setPreferredSize(screenSize);
    pfinal.setBounds(0,0,MENU_WIDTH,screenSize.height);

    pfinal.add(_backgroundImage, new Integer(1));
    pfinal.add(_bar, new Integer(2));
    pfinal.add(_menuText, new Integer(3));

    add(pfinal);
}

public class bar extends JPanel {
    public bar() {
        setBounds(0,MENU_CENTER,MENU_WIDTH, MENU_HEIGHT);
        setOpaque(false);
    }
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f));
        g2d.setColor(Color.BLACK);
        g2d.fillRect(0,0,screenSize.width,MENU_HEIGHT);
    }
}

public class menuText extends JPanel {

    public menuText() {
        setBounds(0,MENU_CENTER,MENU_WIDTH,MENU_HEIGHT);
        setOpaque(false);
        setFocusable(true);
        requestFocusInWindow();

        try {
            realFont = new RealFont(((int)(MENU_HEIGHT * 0.80))).getRealFont();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                //JOptionPane.showMessageDialog(null, "OK");
                int keyCode = e.getKeyCode();
                    switch (KeyEvent.getKeyText(keyCode)) {
                        case "Up":
                            //JOptionPane.showMessageDialog(null, "Up");
                            if(MenuChannel < MenuSize) {
                                MenuChannel++; 
                                repaint();
                            } else {
                                MenuChannel = 0; //reset
                                repaint();
                            }
                        break;
                        case "Down":
                            if(MenuChannel == 0) {
                                MenuChannel = MenuSize + 1;
                                MenuChannel--;
                                repaint();
                            } else {
                                MenuChannel--;
                                repaint();
                            }   
                        break;
                    }
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        String MenuString = Menu.get(MenuChannel);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //g2d.setBackground(Color.PINK);
        //g2d.fillRect(0,0,getWidth(),getHeight());
        g2d.setColor(Color.WHITE);
        g2d.setFont(realFont);
        FontRenderContext context = g2d.getFontRenderContext();
        java.awt.geom.Rectangle2D rect = realFont.getStringBounds(MenuString, context);
        int textHeight = (int)rect.getHeight();
        int textWidth = (int)rect.getWidth();
        int panelHeight = getHeight();
        int panelWidth = getWidth();
        int x = (panelWidth - textWidth) / 2;
        int y = (panelHeight - textHeight) / 2;
        //System.out.println("f:"+ fontMetrics.gettH:" + textHeight + "\ntW:" + textWidth + "\npH:" + panelHeight + "\npW:" + panelWidth);
        g2d.drawString(MenuString,x,(float)-rect.getY()+2);
    }
}

public class backgroundImage extends JPanel {

    private Image icon;
    private int screenWidth = new ScreenDimensions().getScreenWidth();
    private int screenHeight = new ScreenDimensions().getScreenHeight();

    public backgroundImage(String background) {
        icon = new ImageIcon(background).getImage();
        Dimension size = new Dimension(screenWidth,screenHeight);
        setPreferredSize(size);
        setMinimumSize(size);
        setMaximumSize(size);
        setSize(size);
        setLayout(null);

    }

    @Override
    protected void paintComponent(Graphics g) {
        g.drawImage(icon,0,0,screenSize.width,screenSize.height,null);
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:44:01+00:00Added an answer on June 7, 2026 at 7:44 am

    Change the NavigationMenu’s layout to a BorderLayout

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
I have the following: import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JLayeredPane; import javax.swing.JFrame; import javax.swing.BorderFactory;
I need to have a JToggleButton (that has custom background) that contains a JPanel
I have some JInternalFrames in a JLayeredPane. Assume that the JInternalFrames are in the
Have advancements in CPU design like dynamic instruction scheduling narrowed the performance gap between
Have an app that can use tts to read text messages. It can also
Have a rails app that is supposed to display a list of products/managers. After
I have built a simple interface that fills my entire JFrame. I am using
I have a problem with my JLayeredPane , I am probably doing something incredibly
I'm using a custom JLayeredPane. I have several Shapes which needed to be drawn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.