2012-07-05 24 views
0

我可以讓程序出現並用一種顏色繪製,但如果點擊其中一個按鈕,它將不會改變顏色。我不斷收到空指針異常。我認爲這可能是因爲我沒有初始化圖形,圖像或兩者。如果是這個問題,我不太清楚如何去解決這個問題。我相當新的編程和任何幫助將不勝感激。更改我的Java繪畫程序中的顏色

代碼:

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(); 
     } 
    } 
+0

哪條線是被拋出的NPE? – JRSofty 2012-07-05 14:30:58

+0

歡迎來到Stack Overflow!請將您的代碼示例限制在相關部分。理想的示例是[簡短,獨立和可編譯](http://sscce.org/)。通讀整個源代碼非常耗時,並且會爲您提供更少的答案。 – 2012-07-05 14:31:49

+0

'私人Graphics2D克;'問題開始那裏,我懷疑..圖形對象通常是短暫的 - 那裏,走了。 – 2012-07-05 14:32:46

回答

1

您正在從按鈕監聽器調用red()方法,並且一旦您使用了x1,x2,y1和你調用drawLine時的y2值。你在哪裏初始化這些變量?

您正在依靠那些用鼠標點擊時應寫入的變量,但這可能不會發生。嘗試在構造函數中初始化它們。

1

存儲您的圖形對象是什麼天色你陷入困境。簡單的答案是所有繪畫都需要通過JComponent.paintComponent(...)來完成,因此您的red()green()blue()應該由paintComponent通過paintComponent的Graphics對象調用。

從根本上講, java繪畫的作品是,通過回調paint()/ paintcomponent(),所有東西都被繪製在同一個循環中。該機制以這種方式工作,以便組件可以在不同的時間,不同的鼠標/點擊狀態等看起來不同。