0

我有一个带有单选按钮的小程序来选择线条的颜色,但是当我尝试运行它时,我收到了这个错误:

java.lang.NullPointerException

这就是我迄今为止所拥有的。关于使颜色选择起作用的任何建议?

import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.awt.event.*;

public class ProjectPaint extends Applet {

  private JRadioButton redButton = new JRadioButton("Red");
  private JRadioButton blueButton = new JRadioButton("Blue");
  private JRadioButton greenButton = new JRadioButton("Green");
  private JRadioButton blackButton = new JRadioButton("Black");
    Graphics g;

  public void init() {
    // Create a border layout design
     setLayout(new BorderLayout());
    // Make a new object of type DrawPanel
     DrawPanel dp = new DrawPanel();
    // Add a draw panel in the center
     add("Center", dp);
    // Add another draw panel for color selection on top
     add("North",new DrawControls(dp));
  }

  int x1;
  int y1;
  class DrawPanel extends Panel
  {
     public boolean mouseDown(Event e, int x, int y)
     {
     // User has started a mouse drag.
     // Remember where:
        x1 = x;
        y1 = y;

        return true;
     }

     public boolean mouseDrag(Event e, int x, int y)
     {
     // User is continuing a mouse drag.
     // Draw line from last point to this
     // point:
            g = getGraphics();
        g.drawLine( x1,y1, x,y );

     // Remember new "last point":
        x1 = x;
        y1 = y;

        return true;
     }
  }
  class DrawControls extends Panel
  {
     public DrawControls(DrawPanel target)
     {
        setLayout(new BorderLayout());
        JPanel panel = new JPanel();
        ButtonGroup radioButtonGroup = new ButtonGroup();
        radioButtonGroup.add(redButton);
        radioButtonGroup.add(blueButton);
        radioButtonGroup.add(greenButton);
        radioButtonGroup.add(blackButton);
        panel.add(redButton);
        panel.add(blueButton);
        panel.add(greenButton);
        panel.add(blackButton);
        add(panel, BorderLayout.NORTH);
        redButton.addActionListener(new RadioButtonListener());
        blueButton.addActionListener(new RadioButtonListener());
        greenButton.addActionListener(new RadioButtonListener());
        blackButton.addActionListener(new RadioButtonListener());
            blackButton.doClick();
     }
  }
  private class RadioButtonListener implements ActionListener
  {
     public void actionPerformed(ActionEvent e)
     {
            int color = -1;
        if(e.getSource() == redButton)
        {
                g.setColor(Color.red);
        }
        if(e.getSource() == blueButton)
        {
                g.setColor(Color.blue);
        }
        if(e.getSource() == greenButton)
        {
                g.setColor(Color.green);
        }
        if(e.getSource() == blackButton)
        {
                g.setColor(Color.black);
        }
     }
  }
}
4

1 回答 1

3

Graphics引用尚未分配,因此NPE.

不要尝试使用ActionListeneror进行任何自定义绘画MouseListener。而是使用Color类成员变量来设置颜色。对于 Swing 应用程序,所有自定义绘制都应在该paintComponent方法中完成。在这种情况下DrawPanel,将需要更改以进行扩展JPanel,以便它可以覆盖该方法。添加@Override注释并确保调用super.paintComponent(g).

JApplet支持 JFC/Swing 组件架构,所以也使用它。

于 2013-05-28T18:10:44.070 回答