2015-05-21 145 views
2

我想在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 

    } 


} 
+1

当你使用这个关键字时,你是什么类的?你可以发布[MCVE](http://stackoverflow.com/help/mcve)吗? –

+0

确定生病进行编辑。 – ArcWalrus

+0

在确定之前调用获取大小(第一次绘制)。尝试使用'getPerferredSize'来代替。 – Mordechai

回答

0

的宽度和高度的小应用程序是可见之后被实例化的代码。尝试在发生这种情况后调用.width方法。

+1

我不完全理解...在发生什么后调用.width。 – ArcWalrus

+0

在run()方法中调用它。 –

+0

我做了,没有出现,即使我将它设置为一个不变的整数,如100。 – ArcWalrus