How to do this?

I am creating an applet for my game and have run into a wall. I need to somehow get the height and width of my applet so the game objects don’t go off-screen. I could just hardcode it, but i want to make my code as reusable as possible and i don’t want to tie a perticular object to a single applet. So how would i get about this?


// import necessary packages
import java.applet.*;
import java.awt.*;

// Inherit the applet class from the class Applet
public class SpaceXscape extends Applet implements Runnable
{
	// declare two instance variables for double buffering at the head of the program
	private Image dbImage;
	private Graphics dbg;

	//declare our game objects
	Ball ball;

      // Now you should implement the following methods

      // init - method is called the first time you enter the HTML site with the applet
      public void init() {
      	Ball ball = new Ball();
      	}

      // start - method is called every time you enter the HTML - site with the applet
      public void start() {
      	// define a new thread
	  Thread th = new Thread (this);
	  // start this thread
	  th.start (); 
      	}

      // stop - method is called if you leave the site with the applet
      public void stop() {
      	}

      // destroy method is called if you leave the page finally (e. g. closing browser)
      public void destroy() {
      	}

	 // run method is called everytime to move the objects onscreen
	 // define a new thread
	
	  public void run() {
	  	       // lower ThreadPriority
      Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

      // run a long while (true) this means in our case "always"
      while (true)
      {
            // repaint the applet
            repaint();

            try
            {
                  // Stop thread for 20 milliseconds
                  Thread.sleep (20);
            }
            catch (InterruptedException ex)
            {
                  // do nothing
            }

            // set ThreadPriority to maximum value
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
      		}

		}
	
	
/** Update - Method, implements double buffering */
public void update (Graphics g) {

      // initialize buffer
      if (dbImage == null)
      {
            dbImage = createImage (this.getSize().width, this.getSize().height);
            dbg = dbImage.getGraphics ();
      }

      // clear screen in background
      dbg.setColor (getBackground ());
      dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

      // draw elements in background
      dbg.setColor (getForeground());
      paint (dbg);

      // draw image on the screen
      g.drawImage (dbImage, 0, 0, this);
		}
	
	
      /** paint - method allows you to paint into your applet. This method is called e.g. if you move your browser window or if you call repaint() */
      public void paint (Graphics g) {
      	ball.Draw(g);
      	} 

}


import java.applet.*;
import java.awt.*;
import java.net.*;

public abstract class GameObject extends Applet{

	private int x;
    private int y;
    private int x_velocity;
    private int y_velocity;
	private Image image;

	public void setX(int new_x) {x = new_x;}
	public void setY(int new_y) {y = new_y;}
	public void setXVelocity(int new_x_velocity) {x_velocity = new_x_velocity;}
	public void setYVelocity(int new_y_velocity) {y_velocity = new_y_velocity;}

	public int getX() {return x;}
	public int getY() {return y;}
	public int getXVelocity() {return x_velocity;}
	public int getYVelocity() {return y_velocity;}

	public void loadImage(String filename) {
	// load file Land.jpg
	image = getImage (getCodeBase(), filename); 
	}

    public void Draw(Graphics g) {
    // drawing the image: g.drawImage (name, x- value, y - value, frame)
	g.drawImage (image, getX(), getY(), this);
    }

    public abstract void Update();
    
}


class Ball extends GameObject{

public void Update() {
Move();
Check_Bounds();
}

public void Check_Bounds() {
	//ball has collided with the left boundry
	if(getX() =< 0) {
	setXVelocity(-getXVelocity());
	}
	//ball has collided with the right boundry
	if(getX() =< 0) {
	setXVelocity(-getXVelocity());
	}
	
}

public void Move() {
setX(getX() + getXVelocity());
setY(getY() + getYVelocity());
}

}

Any ideas?

this.getWidth()
this.getHeight() ::slight_smile:

Wouldn’t that return the gameobjects width and height?

The SpaceXscape class(first code post) is the main applet where everything is run from.

Everything is run from the applet, so this.getWidth() will return the width of the Applet