2011-02-10 99 views
0

即時通訊工作在「繪圖」程序。到目前爲止,我有一個帶有1個按鈕「Ligne」和一個可繪製面板的GUI。在我的類Paint_Dessin中,一個方法調用TracerLigne()。此方法在隨機模式中畫線。我想要做的是把一個mouselistener所以x1,y1 = click1和x2,y2 =單擊2.這是我的代碼。謝謝(遺憾的法國評論)更改鼠標偵聽器繪製的隨機繪製線

//cree une fenetre 
public class QUESTION { 

    public static void main(String[] args) { 
     Paint_GUI test2 = new Paint_GUI(); 
    } 
} 

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

public class Paint_GUI extends JFrame { 
    //Panels contenant tout les bouton de mon interface 

    private JPanel panelBtn; 
    //Bar d'outil Btn 
    private JButton BtnTracerLigne; 
    //créer l'objet Paint_Dessin 
    private Paint_Dessin espaceDessin = new Paint_Dessin(); 

    public Paint_GUI() { 
     final int WINDOW_WIDTH = 650; 
     final int WINDOW_HEIGHT = 450; 

     setSize(WINDOW_WIDTH, WINDOW_HEIGHT); 
     setTitle("Paint v.2.0"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setLayout(new BorderLayout()); 

     // Appeler la methode qui construit la barre de BTN. 
     buildPanelBtn(); 
     add(panelBtn, BorderLayout.NORTH); 
     add(espaceDessin, BorderLayout.CENTER); 

     // Afficher la fenetre. 
     setVisible(true); 
    } 

    private void buildPanelBtn() { 
     BtnTracerLigne = new JButton("Ligne"); 
     BtnTracerLigne.addActionListener(new LigneListener()); 

     // Creer le panel. 
     panelBtn = new JPanel(); 
     // Ajouter les composantes au label 
     panelBtn.add(BtnTracerLigne); 
    } 

    private class LigneListener implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      espaceDessin.TracerLigne(); 
     } 
    } 
} 

import java.awt.*; 
import java.awt.geom.*; 
import javax.swing.*; 
import java.util.*; 
import java.awt.image.*; 

class Paint_Dessin extends JPanel { 

    private static final long serialVersionUID = -2110723486099015303L; 
    private static final Random RAND = new Random(); 
    private BufferedImage buffer = null; 

    @Override 
    public void paintComponent(final Graphics g) { 
     final Graphics2D g2 = (Graphics2D) g; 
     g2.clearRect(0, 0, getWidth(), getHeight()); // cleanup du composant 
     g2.drawImage(getBuffer(), null, 0, 0); 
    } 

    public void TracerLigne() { 
     final Graphics2D g2 = getBuffer().createGraphics(); 
     g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
       RenderingHints.VALUE_ANTIALIAS_ON); 
     g2.setColor(Color.BLACK); 
     // dessin la ligne au pif dans l'espace visible 
     final int x1 = RAND.nextInt(500); // position en X1 
     final int y1 = RAND.nextInt(500); // position en Y1 
     final int x2 = RAND.nextInt(500); // position en X2 
     final int y2 = RAND.nextInt(500); // position en Y2 
     g2.drawLine(x1, y1, x2, y2); 
     Line2D.Double line = new Line2D.Double(x1, y1, x2, y2); 
     g2.fill(line); 
     repaint(); 
    } 

    private BufferedImage getBuffer() { 
     if (buffer == null) { 
      buffer = new BufferedImage(getWidth(), getHeight(), 
        BufferedImage.TYPE_INT_ARGB); 
     } 
     return buffer; 
    } 
} 
+0

我清理你的問題。下次請正確格式化源代碼。粘貼代碼時,在窗口中選擇它並點擊代碼按鈕(二進制圖標)。它會縮進四個空格,導致頁面呈現爲源代碼。 – I82Much 2011-02-10 03:22:26

回答

1

對於這個工作,你需要一個的MouseListener添加到您畫的JPanel(這樣就可以編碼在擴展MouseAdapter的單個類中)。然後,您將重寫mousePressed和mouseReleased(如果這些是所需的方法),並在這些方法中獲取傳遞給它的MouseEvent對象的鼠標位置Point。然後,您將使用這些點的值在BufferedImage中繪製一條線。我的猜測是,你會希望得到mousePressed上的起點和mouseReleased上的終點,然後在mouseReleased之後在緩衝區中畫線。如果您需要在mouseDragged上動態繪製一條線,則需要一個MouseMotionListener(同樣,上面的MouseAdapter類也適用於此)。

檢查出優秀的示例代碼和解釋教程:How To Write a MouseListener

半僞這樣做的是一樣的東西:

// assuming a private inner class 
private class MyMouseAdapter extends MouseAdapter { 
    @Override 
    public void mousePressed(MouseEvent e) { 
     // get your starting point from e, the MouseEvent and store it in variable 
    } 

    @Override 
    public void mouseReleased(MouseEvent e) { 
     // get your end point from e, the MouseEvent 
     // get the Graphics object from the BufferedImage 
     // set the color 
     // set rendering hints for antialiasing if desired 
     // draw your line using the starting and end points 
     // **** dispose your graphics object **** don't forget! 
     // repaint your JPanel 
    } 
}