0

I created a user interface that looks something like this:

panel

https://i.stack.imgur.com/oBNSZ.jpg

+You can ignore the background pic. The issue is that the 2 JLabel's have white backgrounds. I've been working for a while using setOpaque() on the labels to false, the panel too, editing the setBackground(), and more. My goal is to have the background behind both JLabel's to be black and semi-transparent. Any suggestions?

The idea with this code is that whenever a UserInterface() object is created, the startScreen() can create a JPanel and return it to the constructor, and apply it to the JFrame. --Keep in mind this class is not complete and part of a larger program.

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

public class UserInterface extends JFrame{

private final int WINDOW_WIDTH = 1250;
private final int WINDOW_HEIGHT = 750;

public UserInterface(){
    super("idk");
    setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setLocationRelativeTo(null);
    add(startScreen());
    setVisible(true);
}//UserInterface

/*
public JPanel gameScreen(){

}//gameScreen
*/

public JPanel startScreen(){

    JPanel backgroundPanel;
    backgroundPanel = new JPanel(null);//using absolute positioning
    //backgroundPanel.setBackground(new Color(15, 15, 15));
    backgroundPanel.setOpaque(false);

    JLabel title;
    title = new JLabel("idk");
    title.setOpaque(false);
    //title.repaint();
    title.setBackground(new Color(15, 15, 15));
    title.setFont(new Font("Consolas", Font.PLAIN, 250));
    title.setHorizontalAlignment(SwingConstants.CENTER);
    title.setVerticalAlignment(SwingConstants.TOP);
    title.setBounds(WINDOW_WIDTH / 4, WINDOW_HEIGHT / 20, (WINDOW_WIDTH / 4) * 2, WINDOW_WIDTH / 5);

    JLabel trademark;
    trademark = new JLabel("Eric Parsons");
    trademark.setFont(new Font("Edwardian Script ITC", Font.PLAIN, 60));
    trademark.setBounds(10, (WINDOW_HEIGHT / 20) * 17, 280, 80);

    JButton begin;
    int beginSizeX = 350;
    int beginSizeY = 50;
    begin = new JButton("Begin");
    begin.setBounds((WINDOW_WIDTH / 2) - (beginSizeX / 2),
            (WINDOW_HEIGHT / 2) - (beginSizeY / 2), beginSizeX, beginSizeY);
    begin.setBackground(new Color(160, 160, 160));
    begin.setFont(new Font("Consolas", Font.PLAIN, 25));
    begin.setFocusable(false);

    backgroundPanel.add(title);
    backgroundPanel.add(begin);
    backgroundPanel.add(trademark);

    titlePageBackground.ScrollingBackground background = new titlePageBackground.ScrollingBackground();
    background.setFocusable(true);
    backgroundPanel.add(background);
    return backgroundPanel;

}//startScreen
}//UserInterface

I feel the issue is the order where I add the objects, but I don't know where I've gone wrong. I appreciate all the comments and help!

__ Update: Changing setOpaquq() doesn't help issue for both panel and label

Eric
  • 444
  • 7
  • 19
  • By default, JLabel opaque is set to false. If you want the background to be visible setOpaque(true) and set the background. – Dakshinamurthy Karra May 06 '16 at 07:00
  • Lets say I wanted that background to be semi transparent. I have an image set on the JFrame, doing that doesnt help make the JLabel background semi transparent... – Eric May 06 '16 at 07:09
  • Use Color() constructor that takes 4 parameters, RGB and A (for alpha). 0 is fully transparent and 255 is opaque. – Dakshinamurthy Karra May 06 '16 at 07:11
  • I really do believe that works, the issue is because the JFrame has the image over it, and i'm placing a JLabel over the (moving) image (made a scroller program), the label ignores the images and uses the JFrame background. The semi transparency seems to work, but behind is the JFrame background, not image. Any thoughts? – Eric May 06 '16 at 07:16
  • I think the problem is to do with your repaint of Frame or the Z-Order of the components. – Dakshinamurthy Karra May 06 '16 at 07:34
  • *"Lets say I wanted that background to be semi transparent"* - Swing doesn't know how to handle alpha based colors on opaque components, you need to fake it – MadProgrammer May 06 '16 at 08:23
  • That's unfortunate. I guess I'll work around this some other way. Thanks for your guys' help. – Eric May 06 '16 at 08:58

0 Answers0