先说我的思路:首先继承JPanel,叫它DrawPanel类。public JPanel jp1 = new DrawPanel();我想在jp1里面画图,查阅大量资料:发现有三种方法1。在DrawPanel类中加上
Graphics g = this.getGraphics();
但这个g是空引用,所以无法用g画图。2.重写paint(Graphics g)方法,但是结果是只能在panit()里面用g画图3.重写paintComponent(Graphics g)方法,同上,只能在该方法中用g画图,因为参数g只属于该方法所以我需要一种思路,能让我在DrawPanel中任意的成员函数中画图,而不是只能在paint() 或者 paintComponent()中。

解决方案 »

  1.   

    在DrawPanel类中加上
    Graphics g = this.getGraphics();
    但这个g是空引用,所以无法用g画图。

    g在Janel未显示前为null,但是在显示后不为null,可以获取它来画图
      

  2.   

    ...某类
     
      JPanel jp1 =new DrawPanel;
       该类某方法(){
       jp1.g1.drawline(...);  //但是会提示找不到g1
        }
    }
       class DrawPanel extends JPanel{
        public Graphics g1=this.getGraphics();
    }2楼的意思是这样的?  但是会提示找不到g1.
      

  3.   

    调用getGraphics()前必须保证JPanel已经能够显示
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Test extends JFrame
    {
    MyPanel m = new MyPanel();
    public Test()
    {
    add(m);
    JButton paintButton = new JButton("paint");
    add(paintButton, BorderLayout.NORTH);
    paintButton.addActionListener(new ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    Graphics g = m.getGraphics();
    g.setColor(Color.red);
    g.fillOval(0, 0, 800, 800);
    }
    });
    pack();
    setVisible(true);
    }

    public static void main(String[] args)
    {
    new Test();
    }
    }class MyPanel extends JPanel
    {
    public Graphics g;
    public MyPanel()
    {
    setPreferredSize(new Dimension(800, 600));
    }

    public void paint(Graphics g)
    {
    g.setColor(Color.white);
    g.fillRect(0, 0, getWidth(), getHeight());
    }
    }
      

  4.   

    3楼,如果是要用继承的子类来新建某个控件 比如您代码里的Mypanel,就无法用那种可视化拖放控件来声明定义了吧。
      

  5.   

    额,那个类的定义是没有必要的,忘了删
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class Test extends JFrame
    {
        JPanel m = new JPanel();
        public Test()
        {
            add(m);
            m.setPreferredSize(new Dimension(800, 600));
            JButton paintButton = new JButton("paint");
            add(paintButton, BorderLayout.NORTH);
            paintButton.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    Graphics g = m.getGraphics();
                    g.setColor(Color.red);
                    g.fillOval(0, 0, 800, 800);    
                }
            });
            pack();
            setVisible(true);
        }
        
        public static void main(String[] args)
        {
            new Test();
        }
    }
      

  6.   

     楼上的代码可以画出来,为什么我以前用Graphics g = m.getGraphics();时提示是空引用?
      

  7.   

    在JPanel没有显示前,getGraphics会返回null
     class DrawPanel extends JPanel{
      public Graphics g1=this.getGraphics();//这时JPanel还不可见,所以getGraphics()返回的是null
    }
      

  8.   


            private void jPanel4MousePressed(java.awt.event.MouseEvent evt)   {                                     
                Graphics g2D=jPanel1.getGraphics();
                foreX = evt.getPoint().x;
                foreY = evt.getPoint().y;
                setSize(size,g2D);
                setColor(color,g2D);
                Dot dot = new Dot(foreX, foreY, color, size);
                l1.add(dot);
            }   
    这个响应函数的声明实在initComponents()里,initComponents()是在构造函数中调用。
    而程序一运行,就提示空引用错误,此时JPanel是应该生成了的啊。而五楼的代码,同样是放在把响应函数放在构造函数里面,却没有报错。 
      

  9.   


    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Stroke;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFileChooser;
    import javax.swing.JPanel;public class NewJFrame extends javax.swing.JFrame {    List<Dot> l1;
        Dot tmpDot;
        int size;
        int color;
        int foreX;
        int foreY;
           public NewJFrame() {
            initComponents();
            l1 = new ArrayList<Dot>();
            size = 1;
            color = 0;
            }
        private static PrintWriter stdErr =
                new PrintWriter(System.err, true);    @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {        colorGroup = new javax.swing.ButtonGroup();
            sizeGroup = new javax.swing.ButtonGroup();
            fileChoose = new javax.swing.JFileChooser();
            jPanel1 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jPanel2 = new javax.swing.JPanel();
            jButton2 = new javax.swing.JButton();
            jPanel3 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();
            jRadioButton3 = new javax.swing.JRadioButton();
            jLabel2 = new javax.swing.JLabel();
            jRadioButton4 = new javax.swing.JRadioButton();
            jRadioButton5 = new javax.swing.JRadioButton();
            jRadioButton6 = new javax.swing.JRadioButton();
            jPanel4 = new JPanel();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jPanel1.setLayout(new java.awt.BorderLayout());        jButton1.setText("打开");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
            jPanel1.add(jButton1, java.awt.BorderLayout.CENTER);        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);        jPanel2.setLayout(new java.awt.BorderLayout());        jButton2.setText("保存");
            jPanel2.add(jButton2, java.awt.BorderLayout.CENTER);        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());        jLabel1.setText("Color");        colorGroup.add(jRadioButton1);
            jRadioButton1.setSelected(true);
            jRadioButton1.setText("Black");
            jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton1ActionPerformed(evt);
                }
            });        colorGroup.add(jRadioButton2);
            jRadioButton2.setText("Yellow");
            jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton2ActionPerformed(evt);
                }
            });        colorGroup.add(jRadioButton3);
            jRadioButton3.setText("Blue");
            jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton3ActionPerformed(evt);
                }
            });        jLabel2.setText("PenSize");        sizeGroup.add(jRadioButton4);
            jRadioButton4.setSelected(true);
            jRadioButton4.setText("Thin");
            jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton4ActionPerformed(evt);
                }
            });        sizeGroup.add(jRadioButton5);
            jRadioButton5.setText("Middle");
            jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton5ActionPerformed(evt);
                }
            });        sizeGroup.add(jRadioButton6);
            jRadioButton6.setText("Thick");
            jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton6ActionPerformed(evt);
                }
            });        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jRadioButton6)
                        .addComponent(jRadioButton5)
                        .addComponent(jRadioButton4)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)
                        .addComponent(jRadioButton1)
                        .addComponent(jRadioButton2)
                        .addComponent(jRadioButton3))
                    .addContainerGap(72, Short.MAX_VALUE))
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton3)
                    .addGap(30, 30, 30)
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton5)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jRadioButton6)
                    .addContainerGap(68, Short.MAX_VALUE))
            );
           
      

  10.   


     colorGroup.add(jRadioButton2);
            jRadioButton2.setText("Yellow");
            jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton2ActionPerformed(evt);
                }
            });        colorGroup.add(jRadioButton3);
            jRadioButton3.setText("Blue");
            jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton3ActionPerformed(evt);
                }
            });        jLabel2.setText("PenSize");        sizeGroup.add(jRadioButton4);
            jRadioButton4.setSelected(true);
            jRadioButton4.setText("Thin");
            jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton4ActionPerformed(evt);
                }
            });        sizeGroup.add(jRadioButton5);
            jRadioButton5.setText("Middle");
            jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton5ActionPerformed(evt);
                }
            });        sizeGroup.add(jRadioButton6);
            jRadioButton6.setText("Thick");
            jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton6ActionPerformed(evt);
                }
            });        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jRadioButton6)
                        .addComponent(jRadioButton5)
                        .addComponent(jRadioButton4)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)
                        .addComponent(jRadioButton1)
                        .addComponent(jRadioButton2)
                        .addComponent(jRadioButton3))
                    .addContainerGap(72, Short.MAX_VALUE))
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton3)
                    .addGap(30, 30, 30)
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton5)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jRadioButton6)
                    .addContainerGap(68, Short.MAX_VALUE))
            );        getContentPane().add(jPanel3, java.awt.BorderLayout.LINE_START);        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
            jPanel4.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    jPanel4MousePressed(evt);
                }
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    jPanel4MouseReleased(evt);
                }
            });
            jPanel4.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                public void mouseDragged(java.awt.event.MouseEvent evt) {
                    jPanel4MouseDragged(evt);
                }
            });        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
            jPanel4.setLayout(jPanel4Layout);
            jPanel4Layout.setHorizontalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 309, Short.MAX_VALUE)
            );
            jPanel4Layout.setVerticalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 294, Short.MAX_VALUE)
            );        getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);        pack();
        }// </editor-fold>                            private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            fileChoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (JFileChooser.APPROVE_OPTION == fileChoose.showOpenDialog(null)) {
                try {
                    //读
                    File file = fileChoose.getSelectedFile();
                    BufferedReader input = new BufferedReader(new FileReader(file));
                } catch (IOException ioe) {
                    stdErr.println(ioe.toString());
                }
        }                                        
        }
      

  11.   


     @SuppressWarnings("empty-statement")
            private void jPanel4MouseDragged(java.awt.event.MouseEvent evt) {                                     
                Graphics g2D=jPanel1.getGraphics();
                tmpDot.setX(evt.getPoint().x);
                tmpDot.setY(evt.getPoint().y);
                tmpDot.setColor(color);
                tmpDot.setSize(size);
                l1.add(tmpDot);
                g2D.drawLine(foreX, foreY, evt.getPoint().x, evt.getPoint().y);
                foreX = evt.getPoint().x;
                foreY = evt.getPoint().y;
    }                                            private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                color = 0;//黑
            }                                                     private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                color = 1;//黄
            }                                                     private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                color = 2;//蓝
            }                                                     private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                size = 1;//细
            }                                                     private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                size = 3;//中
            }                                                     private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                size = 5;//粗
            }                                                     private void jPanel4MousePressed(java.awt.event.MouseEvent evt) {                                     
                Graphics g2D=jPanel1.getGraphics();
                foreX = evt.getPoint().x;
                foreY = evt.getPoint().y;
                setSize(size,g2D);
                setColor(color,g2D);
                Dot dot = new Dot(foreX, foreY, color, size);
                l1.add(dot);
            }                                            private void jPanel4MouseReleased(java.awt.event.MouseEvent evt) {                                      
                Graphics g2D=jPanel1.getGraphics();
                Dot dot = new Dot(-1, -1, 0, 0);
                l1.add(dot);
                undo(g2D);
            }                                         /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {
                    new NewJFrame().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.ButtonGroup colorGroup;
        private javax.swing.JFileChooser fileChoose;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        public javax.swing.JPanel jPanel4;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JRadioButton jRadioButton2;
        private javax.swing.JRadioButton jRadioButton3;
        private javax.swing.JRadioButton jRadioButton4;
        private javax.swing.JRadioButton jRadioButton5;
        private javax.swing.JRadioButton jRadioButton6;
        private javax.swing.ButtonGroup sizeGroup;
        // End of variables declaration                   
            public void setSize(int size,Graphics g2D) {
            ((Graphics2D)g2D).setStroke(new BasicStroke(size, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND)); //设置新的画刷
        }    public void undo(Graphics g2D) {
            Stroke stroke = ((Graphics2D)g2D).getStroke(); //得到当前的画刷
            ((Graphics2D)g2D).setStroke(stroke);
        }    public void setColor(int a,Graphics g2D) {
            switch (a) {
                case 0:
                    g2D.setColor(Color.black);
                    break;
                case 1:
                    g2D.setColor(Color.yellow);
                    break;
                case 2:
                    g2D.setColor(Color.blue);
                    break;
                default:
                    break;
            }
        }
    }
      

  12.   

    好长的代码,看的眼花。。
    我试着改了一下,你运行下面的代码,然后再文字!!Test!!附近点击并拖动鼠标,这样就会在jpanel1上画出细线。至于你的代码为什么画不出来,我不太清楚。
      

  13.   

    import java.awt.BasicStroke;
    import java.awt.Dimension;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Point;
    import java.awt.Graphics2D;
    import java.awt.Stroke;
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.JFileChooser;
    import javax.swing.JPanel;
    import javax.swing.JLabel;public class Test extends javax.swing.JFrame {    List<Point> l1;
        Point tmpDot;
        int size;
        int color;
        int foreX;
        int foreY;
           public Test() {
            initComponents();
            l1 = new ArrayList<Point>();
            size = 1;
            color = 0;
            pack();
            }
        private static PrintWriter stdErr =
                new PrintWriter(System.err, true);    @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
        private void initComponents() {        colorGroup = new javax.swing.ButtonGroup();
            sizeGroup = new javax.swing.ButtonGroup();
            fileChoose = new javax.swing.JFileChooser();
            jPanel1 = new javax.swing.JPanel();
            jButton1 = new javax.swing.JButton();
            jPanel2 = new javax.swing.JPanel();
            jButton2 = new javax.swing.JButton();
            jPanel3 = new javax.swing.JPanel();
            jLabel1 = new javax.swing.JLabel();
            jRadioButton1 = new javax.swing.JRadioButton();
            jRadioButton2 = new javax.swing.JRadioButton();
            jRadioButton3 = new javax.swing.JRadioButton();
            jLabel2 = new javax.swing.JLabel();
            jRadioButton4 = new javax.swing.JRadioButton();
            jRadioButton5 = new javax.swing.JRadioButton();
            jRadioButton6 = new javax.swing.JRadioButton();
            jPanel4 = new JPanel();        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);        jPanel1.setLayout(new java.awt.FlowLayout());        jButton1.setText("打开");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
            jPanel1.add(jButton1, java.awt.BorderLayout.CENTER);
            getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_START);        jPanel2.setLayout(new java.awt.BorderLayout());        jButton2.setText("保存");
            jPanel2.add(jButton2, java.awt.BorderLayout.CENTER);        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());        jLabel1.setText("Color");        colorGroup.add(jRadioButton1);
            jRadioButton1.setSelected(true);
            jRadioButton1.setText("Black");
            jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton1ActionPerformed(evt);
                }
            });        colorGroup.add(jRadioButton2);
            jRadioButton2.setText("Yellow");
            jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton2ActionPerformed(evt);
                }
            });        colorGroup.add(jRadioButton3);
            jRadioButton3.setText("Blue");
            jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton3ActionPerformed(evt);
                }
            });        jLabel2.setText("PenSize");        sizeGroup.add(jRadioButton4);
            jRadioButton4.setSelected(true);
            jRadioButton4.setText("Thin");
            jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton4ActionPerformed(evt);
                }
            });        sizeGroup.add(jRadioButton5);
            jRadioButton5.setText("Middle");
            jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton5ActionPerformed(evt);
                }
            });        sizeGroup.add(jRadioButton6);
            jRadioButton6.setText("Thick");
            jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton6ActionPerformed(evt);
                }
            });        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jRadioButton6)
                        .addComponent(jRadioButton5)
                        .addComponent(jRadioButton4)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)
                        .addComponent(jRadioButton1)
                        .addComponent(jRadioButton2)
                        .addComponent(jRadioButton3))
                    .addContainerGap(72, Short.MAX_VALUE))
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton3)
                    .addGap(30, 30, 30)
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton5)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jRadioButton6)
                    .addContainerGap(68, Short.MAX_VALUE))
            );
    colorGroup.add(jRadioButton2);
            jRadioButton2.setText("Yellow");
            jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton2ActionPerformed(evt);
                }
            });        colorGroup.add(jRadioButton3);
            jRadioButton3.setText("Blue");
            jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton3ActionPerformed(evt);
                }
            });        jLabel2.setText("PenSize");        sizeGroup.add(jRadioButton4);
            jRadioButton4.setSelected(true);
            jRadioButton4.setText("Thin");
            jRadioButton4.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton4ActionPerformed(evt);
                }
            });        sizeGroup.add(jRadioButton5);
            jRadioButton5.setText("Middle");
            jRadioButton5.addActionListener(new java.awt.event.ActionListener() {
      

  14.   

                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton5ActionPerformed(evt);
                }
            });        sizeGroup.add(jRadioButton6);
            jRadioButton6.setText("Thick");
            jRadioButton6.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jRadioButton6ActionPerformed(evt);
                }
            });       // javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jRadioButton6)
                        .addComponent(jRadioButton5)
                        .addComponent(jRadioButton4)
                        .addComponent(jLabel2)
                        .addComponent(jLabel1)
                        .addComponent(jRadioButton1)
                        .addComponent(jRadioButton2)
                        .addComponent(jRadioButton3))
                    .addContainerGap(72, Short.MAX_VALUE))
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton3)
                    .addGap(30, 30, 30)
                    .addComponent(jLabel2)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton4)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jRadioButton5)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jRadioButton6)
                    .addContainerGap(68, Short.MAX_VALUE))
            );        getContentPane().add(jPanel3, java.awt.BorderLayout.LINE_START);
    jPanel4.setPreferredSize(new Dimension(200, 200));
    jPanel4.add(new JLabel("!!Drag!!"));
            jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
            jPanel4.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mousePressed(java.awt.event.MouseEvent evt) {
                    jPanel4MousePressed(evt);
                    System.out.println("afd");
                }
                public void mouseReleased(java.awt.event.MouseEvent evt) {
                    jPanel4MouseReleased(evt);
                }
            });
            jPanel4.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                public void mouseDragged(java.awt.event.MouseEvent evt) {
                    jPanel4MouseDragged(evt);
                }
            });        java.awt.FlowLayout jPanel4Layout = new java.awt.FlowLayout();
            jPanel4.setLayout(jPanel4Layout);
            /*jPanel4Layout.setHorizontalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 309, Short.MAX_VALUE)
            );
            jPanel4Layout.setVerticalGroup(
                jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 294, Short.MAX_VALUE)
            );
    */
            getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);        pack();
        }// </editor-fold>                            private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            fileChoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (JFileChooser.APPROVE_OPTION == fileChoose.showOpenDialog(null)) {
                try {
                    //读
                    File file = fileChoose.getSelectedFile();
                    BufferedReader input = new BufferedReader(new FileReader(file));
                } catch (IOException ioe) {
                    stdErr.println(ioe.toString());
                }
        }                                        
        }
     @SuppressWarnings("empty-statement")
            private void jPanel4MouseDragged(java.awt.event.MouseEvent evt) {                                     
                Graphics g2D=jPanel1.getGraphics();
     //           tmpDot.setX(evt.getPoint().x);
     //           tmpDot.setY(evt.getPoint().y);
     //           tmpDot.setColor(color);
     //           tmpDot.setSize(size);
                l1.add(tmpDot);
                g2D.drawLine(foreX, foreY, evt.getPoint().x, evt.getPoint().y);
                foreX = evt.getPoint().x;
                foreY = evt.getPoint().y;
    }                                            private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                color = 0;//黑
            }                                                     private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                color = 1;//黄
            }                                                     private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                color = 2;//蓝
            }                                                     private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                size = 1;//细
            }                                                     private void jRadioButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                size = 3;//中
            }                                                     private void jRadioButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                              
                size = 5;//粗
            }                                                     private void jPanel4MousePressed(java.awt.event.MouseEvent evt) {                                     
                Graphics g2D=jPanel1.getGraphics();
                foreX = evt.getPoint().x;
                foreY = evt.getPoint().y;
                setSize(size,g2D);
                setColor(color,g2D);
    //            Point Point = new Point(foreX, foreY, color, size);
     //           l1.add(Point);
            }                                            private void jPanel4MouseReleased(java.awt.event.MouseEvent evt) {                                      
                Graphics g2D=jPanel1.getGraphics();
    //            Point Point = new Point(-1, -1, 0, 0);
    //            l1.add(Point);
                undo(g2D);
            }                                         /**
         * @param args the command line arguments
         */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {            public void run() {
                    new Test().setVisible(true);
                }
            });
        }
        // Variables declaration - do not modify                     
        private javax.swing.ButtonGroup colorGroup;
        private javax.swing.JFileChooser fileChoose;
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JLabel jLabel2;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JPanel jPanel2;
        private javax.swing.JPanel jPanel3;
        public javax.swing.JPanel jPanel4;
        private javax.swing.JRadioButton jRadioButton1;
        private javax.swing.JRadioButton jRadioButton2;
        private javax.swing.JRadioButton jRadioButton3;
        private javax.swing.JRadioButton jRadioButton4;
        private javax.swing.JRadioButton jRadioButton5;
        private javax.swing.JRadioButton jRadioButton6;
        private javax.swing.ButtonGroup sizeGroup;
        // End of variables declaration                   
            public void setSize(int size,Graphics g2D) {
            ((Graphics2D)g2D).setStroke(new BasicStroke(size, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND)); //设置新的画刷
        }    public void undo(Graphics g2D) {
            Stroke stroke = ((Graphics2D)g2D).getStroke(); //得到当前的画刷
            ((Graphics2D)g2D).setStroke(stroke);
        }    public void setColor(int a,Graphics g2D) {
            switch (a) {
                case 0:
                    g2D.setColor(Color.black);
                    break;
                case 1:
                    g2D.setColor(Color.yellow);
                    break;
                case 2:
                    g2D.setColor(Color.blue);
                    break;
                default:
                    break;
            }
        }
    }
      

  15.   

    好长的代码贴。重点是:graphics这个东西在japnel中,要实例化了jpanel后,它才不为空。
      

  16.   

    17我也知道这个问题,所以graphics放在构造函数中使用,但是还是不行