0

I have put a background image using "paint" and as I've tried to add a Jlabel it is not showing upon running. Here's my code and screenshots.

Output

public class startingPage extends JFrame {
private JPanel contentPane;
private String audioFileName = "bg-music.wav";
private String imagePath = "truck_850x300_mining_island.png";
private String logo = "explore_with_mine.png";

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                startingPage frame = new startingPage();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public startingPage() {
    
    
    
    initComponents();
    createEvents();
    playAudio();

    
    
    
}

public void playAudio() {
    // TODO Auto-generated method stub
    
    try 
    {
    File audioPath = new File(audioFileName);
    programManager.stream = AudioSystem.getAudioInputStream(audioPath);
    programManager.clip = AudioSystem.getClip();
    programManager.clip.open(programManager.stream);
    programManager.clip.start();
    }
    
    catch(Exception e)
    {
        e.printStackTrace();
    }
    
}

public void createEvents() {
    
    
    
}

public void initComponents() {
    
    setTitle("Explore With Mine");
    setIconImage(Toolkit.getDefaultToolkit().getImage(startingPage.class.getResource("/exploreWithMine/resources/logo.png")));
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setBounds(400, 200, 720, 480);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    
    JButton btnNewButton = new JButton("START");
    btnNewButton.setBounds(262, 299, 177, 58);
    btnNewButton.setBackground(Color.LIGHT_GRAY);
    btnNewButton.setForeground(new Color(165, 42, 42));
    btnNewButton.setFont(new Font("Tahoma", Font.BOLD, 42));
    contentPane.add(btnNewButton);
    
    JLabel lblNewLabel = new JLabel("TESTING");
    lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
    lblNewLabel.setBounds(10, 10, 146, 43);
    contentPane.add(lblNewLabel);
    
    
    
    
    
    
}

public void paint(Graphics g) 
{
    
    g.drawImage(new ImageIcon(imagePath).getImage(),
            -70,90, null);
    

    
}
}

OUTPUT JLABEL not Showing (Upper Left)

I have tried removing the painted background image and it worked however I also need that background image to be there.

OUTPUT

  • "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod Nov 18 '22 at 17:54
  • Possiblele duplicate of [_How do I set a JLabel's background color?_](https://stackoverflow.com/q/2380314/230513) – trashgod Nov 18 '22 at 17:57
  • `new ImageIcon(imagePath)` … you will want to read about how [current directories and relative file paths work.](https://en.wikipedia.org/wiki/Working_directory) – VGR Nov 19 '22 at 03:46

0 Answers0