Results 1 to 7 of 7
- 04-27-2012, 05:25 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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
- 04-27-2012, 05:43 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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) { ... ... ... }Website: Learn Java by Examples
- 04-27-2012, 07:07 AM #3
Re: Program won't run need help with code
Why do they call it rush hour when nothing moves? - Robin Williams
- 04-27-2012, 07:12 AM #4
Re: Program won't run need help with code
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-27-2012, 07:10 PM #5
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Program won't run need help with code
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...
-
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.
- 04-28-2012, 06:56 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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
-
error code on my program
By andnlou2678 in forum New To JavaReplies: 11Last Post: 10-11-2011, 11:26 PM -
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 12:57 PM -
What code would you put in a wedding program?
By frenchzebu in forum Forum LobbyReplies: 1Last Post: 07-03-2010, 03:08 PM -
Changes in code not showing up when running program!?
By jchsu in forum New To JavaReplies: 1Last Post: 03-30-2010, 09:23 PM -
Graph program code,hardnut
By Javashak in forum Advanced JavaReplies: 0Last Post: 03-24-2008, 03:25 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks