S
SenioreFamicom
Gast
Hallo Leute 
Ich habe ein JDialog (im nachfolgenden Quellcode nicht enthalten), das ein kleines JPanel enthält, dass wiederrum ausgewählte Bildsegmente als Animation darstellt. Die Bildsegmente werden als Image []-Array an das JPanel tileAniPanel übergeben, dass wiederrum die Klasse Animation aufruft. Allerdings will das Programm nicht so ganz, das was ich will. Es wird nämlich nichts dargestellt. Stattdessen bekomme ich die mir nicht ganz wohl gesonnene Fehlermeldung (ich zitiere):
"Exception occurred during event dispatching: java.lang.NullPointerException
at SN32.Editor.LowJClass.JTileAniPanel.draw(JTileAniPanel.java:55)
at SN32.Editor.LowJClass.JTileAniPanel.animationLoop(JTileAniPanel.java:43)
at SN32.Editor.LowJClass.JTileAniPanel.run(JTileAniPanel.java:31)
at SN32.Editor.LowJClass.JTileAniPanel.<init>(JTileAniPanel.java:17)
at SN32.Editor.TabTile.JTilePropReader.mousePressed(JTilePropReader.java:366)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
[...]"
Woran kann dies liegen?
Der Code:
Ich habe ein JDialog (im nachfolgenden Quellcode nicht enthalten), das ein kleines JPanel enthält, dass wiederrum ausgewählte Bildsegmente als Animation darstellt. Die Bildsegmente werden als Image []-Array an das JPanel tileAniPanel übergeben, dass wiederrum die Klasse Animation aufruft. Allerdings will das Programm nicht so ganz, das was ich will. Es wird nämlich nichts dargestellt. Stattdessen bekomme ich die mir nicht ganz wohl gesonnene Fehlermeldung (ich zitiere):
"Exception occurred during event dispatching: java.lang.NullPointerException
at SN32.Editor.LowJClass.JTileAniPanel.draw(JTileAniPanel.java:55)
at SN32.Editor.LowJClass.JTileAniPanel.animationLoop(JTileAniPanel.java:43)
at SN32.Editor.LowJClass.JTileAniPanel.run(JTileAniPanel.java:31)
at SN32.Editor.LowJClass.JTileAniPanel.<init>(JTileAniPanel.java:17)
at SN32.Editor.TabTile.JTilePropReader.mousePressed(JTilePropReader.java:366)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:263)
[...]"
Woran kann dies liegen?
Der Code:
Code:
package SN32.Editor.LowJClass;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
public class JTileAniPanel extends JPanel {
private Animation anim;
private Image [] img;
public JTileAniPanel(Image [] img) {
this.img = img;
this.setSize(96, 96);
this.setLocation(16, 304 - 8);
this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Preview"));
this.run();
this.setVisible(true);
}
public void loadImages() {
anim = new Animation();
for (int i = 0; i < img.length; i++) {
anim.addFrame(img[i], 100);
}
}
public void run() {
loadImages();
animationLoop();
}
public void animationLoop() {
long startTime = System.currentTimeMillis();
long currTime = startTime;
while(currTime - startTime < 5000) {
if (img.length != -1) {
long elapsedTime = System.currentTimeMillis() - currTime;
currTime += elapsedTime;
anim.update(elapsedTime);
Graphics g = this.getGraphics();
draw(g);
g.dispose();
this.update(g);
try {
Thread.sleep(20);
} catch (InterruptedException exc) {
}
}
}
}
public void draw(Graphics g) {
g.drawImage(anim.getImage(), 0, 0, null);
}
@Override
public void paintComponent(Graphics g) {
if (anim.getImage() != null) {
draw(g);
//ani.update(24);
}
}
}
Code:
package SN32.Editor.LowJClass;
import java.awt.Image;
import java.util.ArrayList;
public class Animation {
private int currFrameIndex;
private long animTime;
private long totalDuration;
private ArrayList <AnimFrame> frames;
public Animation() {
frames = new ArrayList<AnimFrame>();
totalDuration = 0;
start();
}
public synchronized void addFrame(Image image, long duration) {
totalDuration += duration;
frames.add(new AnimFrame(image, totalDuration));
}
public synchronized void start(){
animTime = 0;
currFrameIndex = 0;
}
public synchronized void update(long elapsedTime) {
if(frames.size() > 1) {
animTime += elapsedTime;
if(animTime >= totalDuration) {
animTime = 0;
currFrameIndex = 0;
}
while(animTime > getFrame(currFrameIndex).endTime) {
currFrameIndex++;
}
}
}
public synchronized Image getImage() {
if(frames.size() == 0) {
return null;
}
return getFrame(currFrameIndex).image;
}
private AnimFrame getFrame(int i){
return frames.get(i);
}
private class AnimFrame{
long endTime;
Image image;
public AnimFrame(Image image, long endTime) {
this.image = image;
this.endTime = endTime;
}
}
}