Java JFrame/图形 driver
Java JFrame / Graphics driver
当我想绘制多边形时,我总是收到 NullPointerException Pizza
object。我收到此错误消息:
Exception in thread "main" java.lang.NullPointerException
at Pizza.<init>(Pizza.java:9)
at PolyDemo$PolyDemoPanel.getRandShape(PolyDemo.java:91)
at PolyDemo$PolyDemoPanel.<init>(PolyDemo.java:54)
at PolyDemo.<init>(PolyDemo.java:19)
at PolyDemo.main(PolyDemo.java:28)
我对圆形和矩形没有问题,为什么这不起作用?这是我的披萨 class:
import java.awt.*;
public class Pizza extends Shape{
private Polygon P;
public Pizza(int x, int y) {
super(x,y);
P.xpoints = new int[]{x, x+100, x+200};
P.ypoints = new int[]{y, y+100, y};
P.npoints = 3;
}
@Override
public void draw(Graphics g){
g.setColor(Color.RED);
g.drawPolygon(P);
}
}
这里是 driver:
import java.util.*;
import java.awt.*;
import javax.swing.*;
class PolyDemo extends JFrame {
public PolyDemo() {
getContentPane().add( new PolyDemoPanel());
setSize( 300,300 );
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public static void main( String args[] ) {
PolyDemo myApp = new PolyDemo();
}
public class PolyDemoPanel extends JPanel {
Shape[] myShapes= new Shape[20];
public PolyDemoPanel() {
for( int i = 0; i < 20; i++ ) {
myShapes[i] = getRandShape();
}
}
public void paint( Graphics g ) {
super.paint(g);
for( int i = 0; i < myShapes.length; i++ ){
myShapes[i].draw( g );
}
}
public int getRandInt() {
return ( (int) ( Math.random() * 200 ) );
}
public Shape getRandShape() {
Shape retVal = null;
final int x = getRandInt();
final int y = getRandInt();
retVal = new Pizza(x, y);
return retVal;
}
}
}
您没有初始化 Polygon
字段 P
。
试试这个:
public Pizza(int x, int y) {
super(x,y);
P = new Polygon();
P.xpoints = new int[]{x, x+100, x+200};
P.ypoints = new int[]{y, y+100, y};
P.npoints = 3;
}
您声明了多边形但没有创建对象。这样,当您在 Pizza 的构造函数中使用它时,它就是 null。在构造函数中使用它之前,您需要创建一个实例。 P 也是变量的错误名称
public Pizza(int x, int y) {
super(x,y);
//P is null here - add P=new Poligon()
P.xpoints = new int[]{x, x+100, x+200};
P.ypoints = new int[]{y, y+100, y};
P.npoints = 3;
}
当我想绘制多边形时,我总是收到 NullPointerException Pizza
object。我收到此错误消息:
Exception in thread "main" java.lang.NullPointerException
at Pizza.<init>(Pizza.java:9)
at PolyDemo$PolyDemoPanel.getRandShape(PolyDemo.java:91)
at PolyDemo$PolyDemoPanel.<init>(PolyDemo.java:54)
at PolyDemo.<init>(PolyDemo.java:19)
at PolyDemo.main(PolyDemo.java:28)
我对圆形和矩形没有问题,为什么这不起作用?这是我的披萨 class:
import java.awt.*;
public class Pizza extends Shape{
private Polygon P;
public Pizza(int x, int y) {
super(x,y);
P.xpoints = new int[]{x, x+100, x+200};
P.ypoints = new int[]{y, y+100, y};
P.npoints = 3;
}
@Override
public void draw(Graphics g){
g.setColor(Color.RED);
g.drawPolygon(P);
}
}
这里是 driver:
import java.util.*;
import java.awt.*;
import javax.swing.*;
class PolyDemo extends JFrame {
public PolyDemo() {
getContentPane().add( new PolyDemoPanel());
setSize( 300,300 );
setVisible( true );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public static void main( String args[] ) {
PolyDemo myApp = new PolyDemo();
}
public class PolyDemoPanel extends JPanel {
Shape[] myShapes= new Shape[20];
public PolyDemoPanel() {
for( int i = 0; i < 20; i++ ) {
myShapes[i] = getRandShape();
}
}
public void paint( Graphics g ) {
super.paint(g);
for( int i = 0; i < myShapes.length; i++ ){
myShapes[i].draw( g );
}
}
public int getRandInt() {
return ( (int) ( Math.random() * 200 ) );
}
public Shape getRandShape() {
Shape retVal = null;
final int x = getRandInt();
final int y = getRandInt();
retVal = new Pizza(x, y);
return retVal;
}
}
}
您没有初始化 Polygon
字段 P
。
试试这个:
public Pizza(int x, int y) {
super(x,y);
P = new Polygon();
P.xpoints = new int[]{x, x+100, x+200};
P.ypoints = new int[]{y, y+100, y};
P.npoints = 3;
}
您声明了多边形但没有创建对象。这样,当您在 Pizza 的构造函数中使用它时,它就是 null。在构造函数中使用它之前,您需要创建一个实例。 P 也是变量的错误名称
public Pizza(int x, int y) {
super(x,y);
//P is null here - add P=new Poligon()
P.xpoints = new int[]{x, x+100, x+200};
P.ypoints = new int[]{y, y+100, y};
P.npoints = 3;
}