Results 1 to 8 of 8
  1. #1
    madthinker is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default 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++;
    }
    }
    }

  2. #2
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    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:

    Java Code:
    public int x,y;
    or

    Java Code:
    int x,y;
    Giving a variable access changes is not really necessary in your situation don't bother changing access

    Sari

  3. #3
    madthinker is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    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.

  4. #4
    sari's Avatar
    sari is offline Member
    Join Date
    Jan 2009
    Posts
    32
    Rep Power
    0

    Default

    Screen Is a static context because It extends Thread and you need to add a constructor method to your class MyGame:

    Java 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

  5. #5
    madthinker is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    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?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    madthinker is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    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!

Similar Threads

  1. Basic MouseListener Question
    By jshailes in forum AWT / Swing
    Replies: 9
    Last Post: 01-15-2009, 09:58 AM
  2. Basic array question
    By jigglywiggly in forum New To Java
    Replies: 12
    Last Post: 01-09-2009, 05:44 PM
  3. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 06:44 PM
  4. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 07:30 PM
  5. basic question about files
    By oregon in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 03:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •