Ich habe in das Spiel ein VotilaImage eingebaut mit es flüssiger läuft und nicht so lange arbeitet. Unf geanu
das Votla Image hat jetzt eine Magge in Zeile 120 hier untem im Quellcode ist der Fehler laut Fehlermeldung.
Ich bekomme bai dem Spiel immer eine NullPointerException und
Das ist meine Fehlermeldung:
java.lang.NullPointerException
at PingPong2.paint(PingPong2.java:130)
at java.awt.Container.update(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.motif.MComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
So ich gebe auch gleich mal den ganzen Code.
das Votla Image hat jetzt eine Magge in Zeile 120 hier untem im Quellcode ist der Fehler laut Fehlermeldung.
Ich bekomme bai dem Spiel immer eine NullPointerException und
Das ist meine Fehlermeldung:
java.lang.NullPointerException
at PingPong2.paint(PingPong2.java:130)
at java.awt.Container.update(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.motif.MComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
So ich gebe auch gleich mal den ganzen Code.
Code:
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Arc2D;
import java.awt.geom.AffineTransform;
import javax.swing.*;
import java.awt.image.VolatileImage;
import java.net.URL;
public class PingPong2 extends Frame implements Runnable, MouseMotionListener
{
Thread t;
boolean bWeiter=true;
private Image dbImage;
private Graphics dbg;
Image img;
Label Lstunden;
Label Lminuten;
Label Lsekunden;
Label Doppelpunkt;
Image Hintergrund;
Image Ball;
int Ballx=450;
int Bally=265;
int p=10;
int px=p;
int py=p;
int ypl=200;
int Cypl=200;
int w=970;
int h=580;
VolatileImage vImg = createVolatileImage(w, h);
public PingPong2()
{
addWindowListener(new WindowAdapter()
{public void windowClosing (WindowEvent evt) {System.exit(0);}});
setSize(970,580);
setBackground(Color.black);
setTitle("Ping Pong 2");
setVisible(true);
setLayout(null);
addMouseMotionListener(this);
URL url = null;
url = this.getClass().getResource("weltraum.jpg");
Hintergrund = getToolkit().getImage(url);
url = this.getClass().getResource("ball.gif");
Ball = getToolkit().getImage(url);
start();
}
public static void main (String[] args)
{
new PingPong2();
}
public void start()
{
t = new Thread(this);
t.start();
}
public void stopp()
{
bWeiter=false;
}
public void run()
{
while (bWeiter)
{
try
{
t.sleep(0);
}
catch (Exception e) { }
if((Ballx+10)>=970) // Ballrichtun rechts
{
px=-p;
}
if(Ballx<=0)
{
px=+p;
}
if((Bally+10)>=580) // Ballrichtun rechts
{
py=-p;
}
if(Bally<=25)
{
py=+p;
}
if((Ballx<80) && Bally>(ypl-50) && Bally<(ypl+50)) // ist am Schlaeger
{
px=+p;
}
if((Ballx>890) && Bally<(Cypl+50) && Bally>(Cypl-50)) // Computer schl?ger
{
px=-p;
}
Bally=Bally+py;
Ballx=Ballx+px;
Cypl=Bally;
repaint();
}
}
public void renderOffscreen() {
do {
if (vImg.validate(getGraphicsConfiguration()) ==
VolatileImage.IMAGE_INCOMPATIBLE)
{
// old vImg doesn't work with new GraphicsConfig; re-create it
vImg = createVolatileImage(w, h);
}
Graphics2D g = vImg.createGraphics();
g.dispose();
} while (vImg.contentsLost());
}
public void paint (Graphics g)
{
do {
int returnCode = vImg.validate(getGraphicsConfiguration()); // Laut Fehlermeldung müsste der Code hier sein.
if (returnCode == VolatileImage.IMAGE_RESTORED) {
// Contents need to be restored
renderOffscreen(); // restore contents
} else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
// old vImg doesn't work with new GraphicsConfig; re-create it
vImg = createVolatileImage(w, h);
renderOffscreen();
}
g.drawImage(vImg, 0, 0, this);
} while (vImg.contentsLost());
g.drawImage(Ball,400,400,this);
g.setColor(Color.red);
g.fillOval(Ballx,Bally,10,10);
g.setColor(Color.gray);
g.fillRect(50,ypl-30,10,100);
g.fillRect(910,Cypl-30,10,100);
System.out.println("Paint aufgerufen");
}
public void keyTyped(KeyEvent e) // Tastendefinition
{
}
public void mouseDragged(MouseEvent me)
{
}
public void mouseMoved(MouseEvent me)
{
ypl=me.getY();
}
}
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Arc2D;
import java.awt.geom.AffineTransform;
import javax.swing.*;
import java.awt.image.VolatileImage;
import java.net.URL;
public class PingPong2 extends Frame implements Runnable, MouseMotionListener
{
Thread t;
boolean bWeiter=true;
private Image dbImage;
private Graphics dbg;
Image img;
Label Lstunden;
Label Lminuten;
Label Lsekunden;
Label Doppelpunkt;
Image Hintergrund;
Image Ball;
int Ballx=450;
int Bally=265;
int p=10;
int px=p;
int py=p;
int ypl=200;
int Cypl=200;
int w=970;
int h=580;
VolatileImage vImg = createVolatileImage(w, h);
public PingPong2()
{
addWindowListener(new WindowAdapter()
{public void windowClosing (WindowEvent evt) {System.exit(0);}});
setSize(970,580);
setBackground(Color.black);
setTitle("Ping Pong 2");
setVisible(true);
setLayout(null);
addMouseMotionListener(this);
URL url = null;
url = this.getClass().getResource("weltraum.jpg");
Hintergrund = getToolkit().getImage(url);
url = this.getClass().getResource("ball.gif");
Ball = getToolkit().getImage(url);
start();
}
public static void main (String[] args)
{
new PingPong2();
}
public void start()
{
t = new Thread(this);
t.start();
}
public void stopp()
{
bWeiter=false;
}
public void run()
{
while (bWeiter)
{
try
{
t.sleep(0);
}
catch (Exception e) { }
if((Ballx+10)>=970) // Ballrichtun rechts
{
px=-p;
}
if(Ballx<=0)
{
px=+p;
}
if((Bally+10)>=580) // Ballrichtun rechts
{
py=-p;
}
if(Bally<=25)
{
py=+p;
}
if((Ballx<80) && Bally>(ypl-50) && Bally<(ypl+50)) // ist am Schlaeger
{
px=+p;
}
if((Ballx>890) && Bally<(Cypl+50) && Bally>(Cypl-50)) // Computer schl?ger
{
px=-p;
}
Bally=Bally+py;
Ballx=Ballx+px;
Cypl=Bally;
repaint();
}
}
public void renderOffscreen() {
do {
if (vImg.validate(getGraphicsConfiguration()) ==
VolatileImage.IMAGE_INCOMPATIBLE)
{
// old vImg doesn't work with new GraphicsConfig; re-create it
vImg = createVolatileImage(w, h);
}
Graphics2D g = vImg.createGraphics();
g.dispose();
} while (vImg.contentsLost());
}
public void paint (Graphics g)
{
do {
int returnCode = vImg.validate(getGraphicsConfiguration());
if (returnCode == VolatileImage.IMAGE_RESTORED) {
// Contents need to be restored
renderOffscreen(); // restore contents
} else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
// old vImg doesn't work with new GraphicsConfig; re-create it
vImg = createVolatileImage(w, h);
renderOffscreen();
}
g.drawImage(vImg, 0, 0, this);
} while (vImg.contentsLost());
g.drawImage(Ball,400,400,this);
g.setColor(Color.red);
g.fillOval(Ballx,Bally,10,10);
g.setColor(Color.gray);
g.fillRect(50,ypl-30,10,100);
g.fillRect(910,Cypl-30,10,100);
System.out.println("Paint aufgerufen");
}
public void keyTyped(KeyEvent e) // Tastendefinition
{
}
public void mouseDragged(MouseEvent me)
{
}
public void mouseMoved(MouseEvent me)
{
ypl=me.getY();
}
}