I've wrote a game. But when I compile, and run the main method, there is an error, saying:
No main methods found or something like that.

I have one class called Game.java, which is supposed to be the main one, and gameLoop.java.
For unknown reasons, gameLoop doesnt want to compile.

This is my Game.java class:

import java.applet.Applet;
import java.awt.Graphics;

public class Game extends gameLoop{

    public void init(){
    setSize(854,480);
    Thread th = new Thread(this);
    th.start();
    offscreen = createImage(854,480);
    d = offscreen.getGraphics();
   addKeyListener(this);
}
    public void paint(Graphics g){
     d.clearRect(0,0,854,480);
     d.drawOval(x,y,20,20);
     g.drawImage(offscreen,0,0,this);
    }
    public void update(Graphics g){
    paint(g);
    }
}

And this is my gameLoop.java class:

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyListener;

public class gameLoop extends Applet implements Runnable, KeyListener{

    public int x, y;
    public Image offscreen;
    public Graphics d;
    public boolean up, down, left, right;

    public void run(){
    x = 100;
    y = 100;
    while(true){
    if (left == true){
        x--;
    }
    if (right == true){
        x++;
    }
    if (up == true){
        y--;
    }
    if (down == true){
        y++;
    }

    repaint();
    try {
        Tread.sleep(20);
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }
    }
    }
    public void keyPressed(KeyEvent e){
    if (e.getKeyCode() == 37){
    left = true;
    }
    if (e.getKeyCode() == 38){
    up = true;
    }
    if (e.getKeyCode() == 39){
    right = true;
    }
    if (e.getKeyCode() == 40){
    down = true;
    }

    public void keyReleased(KeyEvent e){
    if (e.getKeyCode() == 37){
    left = false;
    }
    if (e.getKeyCode() == 38){
    up = false;
    }
    if (e.getKeyCode() == 39){
    right = false;
    }
    if (e.getKeyCode() == 40){
    down = false;
    }
    }
    public void keyTyped(KeyEvent e) {}
}

If I could get any advice from proficient programmers, better than me, I would appreciate your help.

Recommended Answers

All 9 Replies

"For unknown reasons, gameLoop doesnt want to compile" - that's just silly. When you compile the compiler gives you detailed error messages, including the line number(s) involved.
Start by posting the exact complete text of all your error messages.

OK. Here You go. These are the errors I see after trying to compile the gameLoop:

gameLoop.java:53: error: illegal start of expression
    public void keyReleased(KeyEvent e){
    ^
gameLoop.java:53: error: illegal start of expression
    public void keyReleased(KeyEvent e){
           ^
gameLoop.java:53: error: ';' expected
    public void keyReleased(KeyEvent e){
                           ^
gameLoop.java:53: error: ';' expected
    public void keyReleased(KeyEvent e){
                                      ^
gameLoop.java:67: error: illegal start of expression
    public void keyTyped(KeyEvent e) {}
    ^
gameLoop.java:67: error: illegal start of expression
    public void keyTyped(KeyEvent e) {}
           ^
gameLoop.java:67: error: ';' expected
    public void keyTyped(KeyEvent e) {}
                        ^
gameLoop.java:67: error: ';' expected
    public void keyTyped(KeyEvent e) {}
                                   ^
gameLoop.java:68: error: reached end of file while parsing
}
 ^
9 errors

Start checking braces. You have an unclosed block above your keyReleased() function declaration.

OK. Thanks, Ezzaral. However now I got other 4 errors which are:

gameLoop.java:39: error: cannot find symbol
    public void keyPressed(KeyEvent e){
                           ^
  symbol:   class KeyEvent
  location: class gameLoop
gameLoop.java:54: error: cannot find symbol
    public void keyReleased(KeyEvent e){
                            ^
  symbol:   class KeyEvent
  location: class gameLoop
gameLoop.java:68: error: cannot find symbol
    public void keyTyped(KeyEvent e) {}
                         ^
  symbol:   class KeyEvent
  location: class gameLoop
gameLoop.java:32: error: cannot find symbol
        Tread.sleep(20);
        ^
  symbol:   variable Tread
  location: class gameLoop
4 errors

If I get this program to work, does anybody know how to make an object like box be placed randomly on the plane, and when the cirlce reaches-touches the box, there will be a message like "You win. Play Again. Quit", if the player selects Play Again, the box will be moved to a different random place, and if the player selects Quit, the window will just close.

I've found the code to place the box at a random location, but not sure where to place it, wheather in the Game class or the gameLoop class. I also need to be careful in which lines I need to place the code too.

class Foo {
    Rectangle box
    Foo() {
        Random random = new Random();
        this.box = new Rectangle(random.nextInt(800), random.nextInt(600), 10, 10);
    }
    void Update(Graphics g) {
        g.setColor(Color.BLUE);
        g.fillRect(box.x, box.y, box.width, box.height);
    }
}

I've tried to place the code in the Game class at the very end, but there came up an error such:

Game.java:23: error: ';' expected
    Rectangle box

But when I've added the ";", there came more errors such:

gameLoop.java:40: error: cannot find symbol
    public void keyPressed(KeyEvent e){
                           ^
  symbol:   class KeyEvent
  location: class gameLoop
gameLoop.java:55: error: cannot find symbol
    public void keyReleased(KeyEvent e){
                            ^
  symbol:   class KeyEvent
  location: class gameLoop
gameLoop.java:69: error: cannot find symbol
    public void keyTyped(KeyEvent e) {}
                         ^
  symbol:   class KeyEvent
  location: class gameLoop
Game.java:24: error: cannot find symbol
    Rectangle box;
    ^
  symbol:   class Rectangle
  location: class Game.Foo
gameLoop.java:33: error: cannot find symbol
        Tread.sleep(20);
        ^
  symbol:   variable Tread
  location: class gameLoop
Game.java:27: error: cannot find symbol
        this.box = new Rectangle(random.nextInt(800), random.nextInt(600), 10, 10);
                       ^
  symbol:   class Rectangle
  location: class Game.Foo
Game.java:30: error: cannot find symbol
        g.setColor(Color.BLUE);
                   ^
  symbol:   variable Color
  location: class Game.Foo
7 errors

You haven't imported any of those classes.

ALso, the reason there is no main() method is simple: the program is written as an applet (a web-based program meant to be run in a browser) rather than a stand-alone application - something you would know if you were the original author of the program. I am assuming you are following a tutorial of some sort and got stuck, right? A rather outdated tutorial at that; not only is the older AWT Applet class deprecated in favor of the Swinf JApplet, but applets in general have fallen by the wayside for the most part.

Schol-R-LEA you are totaly right. I've found a tutorial and tried to modify it for my own creation. Is it possible to get this game going at all?

OK. Now I've got all errors out of the way, but when I try to run the Game class, there pops up a message that says:
No main methods, applets, or MIDlets found in file

Is it possible to fix this problem at all?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.