Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[java] my code needs 911 help me please

Started by thecodehunter Aug 29, 2008 at 8:01 AM 14 replies 2.1k views
Original Post
thecodehunter
thecodehunter
my code has no errors but it still is not running right please help me, I dont know what I did wrong. //import necessary packages import java.applet.*; import java.awt.*; import java.awt.image.*; @SuppressWarnings("serial") public class ReApp extends Applet implements Runnable{ private int x=500,y=500; private int guy_x=WIDTH,guy_y=HEIGHT; BufferedImage bfimg; Image sprite1 = getImage(getCodeBase(),getParameter("sprit1.jpg")); public void init() { this.setSize(x, y); } public void start() { Thread th = new Thread (this); th.start (); } public void stop() { } public void destroy() { } public void draw(Graphics g,int guy_x,int guy_y) { g.drawImage(sprite1,guy_x,guy_y,null); } public void paint (Graphics g) { } @Override public void run() { // whatever i need to do Thread.currentThread().setPriority(Thread.MIN_PRIORITY); while (true){ repaint(); try { Thread.sleep (20); } catch (InterruptedException ex) { // nothing so far } Thread.currentThread().setPriority(Thread.MAX_PRIORITY); } } /** * @param guy_x the guy_x to set */ public void setGuy_x(int guy_x) { this.guy_x = guy_x; } /** * @return the guy_x */ public int getGuy_x() { return guy_x; } /** * @param guy_y the guy_y to set */ public void setGuy_y(int guy_y) { this.guy_y = guy_y; } /** * @return the guy_y */ public int getGuy_y() { return guy_y; } } I appreciate any help that I get.
scottrick49
scottrick49
What exactly is your program supposed to do? As far as I can tell, it doesn't do anything.

Also, could you put
 and 
tags around your code - it makes it alot easier to read.
scottrick49
thecodehunter
thecodehunter
this is just an applet that dispalys an image on screen and right now this is my problem. the next step is to make it move.
thecodehunter
thecodehunter
I tried that i still didn't get get anything. Well, actually i did get the screen to open up to 500x500, aside from that nothing.
Droopy
Droopy
Looking closer at your code, it looks like you may have missed an 'e' in "sprite1.jpg". Also, when I load an image in an Applet, I've never used the getParameter method. My line of code would look like this:

Image sprite1 = getImage(getCodeBase(), "sprite1.jpg");

If this is directly from you code, I'm betting on the missing 'e' though.
thecodehunter
thecodehunter
Well, scottrick49 my intent is to draw my sprite to screen, refreshes every 20 millisecs. That is the only thing i want it to do for now. My goal is to make a sprite that moves, next set bounds.
thecodehunter
thecodehunter
thanks Droopy for all your help. but I still cant get it working right. I checked every thing but i don't see any thing wrong. I did have to fix my draw method but i seems there is an other hidden problem.
Droopy
Droopy
I've been working with your code for a few minutes and I got a version that works. Before I go into the changes that I made for my personal style, try setting guy_x and guy_y to 0 instead of WIDTH and HEIGHT. I think the problem is that you're drawing your image just off the screen to the bottom-right. This might be because you were planning to animate it later, but for now it just seems to be chilling outside of the visible area.
thecodehunter
thecodehunter
Droopy, nope that dose not work i still get the same result
Droopy
Droopy
Alright, here's the full code after I fiddled with some things:

//import necessary packages
import java.applet.*;
import java.awt.*;
import java.awt.image.*;


@SuppressWarnings("serial")
public class Main extends Applet implements Runnable {


private int x, y;
private int guy_x,guy_y;

BufferedImage bfimg;
Image sprite1;

Thread th = new Thread (this);

public void init() {
this.setSize(x, y);
sprite1 = getImage(getCodeBase(),"sprite1.png");
th.start ();
}

public void start() {
}

public void stop() { }

public void destroy() { }

public void draw(Graphics g,int guy_x,int guy_y) {
g.drawImage(sprite1,guy_x,guy_y, null);
}

public void paint (Graphics g) {
draw(g, x, y);
}

public void run() {
// whatever i need to do
while (true){

repaint();

try {
Thread.sleep (20);
}
catch (InterruptedException ex) {
// nothing so far
}

}

}

/**
* @param guy_x the guy_x to set
*/
public void setGuy_x(int guy_x) {
this.guy_x = guy_x;
}

/**
* @return the guy_x
*/
public int getGuy_x() {
return guy_x;
}

/**
* @param guy_y the guy_y to set
*/
public void setGuy_y(int guy_y) {
this.guy_y = guy_y;
}

/**
* @return the guy_y
*/
public int getGuy_y() {
return guy_y;
}

}


Anyway, that worked for me with one of my images named "sprite1.jpg". I'm not really sure of why you have x and y variables AND guy_x and guy_y variables, and then refer to x and y as guy_x and guy_y in the draw method, but I assume you have your reasons, so I left them in...
thecodehunter
thecodehunter
I decided to look at my java console but i still dont understand why its not working:
java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:152)
at ReApp.(ReApp.java:16)
at sun.reflect.GeneratedConstructorAccessor4.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at com.opera.AppletPanel.runLoaderThread(AppletPanel.java:375)
at com.opera.AppletPanel.run(AppletPanel.java:347)
at java.lang.Thread.run(Thread.java:619)
Droopy
Droopy
I think it's because you're trying to create the image using the applet's code base before the applet is initiated. Declare the variable at the beginning of the class with just:

Image sprite1;

and then define it within the init() method like I did:

public void init() {
setSize(x,y);
sprite1 = getImage(getCodeBase(), "sprite1.jpg");
}


I uploaded my revisions of your code to

http://www.rockettanks.webs.com/Test/Untitled.html

to show you that it does in fact work.
thecodehunter
thecodehunter
may I see the source. because maybe its my system that got issues, I use UBUNTU but never had a problem till now.
Droopy
Droopy
I posted the full source a couple posts ago and that's the one that works for me. It should work for you except that I used a PNG instead of a JPG soy ou would have to switch "sprite1.png" back to "sprite1.jpg".

Maybe you could upload what you have somewhere for us to see if it works on our systems?




thecodehunter
thecodehunter
got it working with BlueJ IDE thanks so much for your help Droppy. I can't thank you enough.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.