|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
package 坦克大战;
import java.awt.*;
import javax.swing.*;
public class 画出坦克 extends JFrame{
MyPanel mp = null;
public static void main(String[] args){
画出坦克 tank = new 画出坦克();
}
public 画出坦克(){
mp = new MyPanel();
this.add(mp);
this.setSize(300,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
//我的面板
class MyPanel extends JPanel{
Hero hero = null; //定义我的坦克
//构造函数
public MyPanel(){
hero = new Hero(10,10); //初始位置
}
public void paint(Graphics g){
super.paint(g);
g.fillRect(0, 0, 400, 300);
this.drawTank(hero.getX(), hero.getY(), g, 0, 0);
}
//画出坦克的函数
public void drawTank(int x, int y, Graphics g, int direct, int type){
//判断坦克类型
switch(type){
case 0: g.setColor(Color.cyan); break;
case 1: g.setColor(Color.yellow); break;
}
//判断坦克方向
switch(direct){
//向上
case 0: g.setColor(Color.cyan);
//画出我的坦克
//1.左边的矩形
g.fill3DRect(x, y, 5, 30, false);
//2.右边的矩形
g.fill3DRect(x+15, y, 5, 30, false);
//3.中间的矩形
g.fill3DRect(x+5, y+5, 10, 20, false);
//画出圆形
g.fillOval(x+5, y+10, 10, 10);
//画出线
g.drawLine(x+10, y+15, x+10, y);
break;
}
}
}
//坦克类
class Tank{
int x = 0; //坦克的横坐标
int y = 0; //坦克的纵坐标
public Tank(int x, int y){
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
//我的坦克
class Hero extends Tank{
public Hero(int x, int y){
super(x,y);
}
}
运行这个程序就抛出异常了,不知道怎么回事,我的jdk是1.8的
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at sun.awt.SunToolkit.getImageFromHash(SunToolkit.java:725)
at sun.awt.SunToolkit.getImage(SunToolkit.java:759)
at 坦克大战.MyPanel.paint(java绘图.java:63)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paint(JComponent.java:1065)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
at javax.swing.JComponent.paintChildren(JComponent.java:889)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent.paint(JComponent.java:1042)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:79)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:116)
at java.awt.Container.paint(Container.java:1975)
at java.awt.Window.paint(Window.java:3904)
at javax.swing.RepaintManager$4.run(RepaintManager.java:842)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
|
|