java组件大小问题!由于我不知道的原因,调整到父级大小!
我似乎不明白为什么会这样
我的服务器挡板(基于java.awt.Component)的大小不合适。 我已经放置了一个系统。出来println(serverpable.getSize());在线程循环中,它显示组件是1个循环的正确大小,然后是下一个循环和之后的循环,它与父循环(容器)的大小相同
import javax.swing.JFrame;
import java.awt.Container;
import java.awt.Color;
import java.awt.Graphics;
private final int FRAMERATE = 60, THIS_MUCH = 1000/FRAMERATE,
BALL_DIAMETER = 30, BALL_SPEED = 15,
PADDLE_WIDTH = 15, PADDLE_HEIGHT = 60, PADDLE_SPEED = 30;
private final Color BALL_COLOR = Color.WHITE, PADDLE_COLOR = Color.WHITE;
private Container c;
private Ball puck;
private Paddle serverPaddle, clientPaddle;
...
private Container c;
...
c = getContentPane();
...
public void run() {
//Center puck
puck = new Ball(c.getWidth()/2 - BALL_DIAMETER/2,
c.getHeight()/2 - BALL_DIAMETER/2, BALL_SPEED);
puck.setSize(BALL_DIAMETER, BALL_DIAMETER);
puck.setForeground(BALL_COLOR);
puck.createHitbox();
puck.setMoving(true);
//West paddle
serverPaddle = new Paddle(PADDLE_WIDTH,
c.getHeight()/2 - PADDLE_HEIGHT/2,
PADDLE_SPEED);
serverPaddle.setSize(PADDLE_WIDTH, PADDLE_HEIGHT);
serverPaddle.setForeground(PADDLE_COLOR);
serverPaddle.createHitbox();
c.add(puck);
c.add(serverPaddle);
//Draw at FRAMERATE frames per second
while (true) {
System.out.println(serverPaddle.getSize());
puck.move(determineSituation());
puck.repaint();
wait(THIS_MUCH);
}
}
这是划桨课
import java.awt.Graphics;
import java.awt.Color;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
public class Paddle extends GameObject implements KeyListener {
private int x = 0, y = 0;
public Paddle(int x, int y, int speed) {
super(speed);
this.x = x;
this.y = y;
}
public void keyTyped(KeyEvent e) {
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
public void paint(Graphics g) {
setLocation(x, y);
//These two are the culprits, the size is correct when
//I use constants instead.
g.fillRect(0, 0, **getWidth()**, **getHeight()**);
updateHitbox();
}
}
我的游戏对象类
import java.awt.Component;
import java.awt.Rectangle;
public class GameObject extends Component {
/* Members */
private int speed = 0;
private boolean isMoving = false;
private Rectangle hitbox;
//Dead south = 0 radians, anti-clockwise
private double direction = 0;
public GameObject(int speed) {
this.speed = speed;
}
/* Accessors */
public boolean isMoving() { return isMoving; }
public void setMoving(boolean isMoving) { this.isMoving = isMoving; }
public int getSpeed() { return speed; }
public void setSpeed(int speed) { this.speed = speed; }
public double getDirection() { return direction; }
public void setDirection(double direction) { this.direction = direction; }
public Rectangle getHitbox() { return hitbox; }
public void createHitbox() {
hitbox = new Rectangle(getX(), getY(), getWidth(), getHeight());
}
public void createHitbox(int x, int y, int width, int height) {
hitbox = new Rectangle(x, y, width, height);
}
public void updateHitbox() {
hitbox.setLocation(getX(), getY());
}
}
即使我注释掉球/冰球的所有代码,它也不起作用。注释掉“wait”方法也不起作用。它只是因为某种原因而改变,我不知道,但我真的很想知道,这样我就可以解决这个问题!!十一,
救命啊!谢谢
顺便说一下,wait只是调用线程。睡眠
public void wait(int durationInMilliseconds) {
try {
Thread.sleep(durationInMilliseconds);
} catch (InterruptedException e) {
System.out.println(e);
}
}
# 1 楼答案
你为什么不使用Swing
# 2 楼答案
首先,代码太多了。您希望能够减少问题,使其变得足够小,变得微不足道。如果你想让其他人帮忙,一般来说,最好能生成一个完整的、可编译的程序来显示问题,而不是其他任何东西
实际上,它看起来像是在使用
JFrame
的内容窗格,默认为LayoutManager
。这将默认为BorderLayout
。如果在不指定约束的情况下添加零部件,它们将被添加到“中心”。没有任何侧面组件,中心组件将展开以填充所有可用区域因此,设置一个合适的布局管理器。我更喜欢创建一个
JPanel
并使用setContentPane
,而不必改变我的代码结构# 3 楼答案
你可能会喜欢这个Java 2D games tutorial风格的游戏
# 4 楼答案
呃
出于我不知道的一些非常奇怪的原因,当我在run()方法的正下方添加这一行时
拨片正确渲染(正确大小)
当我移除它时,问题又回来了。。。所以怎么回事