2013-04-23 78 views
0

所以,我試圖結合使用paintComponent()以及使用MouseListener和MouseActionListener,但是當我運行它時遇到了很多錯誤,它不工作,我想要它。在這段代碼中,我希望程序在按下,拖動和釋放按鈕時,獲取印刷機的座標,然後測量版本的座標,然後測量形狀的大小,然後繪製指定的形狀由JComboBox。我也有JFrame底部按鈕的顏色選擇器。我想知道我可以如何運行paintComponent()方法,而不需要它自動運行,所以我可以在繪製之前給它指定規格,並根據需要繪製它。另外,我想知道是否有另一種方法可以做到這一點,而且我對於如何接近它完全錯誤。 我希望解釋不是太混亂:)。任何幫助都很好,謝謝!如何將paintComponent(Graphics g)與MouseListener和MouseMotionListener結合起來

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

@SuppressWarnings({"unchecked", "rawtypes"}) 
public class GraphicGUI extends JPanel implements ActionListener { 

    HandlerClass handler = new HandlerClass(); 

    public int x1, x2, y1, y2, width, height; 
    public String event; 
    public JButton colorChooserButton; 
    public Color color = (Color.WHITE); 
    public JComboBox shapeBox; 
    public JLabel eventLabel; 


    public GraphicGUI(){ 
     shapeBox = new JComboBox(); 
     eventLabel = new JLabel(); 
     colorChooserButton = new JButton("Choose a color"); 
     colorChooserButton.addActionListener(this); 

     shapeBox.addItem("Oval"); 
     shapeBox.addItem("Rectangle"); 
     shapeBox.addItem("Line"); 

     super.addMouseListener(handler); 
     super.addMouseMotionListener(handler); 
    } 

    public void actionPerformed(ActionEvent arg0){ 
     if(arg0.getSource().equals(colorChooserButton)){ 
      color = JColorChooser.showDialog(null, "Pick Your Color", color); 
      if(color==null){ 
       color = (Color.BLACK); 
      } 
     } 
    } 

    public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     this.setBackground(Color.white); 
     if(shapeBox.getSelectedItem() == "Oval") 
      if(event.equals("released")){ 
       width = x1-x2; 
       height = y1-y2; 
       g.setColor(color); 
       g.fillOval(x1, y1, width, height); 
      } 
    } 

    private class HandlerClass implements MouseListener, MouseMotionListener{ 

      //Mouse Events 
      public void mouseClicked(MouseEvent arg0){ 
       event = "click"; 
      } 
      public void mousePressed(MouseEvent arg0){ 
       event = "pressed"; 
       x1 = arg0.getX(); 
       y1 = arg0.getY(); 
       eventLabel.setText(String.format("Mouse pressed at %d, %d", x1, y1)); 
      } 
      public void mouseReleased(MouseEvent arg0){ 
       event = "released"; 
       x2 = arg0.getX(); 
       y2 = arg0.getY(); 
       eventLabel.setText(String.format("Mouse released at %d, %d", x2, y2)); 
      } 
      public void mouseEntered(MouseEvent arg0){ 
      } 
      public void mouseExited(MouseEvent arg0){ 
      } 

      //Mouse Motion Events 
      public void mouseDragged(MouseEvent arg0){ 
      } 
      public void mouseMoved(MouseEvent arg0){ 
      } 

    } 
} 
+0

你應該更確切地說你的代碼現在正在做什麼,即它崩潰,它是否畫了一些東西等 – fstamour 2013-04-23 22:54:51

回答

0

你應該畫出形狀來開始。

public void paintComponent(Graphics g){ 
    //Draw the oval 
    g.setColor(color); 
    g.fillOval(x1, y1, width, height); 
} 

public void mouseReleased(MouseEvent arg0){ 
    event = "released"; 
    x2 = arg0.getX(); 
    y2 = arg0.getY();  
    if(x2 > x1) { 
     width = x2-x1; 
    } else { 
     width = x1-x2; 
    }  
    if(y2 > y1) { 
     height = y2-y1; 
    } else { 
     height = y1-y2; 
    } 
    eventLabel.setText(String.format("Mouse released at %d, %d", x2, y2)); 
} 

然後(如果你想添加更多的形狀),您需要一種方法來添加形狀(廢話),因此你可以有形狀的列表,並通過該列表進行迭代。

你真的應該(必須)初始化所有的變量。

詢問您是否需要更多詳細信息。 :)

+0

是的,問題是,我如何調用paintComponent方法,因爲當我嘗試和做g.SetColor()或g.fillOval(),g給我一個錯誤,因爲它是一個空變量(不存在),所以我不能只是這樣調用它:/ – Mattis 2013-04-23 20:54:53

+0

當然,對不起,我編輯了我的答案。 – fstamour 2013-04-23 22:49:36

+0

我做了另一個編輯。我認爲你遇到的問題是因爲你有時有負面因素。 – fstamour 2013-04-23 22:54:08

相關問題