当我在Japplet的地方安装JPanel但是我想让它在网页上运行时,这很好用。我错过了JApplet所需的东西吗?从错误消息我最好的猜测是我不能根据错误绘制graphics2d,但它似乎很奇怪。当我运行它时,我收到此错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at koopa.draw(koopa.java:144)
at koopa.paint(koopa.java:136)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(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)
-
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
public class koopa extends JApplet implements KeyListener,
ActionListener {
// DECLARE ALL INSTANCE VARIABLES HERE..
private ArrayList<Rectangle> aliens = new ArrayList<Rectangle>();
private Astronaut Luigi = new Astronaut(140, 508);
private static Astronaut Mario = new Astronaut(200, 508);
public static final Rectangle BORDER = new Rectangle(0, 0, 980, 620);
private int frameCount;// used for the score
private String title = "~~ Escape From The Koopas ~~ .. (click here first) .. "
+ "CONTROLS: arrows = move, space = jump, r = restart .. ..........";
private Timer timer;// handles animation
private static Image offScreenBuffer;// needed for double buffering graphics
private Graphics offScreenGraphics;// needed for double buffering graphics
private int highScore = 0;
private boolean mAlive = true;
private boolean lAlive = true;
private int mWins = 0;
private int lWins = 0;
private Font f = new Font("Dialog", Font.PLAIN, 16);
private BufferedImage[] img = new BufferedImage[10];
/**
* main() is needed to initialize the window.<br>
* THIS METHOD SHOULD NOT BE MODIFIED! .. <br>
* you should write all necessary initialization code in initRound()
*/
public static void main(String[] args) {
JFrame window = new JFrame("Escape From Rectangulus");
window.setBounds(100, 100, 980, 630);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
koopa game = new koopa();
game.setBackground(Color.BLACK);
window.getContentPane().add(game);
window.setVisible(true);
game.init();
window.addKeyListener(game);
}
/**
* init method needed to initialize non-static fields<br>
* THIS METHOD SHOULD NOT BE MODIFIED! ..
*/
public void init() {
offScreenBuffer = createImage(getWidth(), getHeight());// should be
// 800x400
offScreenGraphics = offScreenBuffer.getGraphics();
timer = new Timer(30, this);
// timer fires every 30 milliseconds.. invokes method actionPerformed()
// luigi sprites
try {
img[0] = ImageIO.read(getClass().getResource("luigiRunLeft.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
img[1] = ImageIO.read(getClass().getResource("luigiRunRight.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
img[2] = ImageIO.read(getClass().getResource("luigiJumpLeft.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
img[3] = ImageIO.read(getClass().getResource("luigiJumpRight.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
// mario sprites
try {
img[4] = ImageIO.read(getClass().getResource("marioRunLeft.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
img[5] = ImageIO.read(getClass().getResource("marioRunRight.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
img[6] = ImageIO.read(getClass().getResource("marioJumpLeft.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
img[7] = ImageIO.read(getClass().getResource("marioJumpRight.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
img[8] = ImageIO.read(getClass().getResource("koopa.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
try {
img[9] = ImageIO.read(getClass().getResource("bg.png"));
} catch (IOException ex) {
ex.printStackTrace();
}
initRound();
}
/**
* initializes all fields needed for each round of play (i.e. restart)
*/
public void initRound() {
frameCount = 0;
Luigi = new Astronaut(140, 460);
Mario = new Astronaut(200, 460);
aliens = new ArrayList<Rectangle>();
mAlive = true;
lAlive = true;
}
/**
* Called automatically after a repaint request<br>
* THIS METHOD SHOULD NOT BE MODIFIED! ..
*/
public void paint(Graphics g) {
draw((Graphics2D) offScreenGraphics);
g.drawImage(offScreenBuffer, 0, 0, this);
}
/**
* renders all objects to Graphics g
*/
public void draw(Graphics2D g) {
super.paint(g);// refresh the background
if (mAlive == true) {
Mario.draw(g);
}
if (lAlive == true) {
Luigi.draw(g);
}
g.drawImage(img[9], 0, 0, null);
int lx = Luigi.getBody().x;
int ly = Luigi.getBody().y;
int mx = Mario.getBody().x;
int my = Mario.getBody().y;
g.setColor(Color.green);
if (lAlive == true) {
if (Luigi.getDirection() == -1 && ly >= 420)
g.drawImage(img[0], lx, ly, null);
else if (Luigi.getDirection() == -1 && ly < 420)
g.drawImage(img[2], lx, ly, null);
else if (Luigi.getDirection() == 1 && ly < 420)
g.drawImage(img[3], lx, ly, null);
else
g.drawImage(img[1], lx, ly, null);
}
g.setColor(Color.RED);
if (mAlive == true) {
if (Mario.getDirection() == -1 && my >= 420)
g.drawImage(img[4], mx, my, null);
else if (Mario.getDirection() == -1 && my < 420)
g.drawImage(img[6], mx, my, null);
else if (Mario.getDirection() == 1 && my < 420)
g.drawImage(img[7], mx, my, null);
else
g.drawImage(img[5], mx, my, null);
}
g.setColor(Color.WHITE);
g.drawString(title, 100, 20);// draw the title towards the top
g.drawRect(BORDER.x, BORDER.y, BORDER.width - 1, BORDER.height - 1);
g.setFont(f);
g.drawString("Score: " + frameCount + " Highscore: " + highScore, 450,
100);// approximate middle
g.setColor(Color.BLACK);
for (Rectangle Alien : aliens) {
g.drawRect(Alien.x, Alien.y, Alien.width, Alien.height);
g.drawImage(img[8], Alien.x, Alien.y, null);
}
// YOUR CODE GOES HERE..
// render all game objects here
}
/**
* Called automatically when the timer fires<br>
* this is where all the game fields will be updated
*/
public void actionPerformed(ActionEvent e) {
Luigi.move();
Mario.move();
if (Math.random() < 0.05) {
aliens.add(new Rectangle(BORDER.width,
448 - (int) (Math.random() * 40),
(int) (Math.random() * 15 + 1), 0));
}
for (Rectangle Alien : aliens) {
Alien.x -= Alien.width;
}
if (aliens.size() < 0 && aliens.get(0).x == 0) {
aliens.remove(0);
}
/**
* write a loop to subtract the width of each Rectangle from its
* x-coordinate write the code to remove the first Rectangle of aliens
* if its x-coordinate is less than 0
**/
Rectangle Alien;
for (int i = 0; i < aliens.size(); i++) {
Alien = aliens.get(i);
// checks right area for intersection but keeps velocity random
if (Luigi.getBody().intersects(Alien.x, Alien.y, 30, 32) == true) {
lAlive = false;
if (mAlive == false) {
timer.stop();
}
if (frameCount > highScore) {
highScore = frameCount + 1;
}
}
if (Mario.getBody().intersects(Alien.x, Alien.y, 30, 32) == true) {
mAlive = false;
if (lAlive == false) {
timer.stop();
}
if (frameCount > highScore) {
highScore = frameCount + 1;
}
}
}
frameCount++;// used for the score
repaint();// needed to refresh the animation
}
/**
* handles any key pressed events and updates the player's direction by
* setting their direction to either 1 or -1 for right or left respectively
* and updates their jumping status by invoking jump()
*/
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT) {
Luigi.setDirection(-1);
} else if (keyCode == KeyEvent.VK_RIGHT) {
Luigi.setDirection(1);
} else if (keyCode == KeyEvent.VK_UP) {
Luigi.jump();
}
if (keyCode == KeyEvent.VK_A) {
Mario.setDirection(-1);
} else if (keyCode == KeyEvent.VK_D) {
Mario.setDirection(1);
} else if (keyCode == KeyEvent.VK_W) {
Mario.jump();
}
}
/**
* handles any key released events and updates the player's direction by
* setting their direction to 0 if they need to stop and restarts the game
* if the Timer is not running and user types 'r'
*/
public void keyReleased(KeyEvent e) {
int keyCode = e.getKeyCode();
if (keyCode == KeyEvent.VK_LEFT && Luigi.getDirection() == -1) {
Luigi.setDirection(0);
} else if (keyCode == KeyEvent.VK_RIGHT && Luigi.getDirection() == 1) {
Luigi.setDirection(0);
} else if (keyCode == KeyEvent.VK_R) {
if (!timer.isRunning()) {
timer.start();
initRound();
}
} else if (keyCode == KeyEvent.VK_A && Mario.getDirection() == -1) {
Mario.setDirection(0);
} else if (keyCode == KeyEvent.VK_D && Mario.getDirection() == 1) {
Mario.setDirection(0);
}
}
/**
* this method is needed for implementing interface KeyListener<br>
* THIS METHOD SHOULD NOT BE MODIFIED! ..
*/
public void keyTyped(KeyEvent e) {
}
}// end class EscapeFromRectangulus