0

I am making a game and the game window will only open to the size of the title. Please let me know what changes I need to make the "b.setSize(900,885)" work. I will change or add any amount of code if needed.

 import java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.Rectangle;
 import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.KeyEvent;
 import java.awt.event.KeyListener;

 import javax.swing.*;


 public class Background extends JFrame implements KeyListener,    ActionListener{
 int platform1X = 300, platform1Y = 555, platform2X, platform2Y;
 Image image;
 public boolean isJumping = false;
 int playerx = 100, playery = 585, velX = 0, velY = 0;
 public boolean[] keyDown = new boolean[4];
 private int fallingSpeed = 0;
 private int gravity = 5;
 private int jumpPower = -30;


 public Background(){

 addKeyListener(this);
 setFocusable(true);
 setFocusTraversalKeysEnabled(false);
 keyDown[0] = false;
 keyDown[1] = false;
 keyDown[2] = false;
 keyDown[3] = false;

 }
  public void paint(Graphics g){
 if(!isOnGround()){
 fall();
}
 ImageIcon i = new ImageIcon("/Users/kairotieremorton/Documents/java   code/Kairo and Enoch Game/src/images/Backgroundsquareman.png");
image = i.getImage();
g.drawImage(image, 0, 0, 900, 700, null);
g.setColor(Color.BLUE);
g.fillRect(platform1X, platform1Y, 120, 20);
g.setColor(Color.RED);
g.fillRect(playerx, playery, 30, 30);
try {
Thread.sleep(40);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} 


repaint();
playery = playery + velY;
playerx = playerx + velX; 
}



public static void main(String[] args){
Background b = new Background();
b.setTitle("Game");
b.setSize(900,885);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.setVisible(true);
}



public void keyPressed(KeyEvent e) {
int key = e.getKeyCode(); 
if(isOnGround() && key == KeyEvent.VK_SPACE){
 keyDown[0] = true; 

 jump();

 }
 //if(key == KeyEvent.VK_DOWN){ setvelY(5); keyDown[1] = true;}
 if(key == KeyEvent.VK_LEFT) { setvelX(-5); keyDown[2] = true;}
 if(key == KeyEvent.VK_RIGHT) { setvelX(5); keyDown[3] = true;}
 System.out.println(key);
 System.out.println(playerx);
 System.out.println(playery);
 }

 public void keyReleased(KeyEvent e) {
 int key = e.getKeyCode();

 if(key == KeyEvent.VK_SPACE){ keyDown[0] = false;}    //tempObject.setVelY(0);
//if(key == KeyEvent.VK_DOWN) keyDown[1] = false;//tempObject.setVelY(0);
 if(key == KeyEvent.VK_LEFT) keyDown[2] = false;//tempObject.setVelX(0);
 if(key == KeyEvent.VK_RIGHT) keyDown[3] = false; //tempObject.setVelX(0);

 //vertical movement
 if(!keyDown[0]) setvelY(0); System.out.println(isOnGround());

//horizontal movement
if(!keyDown[2] && !keyDown[3]) setvelX(0);
 }
public void keyTyped(KeyEvent e) {}

public void actionPerformed(ActionEvent e) {
 //playery = playery + velY;
 //playerx = playerx + velX; 
 //repaint();
 }
 public boolean isOnGround(){
 if(playery + (30 /2) >= 585) {
     return true;
 }
 return false;

 }

 public void fall(){
 playery = playery + fallingSpeed;
 fallingSpeed = fallingSpeed + gravity;

 }
 public void jump(){
 fallingSpeed = jumpPower;
 fall();
 }

 public void setvelX(int velX) {
 this.velX = velX;
 }
 public void setvelY(int velY) {
 this.velY = velY;
 }
 public float getvelX(){
 return velX;
 } 
 public float getvelY(){
 return velY;
 }
 public int getplayerx(){
 return playerx;
 }
 public int getplayery(){
 return playery;
 }
 public void setplayerx(int playerx) {
 this.playerx = playerx;
}
public void setplayery(int playery) {
this.playery = playery;
}
public Rectangle getBounds(){
return new Rectangle(getplatform1X(), platform1Y(), 120, 20);
}
public int getplatform1X(){
return platform1X;
}
public int platform1Y(){
return platform1Y;
}

public Rectangle getBoundsTop(){
return new Rectangle(getplayerx() + 10, getplayery(), 30 - 20, 5);
}
public Rectangle getBoundsBottom(){
return new Rectangle(getplayerx() + 10, getplayery() + 30 - 5, 30 - 20, 5);
}
public Rectangle getBoundsLeft(){
return new Rectangle(getplayerx(), getplayery()+10, 5, 30 -20);
}
public Rectangle getBoundsRigth(){
return new Rectangle(getplayerx() + 30 - 5, getplayery()+10, 5, 30 - 20);
}


}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366

2 Answers2

2

Okay, that took a lot of looking, but...

public Rectangle getBounds() {
    return new Rectangle(getplatform1X(), platform1Y(), 120, 20);
}

is your problem. JFrame#getBounds is a method used by JFrame to return the position and the size of the component and since you've overridden it, it will now ignore any values you pass to setSize (directly or indirectly)

Have a read of the following for reasons why you shouldn't override paint of top level containers like JFrame

As a general rule of thumb, you should avoid extending from JFrame, as you're not really adding any new functionality to the class, it locks you into a single use case and, based on your experience, can cause other problems.

You should start with something like JPanel and override its paintComponent method instead (and make sure you call super.paintComponent before you do any custom painting)

Take a look at Painting in AWT and Swing and Performing Custom Painting

You paint methods should run as fast as possible, so you should avoid loading resources like loading images, within the paint method

Swing is single threaded, so calling Thread.sleep in the paint method will cause the entire program to stop, including it's ability to respond to new events from the user.

I'd also discourage you from using KeyListener, using the key bindings API will be more reliable. Take a look at How to Use Key Bindings for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Edit:

A better way to achieve the same result is to call b.setPreferredSize(dimension); and then pack() your frame.

See this answer for more information about JFrame.pack() : https://stackoverflow.com/a/22982334/5224040

Try this in your main method:

public static void main(String[] args) {
    Background b = new Background();
    Dimension d = new Dimension(900, 885); //Create a new Dimension
    b.setTitle("Game");
    b.setPreferredSize(d);  //Set the PreferredSize;
    b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    b.pack();  //Pack the frame
    b.setVisible(true);  //Setting Visible
}

You can also use the following methods to reposition your frame as necessary:

b.setLocationRelativeTo(null); //center the location on the screen.

b.setLocation(0, 0); //Set location explicitly

Call either of these methods below b.pack(); to reposition your frame.

Community
  • 1
  • 1
Austin Aryain
  • 41
  • 1
  • 5