0

プログラムを起動して 1 つの色でペイントすることはできますが、ボタンの 1 つをクリックしても色が変わりません。ヌル ポインター例外が発生し続けます。グラフィック、画像、またはその両方を初期化していないことが原因である可能性があります。それがその問題である場合、それを修正する方法がよくわかりません。私はプログラミングにかなり慣れていないので、どんな助けでも大歓迎です。

コード:

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


    public class Paint extends JFrame{
        //Class Variables
        private JFrame frame;
        PaintPanel paint_panel;
        private JPanel btn_panel;
        private JButton red_btn, green_btn, blue_btn, clear_btn, erase_btn ;



        //class to paint the panel in the frame
        public class PaintPanel extends JComponent{     
            //Class Variables
            private Graphics2D g;
            private int x1, y1, x2, y2;
            private Cursor paint_cursor, select_cursor;
            private Image image;

            //Constructor with Mouse listeners in it  
            public PaintPanel(){

                 addMouseListener(new MouseAdapter(){
                     public void mousePressed(MouseEvent e){
                             x1 = e.getX();
                             y1 = e.getY();
                             }
                    });
                 addMouseMotionListener(new MouseMotionAdapter(){

                     public void mouseDragged(MouseEvent e){
                             x2 = e.getX();
                             y2 = e.getY();

                             if(g != null)
                                 g.drawLine(x1, y1, x2, y2);
                             repaint();
                             x1 = x2;
                             y1 = y2;
                     }
             });
             }
            //PaintPanel Method that sets the cursor and does the drawing
            public void paintComponent(Graphics gr){
                super.paintComponent(gr);
                gr.setColor(Color.black);
                gr.fillRect(0, 0, this.getWidth(), this.getHeight());
                paint_cursor=new Cursor(Cursor.CROSSHAIR_CURSOR);
                select_cursor=new Cursor(Cursor.HAND_CURSOR);
                paint_panel.setCursor(paint_cursor);
                btn_panel.setCursor(select_cursor);
                if(image == null){
                    image = createImage(this.getWidth(), this.getHeight());
                    g = (Graphics2D)image.getGraphics();
                    clear1();
                 }
                 gr.drawImage(image, 0, 0, null);
         }
             public void clear1(){
                g.setPaint(Color.black);
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                g.setPaint(Color.blue);
                repaint();  
             }
             public void red(){ 
                 g.drawLine(x1, y1, x2, y2);
                 g.setPaint(Color.red);

                 x1 = x2;
                 y1 = y2;
                 repaint();
             }
             public void blue(){    
                 g.drawLine(x1, y1, x2, y2);
                 g.setPaint(Color.blue);

                 x1 = x2;
                 y1 = y2;
                 repaint();
             }
             public void green(){   
                g.drawLine(x1, y1, x2, y2);
                g.setPaint(Color.green);

                 x1 = x2;
                 y1 = y2;
                 repaint();
             }
            }

        //Constructor
        public Paint(){

            frame = new JFrame("Paint Program");
            frame.setSize(500,500);
            frame.setLocation(500,100);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



            red_btn = new JButton("RED");
            green_btn = new JButton("GREEN");
            blue_btn = new JButton("BLUE");
            clear_btn = new JButton("CLEAR");
            erase_btn = new JButton("ERASE");

            btn_panel = new JPanel();
            paint_panel = new PaintPanel();

            btn_panel.setLayout(new GridLayout(5,1));

            btn_panel.add(red_btn);
            btn_panel.add(green_btn);
            btn_panel.add(blue_btn);
            btn_panel.add(clear_btn);
            btn_panel.add(erase_btn);
            frame.add(BorderLayout.CENTER, paint_panel);
            frame.add(BorderLayout.EAST, btn_panel);

            final PaintPanel pp1 = new PaintPanel();

            red_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.red();
                }   
                });
            blue_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.blue();
                }   
                });
            green_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.green();
                }   
                });
            clear_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.clear1();
                }   
                });
            erase_btn.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    pp1.green();
                }   
                });
        }
        //Makes the frame visible and working
        public void launch(){
            frame.setVisible(true);
        }

        //Main Method
        public static void main(String[] args){
            Paint p1 = new Paint();
            p1.launch();
        }
    }
4

2 に答える 2

1

ボタン リスナーから red() メソッドを呼び出しており、そこで drawLine を呼び出すときに x1、x2、y1、および y2 の値を使用しています。これらの変数をどこで初期化していますか?

マウスでクリックしたときにそれらの変数が書き込まれることに依存していますが、そうではない場合があります。コンストラクターでそれらを初期化してみてください。

于 2012-07-05T16:15:16.607 に答える
1

グラフィックス オブジェクトを格納することが問題の原因です。簡単な答えは、すべての描画は JComponent.paintComponent(...) を介して行う必要があるため、 red() green() blue() は、paintComponent の Graphics オブジェクトを使用して paintComponent によって呼び出される必要があるということです。

基本的に、Java ペインティングが機能する方法は、paint() / paintcomponent() へのコールバックを通じて、すべてが同じサイクルで描画されることです。メカニズムはこのように機能するため、コンポーネントは異なる時間、異なるマウス/クリック状態などで異なるように見えます。

于 2012-07-05T14:35:35.900 に答える