46
Computer Programming / Re: Java Panel/Frame has different sizes
« on: September 14, 2011, 10:29:39 pm »
I have added the code I suggested, but for the JPanel, to your original code, and it works fine for me.
I had a similar problem to this when I wrote my own graphics engine, but it was around 5 years ago, so the solution is pretty hazy. If I remember correctly, it could be that the width is including other items in the JFrame, such as the surrounding border. However I don't know for sure. For safety I'd concentrate on checking the size of your inner JPanel, as this is the component where the size really matters, and not care what the outer frame is using.
I also added a call to pack, to ensure it's all packed down to the size you want.
Code: [Select]
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class MainFrame extends JPanel implements KeyListener {
/* Game Panel class containing all functions */
private JFrame mainFrame;
private boolean needs_repaint;
public void paintComponent(Graphics g) {
/* Draws the elements on the screen */
super.paintComponent(g);
System.out.printf("Panel Width: %d\n", this.getWidth());
System.out.printf("Frame Width: %d\n", this.getParent().getWidth());
/* Repainting has finished */
needs_repaint = false;
}
public void update() {
/* Main game loop */
/* Always repaint */
//this.repaint();
}
public void run() {
/* Displays the frame, start the game */
mainFrame.setVisible(true);
this.requestFocus();
}
public MainFrame() {
/* Defines the frame and the game panel */
this.setFocusable(true);
this.addKeyListener(this);
/* Fixed Layout frame */
Dimension size = new Dimension(850, 600);
this.setSize(size);
mainFrame = new JFrame("Game");
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.getContentPane().add(this); // Add game panel to main frame
mainFrame.pack();
/* Start timer with main game loop */
Timer timer = new Timer(25, new ActionListener() {
public void actionPerformed(ActionEvent e) {
update();
}});
timer.start();
}
/* Game Class is not abstract, and these functions are not needed */
public void keyPressed(KeyEvent key) {}
public void keyReleased(KeyEvent arg0) {}
public void keyTyped(KeyEvent arg0) {}
public static void main(String[] args) {
new MainFrame().run();
}
}
I had a similar problem to this when I wrote my own graphics engine, but it was around 5 years ago, so the solution is pretty hazy. If I remember correctly, it could be that the width is including other items in the JFrame, such as the surrounding border. However I don't know for sure. For safety I'd concentrate on checking the size of your inner JPanel, as this is the component where the size really matters, and not care what the outer frame is using.
I also added a call to pack, to ensure it's all packed down to the size you want.