簡體   English   中英

獲取小程序的高度和寬度

[英]Getting the height and width of an applet

我正在嘗試使用Java中的applet進行積木游戲。 由於可以調整小應用程序的大小,因此我試圖在小應用程序的大小更改時重新調整所使用的塊的大小。 塊是正方形,應為小程序的高度的1/10,寬度為小程序的1/10,但是,每當我調用

this.getSize().width

要么

this.getSize().height

要么

this.getWidth()

要么

this.getHeight()

它似乎返回0。那么,如何訪問小程序的高度和寬度?

編輯:這是小程序類的代碼

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

    }


}

使小程序可見后,實例化寬度和高度。 發生這種情況后,請嘗試調用.width方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM