空指针异常java,想做个画图板,
package control;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JButton;

import elements.Circle;
import elements.Elements;
import elements.Line;
import elements.Rectangle;

public class Control implements ActionListener, MouseListener, MouseMotionListener {
    Graphics g;


    public String doit = "";
    public int x1, y1, x2, y2; //鼠标的按下和释放时的坐标
//  private Color curColor;//当前画笔颜色
    ArrayList <Elements> drawX = new ArrayList <Elements>();

    public ArrayList<Elements> getDrawX() {
        return drawX;
    }


    public void setDrawX(ArrayList<Elements> drawX) {
        this.drawX = drawX;
    }

    public void setG(Graphics g)
    {
        this.g= g;
    }//传递画笔

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(""))
        {           
            Color newcolor = new Color((int) e.getSource());
            g.setColor(newcolor);       
        }
        else
        {
            doit = e.getActionCommand();      
            JButton buttonblack = new JButton();
            buttonblack.setBackground(java.awt.Color.black);
            Color coloruse = buttonblack.getBackground();    
            g.setColor(coloruse);           
        }           
    }


    @Override
    public void mouseMoved(MouseEvent e) {

    }
    @Override
    public void mouseClicked(MouseEvent e) {

    }
    @Override
    public void mousePressed(MouseEvent e) {
        System.out.println("2");//   鼠标点下  点x1,y1
        x1 = e.getX();        
        y1 = e.getY();    
    }
    @Override
    public void mouseReleased(MouseEvent e) {
        x2=e.getX();
        y2=e.getY();

    }
    @Override
    public void mouseEntered(MouseEvent e) {

    }
    @Override
    public void mouseExited(MouseEvent e) {

    }


    @Override
    public void mouseDragged(MouseEvent e) {
        System.out.println("3");
        if ( doit.equals("circle"))
        {
            drawX.add(new Circle(x1, y1, x2, y2));
        }
        if ( doit.equals("rectangle"))
        {
            drawX.add(new Rectangle(x1, y1, x2, y2));           
        }
        if ( doit.equals("line"))
        {
            drawX.add(new Line(x1, y1, x2, y2));            
        }   
    }

}





=============================
package view;

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

import control.Control;

import java.awt.event.*;


public class Gui extends JFrame {
    public Control drawit;



    private static final long serialVersionUID = 1L;

    public Gui()
    {
        JFrame frame = new JFrame("miniCAD");
        JPanel panelBig = new JPanel();//画板

        JPanel panelSmall  = new JPanel();//图形和颜色板
        JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelBig, panelSmall);          
        panelBig.setBackground(Color.white);
        panelSmall.setBackground(Color.white);
        pane.setRightComponent(panelSmall);
        pane.setLeftComponent(panelBig);   
        pane.setContinuousLayout(true);
        pane.setOneTouchExpandable(true);
//      Container C = frame.getContentPane();
//      C.add(pane);
        frame.add(pane);
//添加图形              
        JButton buttoncircle = new JButton("circle");
        JButton buttonline = new JButton("line");
        JButton buttonrectangle = new JButton("rectangle");
        JButton buttontext = new JButton("text");
        JButton buttontriangle = new JButton("triangle");
        frame.setVisible(true);

        panelSmall.setLayout(new GridLayout(6,1,0,0));     
        panelSmall.add(buttoncircle);
        panelSmall.add(buttonline);
        panelSmall.add(buttonrectangle);
        panelSmall.add(buttontext);
        panelSmall.add(buttontriangle);


//监听使用哪个图形  
        control.Control monitor = new control.Control();
        buttoncircle.addActionListener(monitor);
        buttonline.addActionListener(monitor);
        buttonrectangle.addActionListener(monitor);
        buttontext.addActionListener(monitor);
        buttontriangle.addActionListener(monitor);  

//添加颜色板     
        JPanel colorpane = new JPanel();
        panelSmall.add(colorpane);
        colorpane.setLayout(new GridLayout(3,3));
        JButton buttonred = new JButton();
        buttonred.setBackground(Color.red);
        colorpane.add(buttonred);
        JButton buttonblack = new JButton();
        buttonblack.setBackground(Color.black);
        colorpane.add(buttonblack);
        JButton buttonblue = new JButton();
        buttonblue.setBackground(Color.blue);
        buttonblue.setContentAreaFilled(false);
        colorpane.add(buttonblue);
        JButton buttongreen = new JButton();
        buttongreen.setBackground(Color.green);
        colorpane.add(buttongreen);
        JButton buttongray = new JButton();
        buttongray.setBackground(Color.gray);
        colorpane.add(buttongray);
        JButton buttonwhite = new JButton();
        buttonwhite.setBackground(Color.white);
        colorpane.add(buttonwhite);
        JButton buttonyellow = new JButton();
        buttonyellow.setBackground(Color.yellow);
        colorpane.add(buttonyellow);
        JButton buttonpink = new JButton();
        buttonpink.setBackground(Color.pink);
        colorpane.add(buttonpink);
        JButton buttonorange = new JButton();
        buttonorange.setBackground(Color.orange);
        colorpane.add(buttonorange);
        frame.setVisible(true);

//添加颜色监听器
        control.Control color = new control.Control();
        buttonred.addActionListener(color);
        buttonblack.addActionListener(color);
        buttonblue.addActionListener(color);
        buttongreen.addActionListener(color);
        buttongray.addActionListener(color);
        buttonwhite.addActionListener(color);
        buttonyellow.addActionListener(color);
        buttonpink.addActionListener(color);
        buttonorange.addActionListener(color);

        control.Control drawit  = new control.Control();
        panelBig.addMouseListener(drawit);
        panelBig.addMouseMotionListener(drawit);
        drawit.setG(panelBig.getGraphics());
    }   

    public static void main(String[] args)
    {
        Gui pic = new Gui();
    }
}





在control类里
g.setColor(coloruse);这句报错,查了下资料,我想可能是Graphics g问题或者是JPanel问题,不知道怎么改

每次点击按钮,报错空指针异常

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐