Java 在此处更改随机绘制线,以进行mouseListener绘制

Java 在此处更改随机绘制线,以进行mouseListener绘制,java,swing,mouseevent,mouselistener,Java,Swing,Mouseevent,Mouselistener,我正在做一个“画画”项目。到目前为止,我有一个GUI,有一个按钮“Ligne”和一个可绘制的面板。在我的类Paint_Dessin中,有一个方法调用TracerLigne()。这种方法画出了一条随机模式的直线。我想做的是放一个鼠标听筒,让x1,y1=点击1,x2,y2=点击2。这是我的密码。谢谢(对法国的评论感到抱歉) 为了实现这一点,您需要在您的绘画JPanel中添加一个MouseListener(并且可以在扩展MouseAdapter的单个类中进行编码)。然后重写mousePressed

我正在做一个“画画”项目。到目前为止,我有一个GUI,有一个按钮“Ligne”和一个可绘制的面板。在我的类Paint_Dessin中,有一个方法调用TracerLigne()。这种方法画出了一条随机模式的直线。我想做的是放一个鼠标听筒,让x1,y1=点击1,x2,y2=点击2。这是我的密码。谢谢(对法国的评论感到抱歉)




为了实现这一点,您需要在您的绘画JPanel中添加一个MouseListener(并且可以在扩展MouseAdapter的单个类中进行编码)。然后重写mousePressed和MouseRelease(如果需要这些方法),并在这些方法中从传递给它的MouseEvent对象获取鼠标位置点。然后,您将使用点的值在BuffereImage中绘制一条线。我的猜测是,你会想得到鼠标按下的起点和鼠标松开的终点,然后在鼠标松开后在缓冲区中画线。如果需要在mouseDragged上动态绘制线,则需要一个MouseMotionListener(同样,上面的MouseAdapter类也适用于此)

查看教程以获得优秀的示例代码和说明:

执行此操作的半伪代码如下所示:

// 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
   }
}

我澄清了你的问题。下次请发布格式正确的源代码。粘贴代码时,在窗口中选择它并点击代码按钮(二进制图标)。它会将其缩进四个空格,导致页面将其呈现为源代码。
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;
    }
}  
// 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
   }
}