0 Members and 3 Guests are viewing this topic.
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 Game extends JPanel implements KeyListener { /* Game Panel class containing all functions */ JFrame mainFrame = new JFrame("Game"); 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 Game() { /* Defines the frame and the game panel */ this.setFocusable(true); this.addKeyListener(this); /* Fixed Layout frame */ mainFrame.setSize(852, 600); mainFrame.setResizable(false); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.getContentPane().add(this); // Add game panel to main frame /* 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 Game().run(); }}
david@davidbuntu:~/temp$ java Game Panel Width: 850Frame Width: 850Panel Width: 850Frame Width: 850Panel Width: 842Frame Width: 842Panel Width: 850Frame Width: 850david@davidbuntu:~/temp$ java Game Panel Width: 850Frame Width: 850Panel Width: 850Frame Width: 850david@davidbuntu:~/temp$ java Game Panel Width: 850Frame Width: 850Panel Width: 850Frame Width: 850david@davidbuntu:~/temp$ java Game Panel Width: 836Frame Width: 836Panel Width: 842Frame Width: 842Panel Width: 836Frame Width: 836david@davidbuntu:~/temp$
Dimension size = new Dimension(width, height); setSize(size); setMinimumSize(size); setMaximumSize(size); setPreferredSize(size);
import java.awt.Dimension;import javax.swing.JFrame;@SuppressWarnings("serial")public class MainFrame extends JFrame { public MainFrame(String string) { this.setTitle(string); Dimension size = new Dimension(850, 600); this.setSize(size); this.setMinimumSize(size); this.setMaximumSize(size); this.setPreferredSize(size); this.setResizable(false); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void run() { this.setVisible(true); }}
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(); }}
import javax.swing.JFrame;@SuppressWarnings("serial")public class MainFrame extends JFrame { public MainFrame(String string) { setTitle(string); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void run() { this.pack(); this.setVisible(true); }}