0

他我是java初学者,我正在做一个需要画汽车的练习。我书中的代码不起作用,我不知道为什么。

我不是 java 的菜鸟,我知道类、对象、方法等,但如果涉及到语法的确切行为,我仍然是菜鸟。

这是代码:

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class Vb1101 extends JFrame {
    public static void main( String args[] ){
        JFrame frame = new Vb1101();
        frame.setSize(350,200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("exercise 1101 draw car parts");
        frame.setContentPane( new Carpanel() );
        frame.setVisible( true );
    }
}

class Carpanel extends JPanel {
    public Car car;

    public void Carpanel(){
        car = new Car( 20, 150, 80, 30 );
    }

    public void paintComponent( Graphics g ){
        super.paintComponent(g);
        setBackground(Color.WHITE);
        car.teken(g);
    }
}

 class Car {
    public ArrayList<Part> parts;

    public Car( int left, int under, int width, int height){
        parts = new ArrayList<Part>();
        int wielgrootte = 20;
        //int carosOnder = onder - wielgrootte / 2;

        //de carosserie
        parts.add( new Rectangle( Color.BLUE, left, under - 10, width, height ) );

        // De cabine
        parts.add( new Rectangle( Color.CYAN, left, under - 10 - height, 4 * width / 5, 4 * height / 5 ) );

        // het achterwiel
        parts.add( new Circle( Color.YELLOW, left + 5, under, wielgrootte) );

        // het voorwiel
        parts.add( new Circle( Color.YELLOW, left + width - 30, under, wielgrootte ) );
    }

    public void teken( Graphics g ){
        for(Part part : parts){
            part.teken( g );  //polymorfie says my book
        }
    }

    public interface Part {
        public void teken( Graphics g ); //abstract method says my book
    }

    public class Rectangle implements Part {
        public int left, under, width, height;
        public Color color;

        public Rectangle (Color color, int left, int under, int width, int height){
            this.color = color;
            this.left = left;
            this.under = under;
            this.width = width;
            this.height = height;
        }

        public void teken ( Graphics g ){
            g.setColor( color );
            g.fillRect(left, under - height, width, height);
            g.setColor(Color.black);
            g.drawRect(left, under - height, width, height);
        }
    }

    public class Circle implements Part {
        public int left, under, diameter;
        public Color color;

        public Circle( Color color, int left, int under, int diameter ){
            this.color = color;
            this.left = left;
            this.under = under;
            this.diameter = diameter;
        }

        public void teken( Graphics g ){
            g.setColor(color);
            g.fillOval(left, under - diameter, diameter, diameter);
            g.setColor(Color.black);
            g.drawOval(left, under - diameter, diameter, diameter);
        }
    }
}

我的书说它必须起作用,但日食说:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at Carpanel.paintComponent(Vb1101.java:26)
  at javax.swing.JComponent.paint(Unknown Source)
  at javax.swing.JComponent.paintChildren(Unknown Source)
  at javax.swing.JComponent.paint(Unknown Source)
  at javax.swing.JLayeredPane.paint(Unknown Source)
  etc...

我不明白为什么!?有人知道为什么代码会抛出异常吗?起初我在 Eclipse 中看不到任何错误,但是当我运行时,问题就开始了。我确实有窗户,但没有车。

4

1 回答 1

8

从构造函数中删除void关键字,Carpanel以便car初始化实例

public void Carpanel() {

应该

public Carpanel() {
于 2013-06-07T14:38:00.513 回答