我想在java中使用applet製作堆棧遊戲。由於小程序可以調整大小,因此我試圖在小程序大小發生變化時使用調整大小的塊。的塊是正方形,應該是1/10的高度和1/10 Applet的寬度,但是,每當我請獲取applet的高度和寬度
this.getSize().width
或
this.getSize().height
或
this.getWidth()
或
this.getHeight()
它似乎返回0.所以,我如何訪問applet的高度和寬度?
編輯:這裏爲applet類
import java.applet.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class StackerMain extends Applet implements Runnable, KeyListener,
MouseListener, MouseMotionListener{
//ADD MUSIC HERE//
/////////////////
//public StackerBlocks((RectangularShape s, Color c, int n, int rn, int x, int w, int h, int lr, int r){
private StackerBlocks block1;
private boolean gameOver;
private int score;
private boolean movingLeft;
private boolean movingRight;
private Thread stackerAnimator;
private int delay;
public int blockHeight = this.getSize().height/10;
public int blockWidth = this.getSize().width/10 ;
String playerName;
public void init(){
//Put Music Here
///////////////
playerName = JOptionPane.showInputDialog(this, "Your Name");
block1 = new StackerBlocks(new Rectangle2D.Double(), Color.MAGENTA,
3, 5, 500, blockWidth, blockHeight, -1, 1);
delay = 200 * block1.getRowNum();
this.setFocusable(true);
this.addKeyListener(this);
this.addMouseListener(this);
this.addMouseMotionListener(this);
score = 0;
gameOver = false;
movingLeft = true;
movingRight = false;
}
public void start(){
stackerAnimator = new Thread(this);
stackerAnimator.start();
}
public void stop(){
stackerAnimator = null;
}
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D) g;
if (!gameOver){
g2.setColor(block1.getColor());
g2.fill(block1.getShape());
g2.draw(block1.getShape());
g2.setFont(new Font("Times New Roman", Font.BOLD, 20));
g2.drawString("" + playerName + ": " + score, 5, 15);
}
else{
g2.drawString("GAME OVER, Score: " + score, this.getWidth()/2 - 10, this.getHeight()/2 - 8);
}
}
public void run(){
while(Thread.currentThread() == stackerAnimator && !gameOver){
block1.moveShape();
if (movingLeft){
block1.setX(block1.getX() - block1.getWidth());
if (block1.getX() <= 0){
block1.setX(0);
movingLeft = false;
movingRight = true;
block1.changeHorizontalDirection();
}
}
if (movingRight){
block1.setX(block1.getX() + block1.getWidth());
if (block1.getX() + block1.getWidth() >= this.getWidth()){
block1.setX(this.getWidth() - block1.getWidth());
movingRight = false;
movingLeft = true;
block1.changeHorizontalDirection();
}
}
repaint();
try{
Thread.sleep(delay);
}
catch(InterruptedException e){
break;
}
}
}
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_SPACE){
if (movingRight){
movingRight = false;
block1.setRun(0);
}
if (movingLeft){
movingLeft = false;
block1.setRun(0);
}
}
}
@Override
public void keyReleased(KeyEvent k) {
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
當你使用這個關鍵字時,你是什麼類的?你可以發佈[MCVE](http://stackoverflow.com/help/mcve)嗎? –
確定生病進行編輯。 – ArcWalrus
在確定之前調用獲取大小(第一次繪製)。嘗試使用'getPerferredSize'來代替。 – Mordechai