applet issue

L

Linus Norton

i have an applet Game which has a Player class which in turn has an
Animation class. the Animation class loads the image into an array using
getImage and can then retrieve the current Image.

the player class has a method animate which gets the current Image from the
animation class and returns it to the Game class.

but for some reason when i call g.drawImage(player1.animate(),50,50,this) i
get an error at run time:

java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:50)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:736)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2755)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2745)
at Game.Game.paint(Game.java:46)
at sun.awt.RepaintArea.paint(RepaintArea.java:177)
at
sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:260)
at java.awt.Component.dispatchEventImpl(Component.java:3678)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:456)
at
java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.ja
va:201)
at
java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java
:151)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at
java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

but when i create the image in the Game class using:
getImage(codeBase(),("Game/graphics/animations/Default/0.jpg"))
it works. ive checked that the paths it uses are the same so i cant really
think why its doing it. anyway thanks for any help

Linus
 
R

Ryan Stewart

Linus Norton said:
i have an applet Game which has a Player class which in turn has an
Animation class. the Animation class loads the image into an array using
getImage and can then retrieve the current Image.

the player class has a method animate which gets the current Image from the
animation class and returns it to the Game class.

but for some reason when i call g.drawImage(player1.animate(),50,50,this) i
get an error at run time:

java.lang.NullPointerException *snip stack trace*
but when i create the image in the Game class using:
getImage(codeBase(),("Game/graphics/animations/Default/0.jpg"))
it works. ive checked that the paths it uses are the same so i cant really
think why its doing it. anyway thanks for any help

Post more code. Prefereably a small executable block that is causing the
problem:
http://www.physci.org/codes/sscce.jsp
 
L

Linus Norton

Game Class:

public void paint( Graphics g ) {
g.drawImage(players[playerIndex].animate(), 40, 40, this);
//doesnt work

g.drawImage(getImage(codeBase(),("Game/graphics/animations/Default/0.jpg")),
40, 40, this); // works
}

Player Class:

public Image animate() {
return animation.getFrame();
}

Animation Class:

public class Animation extends Game {

private Image[] animation;
private int length;
private int index;
private String source;

/** Creates a new instance of Animation */
public Animation(String source, int length) {
this.length = length;
this.source = source;
animation = new Image[length];
index = 0;

initFrames();
}

/**load in all frames of animation */
private void initFrames() {
for (int i = 0; i < length; i++)
animation = getImage(codeBase(),("Game/graphics/animations/"
+ source + "/" + i + ".jpg"));
}

/** return current animation frame */
public Image getFrame() {
if (index == length)
index = 0;

return animation[index++];
}
}


sorry for the large amount of code. Im sure that the Player and Animation
are being initalised properly and that its not the playerIndex. The
Animation constructor gets passed "Default" and 1. This has been bugging me
for a while so i appreciate your help. Thanks

Linus
 
A

Andrew Thompson

| Game Class:
|
| public void paint( Graphics g ) {

No, that is not a class, it is a snippet.
I suggest you read the link Ryan
pointed to more carefully
http://www.physci.org/codes/sscce.jsp

.....
| sorry for the large amount of code.

2Kb! You must be kidding! ;-)

People type 5Kb arguing some
obscure compiler flag issue.

If your entire entire source is under
10Kb, paste it as is.

There is a thing you should fix in the Applet
though, you have a constructor that accepts
arguments.

Applets should have an 'init()' method
that gets its parameters from the applet
parameter tags in the web page
 
L

Linus Norton

hehe, ok u asked for it buddy heres all the code:

package Game;

import java.awt.*;
import java.applet.Applet;
import java.net.URL;
/**
*
* @author Linus
*/
public class Game extends Applet {

private Player[] players;
private int playerIndex;
private URL codeBase;

/** Initialization method that will be called after the applet is loaded
* into the browser.
*/
public void init() {
players = new Player[7];
playerIndex = 0;
codeBase = getCodeBase();
initPlayers();
}

public void initPlayers() {
players[playerIndex++] = new Player("Bob", "Default");
players[playerIndex--] = new Player("Charles", "Default");
}


public void run() {
}

public void paint( Graphics g ) {
g.drawString(players[playerIndex++].name(), 10, 10);
g.drawString(players[playerIndex--].name(), 30, 10);
g.drawString(codeBase().toString(),0,100);

g.drawImage(players[playerIndex].animate(), 40, 40, this);

//g.drawImage(getImage(codeBase(),("Game/graphics/animations/Default/0.jpg")
), 40, 40, this);
}

public URL codeBase() {
return codeBase;
}
}

-----------------------------------------------------------
package Game;
import java.awt.*;
import java.applet.Applet;
/**
*
* @author Linus
*/
public class Player {

private String name;
private String type;
private Animation animation;
private int x,y;

/** Creates a new instance of Player */
public Player(String name, String type) {
this.name = name;
this.type = type;

x = y = 50;

animation = new Animation(type, 1);
}

public String name() {
return name;
}

public Image animate() {
return animation.getFrame();
}

public int x() {
return x;
}

public int y() {
return y;
}

}

--------------------------------------------
package Game;
import java.awt.*;

/**
*
* @author Linus
*/
public class Animation extends Game {

private Image[] animation;
private int length;
private int index;
private String source;

/** Creates a new instance of Animation */
public Animation(String source, int length) {
this.length = length;
this.source = source;
animation = new Image[length];
index = 0;

initFrames();
}

/**load in all frames of animation */
private void initFrames() {
for (int i = 0; i < length; i++)
animation = getImage(codeBase(),("Game/graphics/animations/"
+ source + "/" + i + ".jpg"));
//System.out.println(codeBase() + "Game/graphics/animations/" +
source + "/" + i + ".jpg");
}

/** return current animation frame */
public Image getFrame() {
if (index == length)
index = 0;

return animation[index++];
}
}
 
A

Andrew Thompson

| hehe, ok u asked for it buddy heres all the code:

Looking at your code buddy, it is obvious
you should be over on c.l.j.help.

You are obviously doing things with
little or no understanding, your code
looks like it has been cut and paste
from 3 or 4 other (bad) examples.

I got your code to the point it compiles
and runs without throwing exceptions,
but what you were actually trying to
achieve was a mystery.

Get yourself a book on Java, and
read the questions and answers of
once you
have read enough to understand
some of the basics, put your further
questions to them.

Oh, your code? This is what's left of it.

[ And you missed some important points
in http://www.physci.org/codes/sscce.jsp ]
_____________________
import java.awt.*;
import java.applet.Applet;
import java.net.URL;
import java.awt.image.BufferedImage;

/**
*
* @author Linus
*/
public class Animation extends Applet {

private Player[] players;
private int playerIndex;
private URL codeBase;

private Image[] frames;

private int length = 5;
private int index;

private int col = 20;

/** Initialization method that will be called
* after the applet is loaded
* into the browser.
*/
public void init() {
players = new Player[7];
playerIndex = 0;
codeBase = getCodeBase();
initPlayers();

frames = new Image[length];
index = 0;

initFrames();
}

/**load in all frames of animation */
private void initFrames() {
for (int i = 0; i < length; i++)
frames = getImage(new Color(col, col, col));
col+=20;
}

private Image getImage(Color c)
{
BufferedImage bi = new
BufferedImage( 20, 20, BufferedImage.TYPE_INT_RGB );
Graphics g = bi.getGraphics();
g.setColor( c );
g.drawRect( 0, 0, 20, 20 );
return bi;
}

/** return current animation frame */
public Image getFrame() {
if (index < length-1)
index = 0;

return frames[index++];
}

public void initPlayers() {
players[playerIndex++] = new Player("Bob");
players[playerIndex--] = new Player("Charles");
}

public void run() { }

public void paint( Graphics g ) {
g.drawString(players[playerIndex++].name(), 10, 10);
g.drawString(players[playerIndex--].name(), 30, 10);
g.drawString(getCodeBase().toString(),0,100);

g.drawImage(getFrame(), 40, 40, this);
}

}

/**
*
* @author Linus
*/
class Player {

private String name;
private int x,y;

/** Creates a new instance of Player */
public Player(String name) {
this.name = name;
x = y = 50;
}

public String name() { return name; }

public int x() { return x; }

public int y() { return y; }
}
__________________________
 
L

Linus Norton

i understand all the code and didnt get any of it from
examples, granted though it is bad code - im only just
starting java.

what im trying to achieve was just to make a working applet
that can display a players picture and later animates it,
because this is my first attempt at applets i did some strange
things with g.drawString and the playerIndex++ blah blah just for testing
stuff.

anywho i should have posted it over at help but dont be so
harsh. thanks for the help though.

Linus
 
R

Ryan Stewart

Andrew Thompson said:
| hehe, ok u asked for it buddy heres all the code:

Looking at your code buddy, it is obvious
you should be over on c.l.j.help.

You are obviously doing things with
little or no understanding, your code
looks like it has been cut and paste
from 3 or 4 other (bad) examples.

I got your code to the point it compiles
and runs without throwing exceptions,
but what you were actually trying to
achieve was a mystery.

Get yourself a book on Java, and
read the questions and answers of
once you
have read enough to understand
some of the basics, put your further
questions to them.

I have to agree with Andrew here. I see what you're trying to do, but even
if you get this to work, there's no way you'll be able to deal with the
needless complexity of it later on as it grows. Get a good basic Java book
and learn about the language and OOP before you jump into something like
this. Animation should never in a thousand years extend Game.

I think the answer to your original question is that codebase in Animation
is null when you try to get an Image. Your code should be throwing all kinds
of NullPointerExceptions. As a first step, I'd recommend getting rid of your
Animation class. Put the code from there in Player. Split the code into two
methods (mainly): render(Graphics g) and animate(). The first will draw the
Player in its current state to the given Graphics object. The second will
change the Player's current state. Until you get this working, drop the
codebase idea and just hardcode the image paths. You can figure out how to
implement that later.
 
A

Andrew Thompson

| i understand all the code

As long as you are deluding yourself of that
Linus, you will get nowhere.

The code appeared to have been written either
by someone very new to java, who was using
things way beyond their level, or a complete idiot.

(shrugs) I gave you the benefit of the doubt.

| ..but dont be so
| harsh.

Your amazement that 4Kb of code is OK to
post to the group indicates you have not read
the group for a while prior to posting.

You should read the group, check the FAQ
and read the JavaDocs before posting.

You did not read the advice provided
in the SSCCE carefully either.

I had to remove the package statements,
demote classes to default and shorten various
lines before I could even begin working
on your code.

Don't be such a spoilt, precious,
self absorbed, prat.
 
S

Stephen Gilbert

Hi,

Look in the Animation initFrames() method. You aren't
doing anything to wait for the images to arrive. You
are just calling getImage(). I think you need to use
a MediaTracker or some other method to wait for your
images to actually arrive.

--Steve
 
R

Ryan Stewart

Stephen Gilbert said:
Hi,

Look in the Animation initFrames() method. You aren't
doing anything to wait for the images to arrive. You
are just calling getImage(). I think you need to use
a MediaTracker or some other method to wait for your
images to actually arrive.

--Steve

Whether that's true or not, he'll never get the images because he's not
giving it a valid path. See my other post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top