Reputation: 49
public class GamePanel extends JPanel {
private int windowHeight = Toolkit.getDefaultToolkit().getScreenSize().height-37;
private int windowWidth = Toolkit.getDefaultToolkit().getScreenSize().width;
private int height = windowHeight / 6;
private int width = windowWidth;
private BufferedImage bg;
public GamePanel() {
setBounds(0, windowHeight / 5 * 4, windowWidth, windowHeight);
try {bg = ImageIO.read(new File("GUI Images/wood background.png"));}
catch (Exception e) {Utilities.showErrorMessage(this, e);}
setVisible(true);
//add buttons
JButton InventButton = new JButton("Inventory");
InventButton.setOpaque(false);
InventButton.setBorderPainted(false);
InventButton.setContentAreaFilled(false);
InventButton.setVerticalTextPosition(SwingConstants.CENTER);
InventButton.setHorizontalTextPosition(SwingConstants.CENTER);
InventButton.setFont(new Font("TimesRoman", Font.PLAIN, 20));
InventButton.setBounds(width/6*5,0,width/6,height/3);
//get button texture
Image i1 = new ImageIcon("GUI Images/Button.png").getImage().getScaledInstance
(InventButton.getWidth(),InventButton.getHeight(),java.awt.Image.SCALE_SMOOTH);
InventButton.setIcon(new ImageIcon(i1));
InventButton.setForeground(Color.white);
InventButton.addActionListener(e -> {
});
JButton PartyButton = new JButton("Party");
PartyButton.setOpaque(false);
PartyButton.setBorderPainted(false);
PartyButton.setContentAreaFilled(false);
PartyButton.setVerticalTextPosition(SwingConstants.CENTER);
PartyButton.setHorizontalTextPosition(SwingConstants.CENTER);
PartyButton.setFont(new Font("TimesRoman", Font.PLAIN, 20));
PartyButton.setBounds(width/6*5,height/3,width/6,height/3);
PartyButton.setIcon(new ImageIcon(i1));
PartyButton.setForeground(Color.white);
PartyButton.addActionListener(e -> {
});
JButton MenuButton = new JButton("Menu");
MenuButton.setOpaque(false);
MenuButton.setBorderPainted(false);
MenuButton.setContentAreaFilled(false);
MenuButton.setVerticalTextPosition(SwingConstants.CENTER);
MenuButton.setHorizontalTextPosition(SwingConstants.CENTER);
MenuButton.setFont(new Font("TimesRoman", Font.PLAIN, 20));
MenuButton.setBounds(width/6*5,height/3*2,width/6,height/3);
MenuButton.setIcon(new ImageIcon(i1));
MenuButton.setForeground(Color.white);
MenuButton.addActionListener(e -> {
});
add(MenuButton);
add(InventButton);
add(PartyButton);
revalidate();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bg,0,0,width,height, null);
g.dispose();
}
}
I've asked essentially the same question question already and was told that the reason that my background was painting over my JButtons is because I never called super.paintComponent(g), but I've since learned. Except now I've managed to break my new code as well as my old code which worked for a brief period of time. It seems that my code works as soon as I remove g.dispose(); Does anybody know why the buttons don't seem to paint properly?
Upvotes: 2
Views: 136
Reputation: 285405
You state:
It seems that my code works as soon as I remove g.dispose();
So then remove it.
You should never never dispose of a Graphics object given to you by the JVM. The first thing you should do is get the g.dispose()
call out of your paintComponent(...)
method. If you copy the Graphics object and paint with the copy, or if you paint with a Graphics object obtained from a BufferedImage, then yes, dispose of them to save on resources, but if you dispose of the JVM's Graphics object you risk painting problems down stream when the JVM tries to continue using that Graphics object to paint other components.
Upvotes: 3