So im using the this.getContentPane().getSize(); to set a Dimension variable called borderlessSize (because its supposed to contain the size of the frame excluding the borders) and then im setting an int borderlessWidth and an int borderlessHeight, to borderlessSize.getWidth/Height. However when i print out borderlessWidth, and borderlessHeight, theyre both showing up as 0?? please help!! Heres my code.
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import javax.swing.JFrame; public class Frame extends JFrame { int xCoord = 50; int yCoord = 50; private final int ovalWidth = 25; private final int ovalHeight = 25; private final int WIDTH = 800; private final int HEIGHT = WIDTH / 5 * 4; private Dimension borderlessSize = this.getContentPane().getSize(); private final int borderlessWidth = (int)borderlessSize.getWidth(); private final int borderlessHeight = (int)borderlessSize.getHeight(); private Image dbImage; private Graphics dbG; private Dimension d = new Dimension(WIDTH, HEIGHT); Frame() { addKeyListener(new AL()); setTitle("Frame"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(d); setResizable(false); setVisible(true); setBackground(Color.WHITE); System.out.println(borderlessWidth + " " + borderlessHeight); } public void paint(Graphics g) { dbImage = createImage(getWidth(), getHeight()); dbG = dbImage.getGraphics(); paintComponent(dbG); g.drawImage(dbImage, 0, 0, this); } public void paintComponent(Graphics g) { g.fillOval(xCoord, yCoord, ovalWidth, ovalHeight); repaint(); } public class AL extends KeyAdapter { public void keyPressed(KeyEvent e) { int keyCode = e.getKeyCode(); if(keyCode == e.VK_LEFT) { if(xCoord <= 0) { xCoord = 0; } else { xCoord += -15; } } if(keyCode == e.VK_RIGHT) { if(xCoord >= borderlessWidth - ovalHeight) { xCoord = borderlessWidth - ovalHeight; } else { xCoord += 15; } } if(keyCode == e.VK_UP) { if(yCoord <= 0) { System.out.println("top collision"); yCoord = 0; } else { yCoord += -15; } } if(keyCode == e.VK_DOWN) { if(yCoord >= borderlessHeight - ovalHeight) { yCoord = borderlessHeight - ovalHeight; } else { yCoord += 15; } } } } }