2011-02-09 77 views
1

我正在为我的课程之一制作绘画程序,但我被卡住了。这是我的代码的一部分(分为3个Java类)。当我点击按钮“Ligne”时,我希望能够在白色矩形中绘制一条线。对于法国评论感到抱歉。如何在我的Java绘画程序中绘制直线,矩形和圆圈?


//cree une fenetre  
public class QUESTION 
{ 
    public static void main(String[] args) 
    { 
     Paint_GUI test2 = new Paint_GUI(); 
    } 
} 

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

//class contenant le code pour dessiner 
public class Paint_Dessin extends JPanel 
{ 
    public void paintComponent(Graphics g) 
    { 
     super.paintComponent(g); 
    setBackground(Color.white); 
    g.setColor(Color.black); 
    } 

    public void TracerLigne() 
    { 
    System.out.println("LIGNE"); 
    } 
} 

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; 

    //object 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(); 
    } 
} 

回答

0

你应该创建另一个类像GraphComponent例如,扩展JComponent。你也应该看看执行MouseInputListener。我不会给你解决方案,但这是一个好的开始;)。

无论如何,我看你是法国人,所以看看site-du-zero,它有很多很好的教程(法语)。

N.B:你不应该命名你喜欢的类问题,提出问题,而不是

编辑: 这里的另一个提示:

public class Paint_Dessin extends JComponent implements MouseInputListener{ 
    private List<Point> startPoints = new ArrayList<Point>(); 
    @Override 
    public void mouseClicked(MouseEvent e) { 
     int x = e.getX(); 
     int y = e.getY(); 
     Point p = new Point(x,y); 
     startPoints.add(p); 
     repaint(); 
    } 
    protected void paintComponent(Graphics g) { 
     g.setColor(getForeground()); 
     Graphics2D g2 = (Graphics2D) g; 
     for (Point p : startPoints) 
      p.draw(g2); 
    } 
    public class Point{ 
     private int x,y; 
     public Point(int x, int y){ 
      this.x = x; 
      this.y = y; 
     } 
     void draw(Graphics2D g2) { 
      //do the drawing with the right shape you want 
     } 
    } 

Shape。您应该实现MouseInputListener中的其他方法,如mouseDragged,...

+0

Ty为快速答案,我知道的类名:)它的一部分我的代码我编辑真的很快的论坛。 – Panda 2011-02-09 22:58:05

+0

但真正的问题是,不知道如何做MouseInputListener,我应该把它放在 – Panda 2011-02-09 22:58:48

1

您当前的代码有问题,因为您正在LigneListener类的actionPerformed方法中创建一个新的Paint_Dessin,并且在创建此对象时从与JFrame中显示的Paint_Dessin对象相同的类,它是一个完全不同的对象,调用它的方法对显示的Paint_Dessin绝对没有影响。您需要做的是在Paint_GUI类中声明一个私有的Paint_Dessin变量,一个类字段,在其声明或Paint_GUI构造函数中初始化此变量,然后在GUI中显示此对象,并在此GUI上调用方法在监听器类中。

public class Paint_GUI extends JFrame 
{ 
    private JPanel panelBtn; 
    private JButton BtnTracerLigne; 
    private Paint_Dessin espaceDessin = new Paint_Dessin(); 

    public Paint_GUI() 
    { 
     final int WINDOW_WIDTH = 650; 
     //... code deleted for sake of brevity 

     // Paint_Dessin espaceDessin = new Paint_Dessin(); 
     add(espaceDessin, BorderLayout.CENTER); 

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

private void buildPanelBtn() 
{ 
    // .... 
} 

private class LigneListener implements ActionListener 
{ 
    public void actionPerformed(ActionEvent e) 
    { 
    // Paint_Dessin tracerLigne = new Paint_Dessin(); 
    // tracerLigne.TracerLigne(); 
    espaceDessin.TracerLigne(); // call the method on the same object! 
    } 
}