java - 如何为g.drawLine选择颜色?

标签 java swing graphics applet mixing

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

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);
        }
     }
  }
}

最佳答案

尚未分配Graphics引用,因此NPE

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

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

关于java - 如何为g.drawLine选择颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16798655/

相关文章:

java - JTextField 缺失且布局错误地放置了 JButton

java - jtree 图标缩放时的缩放大小

c# - 旋转字符串以在位图上垂直书写

java - 为什么这个 java 递归方法不创建无限循环?

java - Spring 3 MVC Hibernate 3.5.4 hibernateTemplate 不关闭连接(非事务性)

java - 在 SymmetricDS 文件同步中,通过从数据库 bean shell 脚本获取动态设置目标基本目录路径

java - 为什么这个javafx fxml不调整大小?

java - 运行Eclipse导出的jar时出现"Could not find the main class"错误

iOS 图形引擎

java - 随机和图形代码太长