🔒 Locked
🎮 Unity
[java] my code needs 911 help me please
Advertisement
Original Post
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.
What exactly is your program supposed to do? As far as I can tell, it doesn't do anything.
Also, could you put
Also, could you put
and tags around your code - it makes it alot easier to read.
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.
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.
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.
Image sprite1 = getImage(getCodeBase(), "sprite1.jpg");
If this is directly from you code, I'm betting on the missing 'e' though.
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.
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.
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.
Droopy, nope that dose not work i still get the same result
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...
//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...
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)
java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:152)
at ReApp.
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)
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.
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.
may I see the source. because maybe its my system that got issues, I use UBUNTU but never had a problem till now.
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?
Maybe you could upload what you have somewhere for us to see if it works on our systems?
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.
Advertisement