So I have a JPanel
which is added to a JFrame
.
The JPanel
includes a large graphic, and the main part that I wish to display is 800 x 400
pixels.
There is extra on either sides, which doesn't matter.
When I resize the JFrame
which contains the inner JPanel
, is there a way to resize the JPanel
as well so that those important pixels are still shown?
Here's what I tried:
JFrame frame = new JFrame("Simon");
Renderer renderer = new Renderer(); //this object extends JPanel, which is what i'm adding to the frame
frame.setLayout(new BorderLayout());
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(true);
renderer.setPreferredSize(new Dimension(WIDTH, HEIGHT)); //want to keep it centered on this width/height because there is important content within there.
frame.add(renderer, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
This is not doing anything different to it than normal.
Any advice?
Edit: Adding the renderer class:
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class Renderer extends JPanel
{
/* (non-Javadoc)
* @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
Simon.simon.paint((Graphics2D) g); //Simon is the first class that holds the code all the way above
}
}