0

I have trouble with my IT class project in NetBeans and jFrame. When I set the background image, there is a weird rectangle, after changing it via menu bar and menu item. There is no jPanel or other components that could cover it. This is how I set the background:

private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    Graphics x= this.getGraphics(); 
    Image img = null;
        try 
            {img = ImageIO.read(new File("noga1.jpg"));
            x.drawImage(img, 0, 0, rootPane);} 
        catch (Exception e) {}
    
}

And that's what we can see after performing an action:

running app after changing background

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 3
    Don't use `getGraphics`, this is not how custom painting works in Swing, see [Painting in AWT and Swing](https://www.oracle.com/java/technologies/painting.html) and [Performing Custom Painting](https://docs.oracle.com/javase/tutorial/uiswing/painting/index.html) for more details – MadProgrammer Mar 30 '22 at 19:28
  • 1
    See this [example](https://stackoverflow.com/questions/22162398/how-to-set-a-background-picture-in-jpanel/22162430#22162430) for nice long boring discussion on a number of ways you might achieve your result – MadProgrammer Mar 30 '22 at 19:31
  • Your menu bar doesn't look right. Typically the menu bar covers the complete width of the frame when you use the setJMenuBar(...) method. – camickr Mar 30 '22 at 19:42

0 Answers0