想作图java, 每次按button都报错,空指针异常
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问题,不知道怎么改

2个回答

你这个一大堆代码,首先没人愿意看,其次报错你也不说报什么错,那么你就是想人家再去给你拷贝代码测试一遍? 这样不现实的。有问题就说问题就好了,哪里有问题哪个具体的方法贴出来就是了 ,哪一行代码报错 报什么错 贴代码 请问怎么解决? 你这样确实没人愿意解决的 你重新贴一下吧

qq_35728177
Tsui丶 对于你这个问题:空指针异常,首先在这个位置打断点,然后看一下你的g有没有获取到,虽然不用看这个g是肯定没有拿到的。具体的解决可以试下 Graphics g = new Graphics();
接近 3 年之前 回复

只看到Graphics g,没有看到画笔的初始化

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