gpt4 book ai didi

java 'Background' 打开小看什么

转载 作者:行者123 更新时间:2023-12-01 21:50:52 27 4
gpt4 key购买 nike

我正在制作一个游戏,游戏窗口只会打开到标题的大小。请让我知道需要进行哪些更改才能使“b.setSize(900,885)”正常工作。如果需要,我将更改或添加任意数量的代码。

 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);
}


}

最佳答案

好吧,这花了很多功夫,但是......

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

是你的问题。 JFrame#getBoundsJFrame 用于返回组件位置和大小的方法,由于您已覆盖它,因此它现在将忽略您传递给 setSize 的任何值(直接或间接)

请阅读以下内容,了解为什么不应覆盖 JFrame 等顶级容器的 paint 的原因

作为一般经验法则,您应该避免从 JFrame 扩展,因为您并没有真正向类添加任何新功能,它会将您锁定在单个用例中,并且基于您的经验可能会导致其他问题。

您应该从诸如 JPanel 之类的东西开始,并重写其 paintComponent 方法(并确保在执行任何操作之前调用 super.paintComponent定制绘画)

看看Painting in AWT and SwingPerforming Custom Painting

您的绘制方法应该尽可能快地运行,因此您应该避免在绘制方法中加载资源,例如加载图像

Swing 是单线程的,因此在 paint 方法中调用 Thread.sleep 将导致整个程序停止,包括它响应用户新事件的能力.

我也不鼓励您使用 KeyListener,使用键绑定(bind) API 会更可靠。看看How to Use Key Bindings了解更多详情

关于java 'Background' 打开小看什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35261761/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com