Java - JFrame, JPanel, Layout and Clipping

I have three questions / concerns. (NOTE: I don't have enough reputation to post photos, so I linked them. And I needed to confuse them ...)

1) I created a panel that stores my game graphics (player area). It is assumed that the panel will be 800x800 and copy everything below and to the right. But when I add a graphic panel to the JFrame, it does not clamp. Thus, the images go right and left. This is a picture of how the game begins. Ideally, graphics will start in this rectangle all the time:

Picture # 1: http://i.stack.imgur.com/idL8f.png

Now, what happens when I click the game to start.

Photo # 2: http://i.stack.imgur.com/dxtbe.png

How to configure the panel / frame so that the graphics occupy 800x800 (for example, the first image) and everything else is cropped?

2) I'm a little confused about how I can customize JFrame. Here is how I want it to be laid out:

Photo # 3: http://i.stack.imgur.com/ZyJS5.png

How would you postpone JFrame / Panels? I thought BorderLayout, but I'm not sure if this will work.

3) For this game, my class, which extends JFrame, also contains main (). Is this a bad practice? ** Do you intend not to extend the JFrame to the main class?

+4
source share
1 answer

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; /** @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(); } }); } } 
+3
source

All Articles