-
Basic question on OOP.
Hi,
I'm new to OOP, Applets and threading, and am having a bit of trouble getting my head around things.
Basically, I am trying to make a small game where a character moves from left to right, and can dodge things by moving up of down (via mouse). I'm following the JavaGently book, and so am inheriting from Thread, rather than interfacing with Runable. The skeleton code is below.
Ideally, I would be able to write g.fillOval(x,y,5,5); from the Screen class, but obviously I can't, as it doesn't know what g is.
I've worked out that I can use Graphics h = this.getGraphics(); to draw stuff from within MyGame, but how can I make changes to g from Screen? Whenever I write MyGame.getGraphics();, it tells me that non-static methods cannot be referenced from a static context.
Any help much appreciated.
--------------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyGame extends Applet
{
public static int x, y;
public void init ()
{
this.addMouseListener(new mousePressHandler());
Screen thisgame = new Screen();
thisgame.start();
x=0;
y=200;
}
class mousePressHandler extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
y=y+5;
}
}
public void paint (Graphics g)
{
g.setColor(Color.black);
g.fillRect(0,0,1000,500);
g.setColor(Color.white);
g.fillOval(x,y,5,5);
}
}
class Screen extends Thread
{
public void run()
{
while(MyGame.x<1000)
{
MyGame.x++;
}
}
}
-
I'm no expert in Java Graphics but I believe your error is that the variables x and y are
static they should be declared like this:
or
Giving a variable access changes is not really necessary in your situation don't bother changing access
Sari
-
Hi Sari, thanks for the reply.
I've tried removng the static modifier to int x,y but it doesn't seem to help. In fact, it gives a new error on the while(MyGame.x...) line The error says that the non-static x cannot be referenced from a static context.
The program instatiates a new Screen, so I don't see why Screen is a 'static context'.
Does MyGame get instantiated? It's hard to tell what goes on behind the scenes when uing Applet and Thread.
-
Screen Is a static context because It extends Thread and you need to add a constructor method to your class MyGame:
Code:
public MyGame() {
//Set up the graphics panels etc.
}
Try to put the thread In the class MyGame or Subclass Screen
directly into the class MyGame
-
I've been pottering aroud with what you suggested but to no avail.
I don't understand why I need a constructor fo MyGame, I thought that this is what init() is for.
I understand why I can't write MyGame.getGraphics, but surely if I can write this.getGraphics() from inside MyGame, I can write X.getGraphics() from Screen, where X is the name of the instantiated MyGame.
But what is X? Anybody know?
-
So this book has you coding with AWT (Applets, Buttons, Canvas, Labels, etc...) and not with Swing (JApplets, JButtons, JPanels, JLabels, etc..)?
I speak for some here in that I can't help you as I don't know AWT all that well. AFAIK, AWT has pretty well be superseded by Swing for most GUI apps. Good luck.
-
Yeah, it's quite an old book to be fair, second edition.
You're the second person to mention Swing to me, so it seems that is the way to go.
But I still can't believe that I can't refer to the Applet and just call getGraphics(), and take it from there.
What I'm trying to do seems so simple that its hard to believe that there's not an easy answer.
-
I'm not saying that there isn't an easy answer, just that since it's AWT and not Swing many here, me included, will not be able to give it to you. The Sun Graphics tutorials should help though.
Best of luck!