1) The easiest way to get the 800x800 panel is to use setPreferredSize() and then pack() containing the JFrame . Conveniently, pack() "Makes this Window size fit the size and layout of its subcomponents."
2). See the Visual Guide for Layout Managers for layout suggestions. You can use nested panels to achieve your desired layout.
3). There is nothing wrong with a JFrame extension, but there is little point if you do not change the behavior of the JFrame . In contrast, JPanel is a convenient container for grouping components; It was designed to expand. You can study this example in this regard.
Application:
I don't want the panel to show anything but 800 pixels in the x and y directions.
You can override paintComponent() and copy any part of the image. In the example below, g.drawImage(img, 0, 0, null) draws the top left 800 pixels of the image, and g.drawImage(img, 0, 0, getWidth(), getHeight(), null) scales the image with the size of the panel. Note that f.setResizable(false) prevents window resizing.
Addendum: You can also copy arbitrary parts of the source image to arbitrary areas on the destination panel, as shown below.
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; 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) {
source share