简体   繁体   中英

Getting the height and width of an applet

I'm trying to make a stacker game using an applet in java. Since the applet can be resized, I'm trying to make the blocks used resize whenever the applet's size changes. The blocks are squares that should be 1/10 the height and 1/10 the width of the applet, however, whenever I call

this.getSize().width

or

this.getSize().height

or

this.getWidth()

or

this.getHeight()

it seems to be returning 0. So, how do I access the height and width of an applet?

EDIT: Here is the code for the applet class

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

    }


}

The width and height are instantiated after the applet is made visible. Try calling the .width methods after this happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM