I have a jframe with a jpanel on top of it and the panel often has to change its contents with repaint() and revalidate(). I have managed to place the images, texts and buttons just the I way I want them to be on this jpanel. Everything is working, but now I am trying to set a background to the jframe, that does not interfere with the contents above it. For example, if there is a drawing of a tree, it should appear behind the text of the jpanel, without disrupting it. I found that the thing that semi works is to use setContentPane() on the jframe, adding a class, that has extended jpanel and has overrode paintComponent(). Everything appears on the screen, but the text is squashed vertically and the elements are moved towards the top of the frame.
If instead of using setContentPane() I just add the background class to the frame, it doesn't appear, no matter the setOpaque() of the jpanel.
I also tried using jLayeredPane, since the things I read on the internet suggest that this is the right answer. However, I couldn't make it work and the background remained hidden.
private final int WIDTH = 1024;
private final int HEIGHT = 768;
Frame()
{
JFrame frame = new JFrame();
panel = new JPanel();
gbc = new GridBagConstraints();
//Unrelated elements
//font = new Font(Font.MONOSPACED, Font.PLAIN, 20);
//border = BorderFactory.createEmptyBorder();
//imageResizer = new ImageResizer();
frame.setTitle("Shady Path");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setIconImage(new ImageIcon("res/human.png").getImage());
frame.setContentPane(new DrawPanel());
panel.setLayout(new GridBagLayout());
panel.setOpaque(false);
gbc.anchor = GridBagConstraints.PAGE_START;
frame.add(panel);
frame.setVisible(true);
}
//One of the two methods that change the contents of the jpanel
void appendMain(String mainImage, JTextArea mainText, JButton button)
{
panel.removeAll();
image = new JLabel(imageResizer.resize(200, 200, mainImage));
gbc.insets = new Insets(0, 0, 30, 0);
gbc.gridwidth = GridBagConstraints.REMAINDER;
panel.add(image, gbc);
formatText(mainText);
panel.add(mainText, gbc);
button.setFont(font);
button.setForeground(Color.WHITE);
button.setBackground(Color.BLACK);
button.setBorder(border);
gbc.fill = GridBagConstraints.VERTICAL;
gbc.insets = new Insets(50, 0, 70, 0);
panel.add(button, gbc);
panel.revalidate();
panel.repaint();
}
//This is for the text formating
private void formatText(JTextArea baseText)
{
baseText.setEditable(false);
baseText.setForeground(Color.WHITE);
baseText.setFont(font);
baseText.setLineWrap(true);
baseText.setWrapStyleWord(true);
baseText.setMargin(new Insets(0, 300, 0, 300));
baseText.setOpaque(false);
gbc.insets = new Insets(30, 0, 0, 0);
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
}
//The following code is for the paintComponent() class
//The imageResizer is another class that I made, but it just resizes images and it is unrelated.
public class DrawPanel extends JPanel
{
private Image image;
public DrawPanel()
{
ImageResizer imageResizer = new ImageResizer();
ImageIcon imageIcon = imageResizer.resize(1024, 768, "res/test.png");
image = imageIcon.getImage();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, this);
}
}
Answer
Well... It looks like @HovercraftFullOfEels was right with his comment. Literally, I just had to set the layout of the DrawPanel to a BorderLayout and everything was fixed.
public DrawPanel()
{
this.setLayout(new BorderLayout());
ImageResizer imageResizer = new ImageResizer();
ImageIcon imageIcon = imageResizer.resize(1024, 768, "res/test.png");
image = imageIcon.getImage();
}
No comments:
Post a Comment