元件尺寸问题!由于我不知道的原因,调整为父母的尺寸!
我似乎无法弄清楚为什么会发生这种情况...
我的 serverPaddle (它基于 java.awt.Component)的大小不正确。 我已经放置了 System.out.println(serverPaddle.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);
}
}
这是 Paddle 类
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();
}
}
我的 GameObject 类...
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”方法也不起作用。它只是由于某种我不知道的原因而发生了变化,但我真的很想知道,这样我就可以修复这个问题!!11
帮助!谢谢。
顺便说一下,wait只是调用Thread.sleep
public void wait(int durationInMilliseconds) {
try {
Thread.sleep(durationInMilliseconds);
} catch (InterruptedException e) {
System.out.println(e);
}
}
I can't seem to figure out why this is happening...
My serverPaddle (it's based on a java.awt.Component) isn't coming out the right size.
I've placed System.out.println(serverPaddle.getSize()); in the thread loop and it shows that the component is the right size for 1 loop and then the next and thereafter, it's the same size as the parent (Container).
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);
}
}
This is the Paddle class
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();
}
}
My GameObject class...
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());
}
}
even when I comment all the code out for the Ball/puck, it doesn't work. Commenting out the "wait" method doesn't work as well. It just somehow changes for some reason that I don't know but I really wanna know SO I CAN FIX THIS THING!!11
Help! Thanks.
By the way, wait is just calling Thread.sleep
public void wait(int durationInMilliseconds) {
try {
Thread.sleep(durationInMilliseconds);
} catch (InterruptedException e) {
System.out.println(e);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,这是非常多的代码。您希望能够将问题缩小到足够小,使其变得微不足道。如果您希望其他人提供帮助,通常最好能够生成一个完整的、可编译的程序来显示问题而不是其他内容。
实际上,您似乎正在使用带有默认
LayoutManager
的JFrame
内容窗格。这将默认为BorderLayout
。当您添加组件而不指定约束时,它们将被添加到“中心”。如果没有任何侧面组件,中心组件将展开以填充所有可用区域。所以设置一个合适的布局管理器。我更喜欢创建一个
JPanel
并使用setContentPane
,而不是必须围绕getContentPane
改变我的代码结构。Firstly, that is an awful lot of code. You want to be able to cut the problem down so that it is small enough that it becomes trivial. And if you want other people to help, it's generally a good idea to be able to produce a complete, compilable program that shows the problem and nothing else.
As it is, it looks as if you are using the content pane of a
JFrame
with the defaultLayoutManager
. This will default toBorderLayout
. When you add components without specifying constraints, they will be added to the "center". Without any side components, the center component will spread out to fill all available area.So set an appropriate layout manager. I prefer to create a
JPanel
and usesetContentPane
, rather than have to bend my code structure aroundgetContentPane
.您不使用 Swing 有什么原因吗?
Any reason you are not using Swing?
呃...
由于一些我不知道的非常奇怪的原因,当我在 run() 方法正下方添加这一行时...
桨正确渲染(正确的大小)。
当我删除它时,问题又回来了...所以...到底是什么?
Eh...
For some very strange reason unbeknownst to me, when I add this line right below the run() method...
The paddle renders (correct size) correctly.
When I remove it, the problem comes back... so... what the heck?
您可能会喜欢这个Java 2D 游戏教程,其中包含Breakout风格的游戏。
You might like this Java 2D games tutorial, featuring a Breakout style game.