Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  How to do this?  (Read 971 times)
0 Members and 1 Guest are viewing this topic.
Space Xscape
Guest
« Posted 2007-07-30 22:45:22 »

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?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
// 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);
         }

}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
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();
   
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
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?
Offline 2playgames

Junior Member





« Reply #1 - Posted 2007-07-30 23:29:46 »

this.getWidth()
this.getHeight()  Roll Eyes

Space Xscape
Guest
« Reply #2 - Posted 2007-07-31 04:39:43 »

this.getWidth()
this.getHeight()  Roll Eyes

Wouldn't that return the gameobjects width and height?

The SpaceXscape class(first code post) is the main applet where everything is run from.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline keldon85

Senior Member


Medals: 1



« Reply #3 - Posted 2007-07-31 10:50:45 »

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!