Results 1 to 8 of 8
Thread: Basic question on OOP.
- 01-18-2009, 11:49 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
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++;
}
}
}
- 01-19-2009, 07:05 AM #2
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:
orJava Code:public int x,y;
Giving a variable access changes is not really necessary in your situation don't bother changing accessJava Code:int x,y;
Sari
- 01-19-2009, 01:24 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
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.
- 01-19-2009, 06:17 PM #4
Screen Is a static context because It extends Thread and you need to add a constructor method to your class MyGame:
Try to put the thread In the class MyGame or Subclass ScreenJava Code:public MyGame() { //Set up the graphics panels etc. }
directly into the class MyGame
- 01-22-2009, 09:30 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
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.
- 01-26-2009, 03:44 PM #7
Member
- Join Date
- Jan 2009
- Posts
- 4
- Rep Power
- 0
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.
-
Similar Threads
-
Basic MouseListener Question
By jshailes in forum AWT / SwingReplies: 9Last Post: 01-15-2009, 08:58 AM -
Basic array question
By jigglywiggly in forum New To JavaReplies: 12Last Post: 01-09-2009, 04:44 PM -
Basic question about EJB
By javaplus in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 07-15-2008, 05:44 PM -
Very basic question
By gvi in forum New To JavaReplies: 2Last Post: 10-30-2007, 06:30 PM -
basic question about files
By oregon in forum New To JavaReplies: 1Last Post: 08-05-2007, 02:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks