- The easiest way to get an 800x800 panel is to use
setPreferredSize() and then pack() the JFrame that contains is. Conveniently, pack() "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."
2). See A Visual Guide to Layout Managers for layout suggestions. You can use nested panels to achieve your desired layout.
3). There's nothing wrong with extending JFrame, but there's little point unless you are modifying the behavior of JFrame. In contrast, JPanel is a convenient container for grouping components; it was designed to be extended. You might examine this example in that regard.
Addendum:
I don't want the panel to show anything but the 800 pixels in the x and y direction.
You can override paintComponent() and copy whatever portion of the image is desired. In the example below, g.drawImage(img, 0, 0, null) draws the top-left 800 pixels of the image, while g.drawImage(img, 0, 0, getWidth(), getHeight(), null) scales the image the panel's size. Note that f.setResizable(false) prevents changing the window's size.
Addendum: You can also copy arbitrary portions of the source image to arbitrary areas of the
the destination panel, as shown below. Also consider overriding getPreferredSize(), as suggested here.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** @see http://stackoverflow.com/q/3851847 */
public class MyPanel extends JPanel {
private BufferedImage img;
public MyPanel() {
this.setPreferredSize(new Dimension(800, 800));
try {
img = ImageIO.read(new File("../scratch/image.png"));
} catch (IOException ex) {
ex.printStackTrace(System.err);
}
}
@Override
protected void paintComponent(Graphics g) {
// g.drawImage(img, 0, 0, 800, 800, null);
// g.drawImage(img, 0, 0, getWidth(), getHeight(), null);
g.drawImage(img, 0, 0, 800, 800, 0, 0, 800, 800, this);
}
private void display() {
JFrame f = new JFrame("MyPanel");
// f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MyPanel().display();
}
});
}
}