Results 1 to 7 of 7
  1. #1
    Upcoming is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Program won't run need help with code

    Hey I'm trying to get this program to run. I'm trying to make a game and this class is supposed to be the class that loads the character for the user to control and for some reason it wont open into any applet. i don't have any errors registering but it just wont open. if someone could take a look at tell me where I'm going wrong i would very much appreciate it. I'm using Eclipse to program cause i hope to eventually incorporate this into an android app but i want to get it to run in java first. Thank you



    import java.awt.Cursor;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.ImageObserver;
    import java.util.*;

    public abstract class Fighter implements ImageObserver
    {

    static boolean leftKey = false;

    static boolean rightKey = false;

    static boolean upKey = false;

    static boolean downKey = false;

    static private int xCoord;

    static private int yCoord;

    static private int height;

    static private int width;

    static private int rise;

    static private int run;

    private int panelHeight;

    private int panelWidth;

    public Fighter()
    {

    setXCoord();

    setYCoord();

    setHeight();

    setWidth();

    setRise();

    setRun();

    }//End constructor

    /** This method sets the initial x-coordinate */

    public void setXCoord()
    {

    int tempX = 50;

    xCoord = tempX;

    }//End setXCoord method



    public int getXCoord()
    {

    return(xCoord);

    }//End getXCoord method



    /** This method sets the initial y-coordinate */

    public void setYCoord()
    {

    int tempY = 50;

    yCoord = tempY;

    }//End setYCoord method



    public int getYCoord()
    {

    return(yCoord);

    }//End getYCoord method



    /** This method sets the initial height */

    public void setHeight()
    {
    int max = 11;
    Random rand = new Random();
    int tempH = rand.nextInt(max) + 10;

    height = tempH;

    }//End setHeight method



    public int getHeight()
    {

    return(height);

    }//End getHeight method



    /** This method sets the initial width */

    public void setWidth()
    {

    int max = 11;

    Random rand = new Random();

    int tempW = rand.nextInt(max) + 10;

    width = tempW;


    }//End setWidth method

    public int getWidth()
    {

    return(width);

    }//End getWidth method

    /** This method sets the initial rise */

    public void setRise()
    {

    int max = 11;

    Random rand = new Random();

    int tempR = rand.nextInt(max);

    rise = tempR;

    }//End setRise method

    public int getRise()
    {

    return(rise);

    }//End getRise method

    /** This method sets the initial run */

    public void setRun()
    {

    int max = 11;

    Random rand = new Random();

    int tempR = rand.nextInt(max);

    run = tempR;

    }//End setRise method

    public int getRun()
    {

    return(run);

    }//End getRun method


    public void paintComponent (Graphics g)
    {
    Image Fighter = Toolkit.getDefaultToolkit().getImage("player.png") ;
    g.drawImage(Fighter, xCoord, yCoord, this);
    } // end method paintComponent

    public interface KeyListener extends EventListener
    {
    public void keyPressed(KeyEvent e);
    public void keyReleased(KeyEvent e);
    }

    public void keyPressed(KeyEvent e)
    {
    switch(e.getKeyCode())
    {
    case KeyEvent.VK_LEFT: leftKey = true;
    xCoord = xCoord - 5;
    break;
    case KeyEvent.VK_RIGHT: rightKey = true;
    xCoord = xCoord + 5;
    break;
    case KeyEvent.VK_UP: upKey = true;
    yCoord = yCoord - 5;
    break;
    case KeyEvent.VK_DOWN: downKey = true;
    yCoord = yCoord + 5;
    break;

    }
    }
    public void keyReleased(KeyEvent e)
    {
    switch(e.getKeyCode())
    {
    case KeyEvent.VK_LEFT: leftKey = false;
    case KeyEvent.VK_RIGHT: rightKey = false;
    case KeyEvent.VK_UP: upKey = false;
    case KeyEvent.VK_DOWN: downKey = false;
    }

    }

    public void move()
    {
    if (upKey = true)
    {
    yCoord = yCoord - 5;
    }

    if (downKey = true)
    {
    yCoord = yCoord + 5;
    }

    if (rightKey = true)
    {
    xCoord = xCoord + 5;
    }

    if (leftKey = true)
    {
    xCoord = xCoord - 5;
    }

    // If fighter is approaching a wall, reverse direction

    if (xCoord < (0 - run) || xCoord > (panelWidth - width))
    {

    run = -run;

    }

    if (yCoord < (0 - rise) || yCoord > (panelHeight - height))
    {

    rise = -rise;

    }

    // "Move" fighter according to values in rise and run

    xCoord += run;

    yCoord += rise;


    }//End move method

    public void detectCollision()

    {

    }//End detectCollision method

    public void setPanelHeight(int panelHeight) {

    this.panelHeight = panelHeight;

    }//End setPanelHeight


    public void setPanelWidth(int panelWidth) {

    this.panelWidth = panelWidth;

    }//End setPanelWidth


    }//End Fighter class

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Program won't run need help with code

    I just had a quick look at your program and see there is no entry point in your program. So how will you code run? Typically when you have a program you need to define the entry point where it execution starts. In Java it will look like this:

    Java Code:
    public static void main(String[] args) {
        ...
        ...
        ...
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Program won't run need help with code

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Program won't run need help with code

    Quote Originally Posted by wsaryada View Post
    I just had a quick look at your program and see there is no entry point in your program.
    The OP mentioned something about an Applet. But a quick glance shows a class that doesn't directly or indirectly extend JComponent, with a paintComponent(Graphics) method that loads an image each time it's called (if it's ever called), that claims to implement ImageObserver but doesn't. IOW, uncompilable code.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Upcoming is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Program won't run need help with code

    Quote Originally Posted by wsaryada View Post
    I just had a quick look at your program and see there is no entry point in your program. So how will you code run? Typically when you have a program you need to define the entry point where it execution starts. In Java it will look like this:

    Java Code:
    public static void main(String[] args) {
        ...
        ...
        ...
    }
    When i put the main class in a get a crap load of other errors on my constructor method saying duplicate classes so i commented out the constructor method to see if it would run and it still doesn't run. do u have to have the constructor method even though im declaring the methods in the program? im really lost right now.. i've been really frustrated with this for a while now...

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

    Default Re: Program won't run need help with code

    If you're trying to run the code as an applet, shouldn't you have a class extend either Applet or JApplet?

    Also, if you truly need our help, you're going to have to put in a lot more effort into your question, because right now you're not telling us anything useful that will enable us to help you, important details such as exactly what you're trying to do, including your assignment instructions, what errors you're seeing, etc... If you've fixed your problem, then ignore this post, but if not, please look at the link at the end of this post titled "how to ask smart questions" as this will tell you just what stuff we need if we're going to have a chance of helping you.

  7. #7
    Upcoming is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Program won't run need help with code

    Sorry im new to the forums. The assingment was to make a game in java. pretty basic assingment but i have just been having trouble trying to get everything to work on it. I added the extends JApplet to my fighter class so it now looks like this....public abstract class Fighter implements ImageObserver extends JApplet.... It is giving me a syntax error on the "extends" call and i can not figure out why. do i need to make a sub class inside of the fighter class and then call the "extends JApplet"?

Similar Threads

  1. error code on my program
    By andnlou2678 in forum New To Java
    Replies: 11
    Last Post: 10-12-2011, 12:26 AM
  2. Replies: 2
    Last Post: 04-02-2011, 01:57 PM
  3. What code would you put in a wedding program?
    By frenchzebu in forum Forum Lobby
    Replies: 1
    Last Post: 07-03-2010, 04:08 PM
  4. Replies: 1
    Last Post: 03-30-2010, 10:23 PM
  5. Graph program code,hardnut
    By Javashak in forum Advanced Java
    Replies: 0
    Last Post: 03-24-2008, 04:25 PM

Posting Permissions

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