java - 更改此处的随机绘制线以进行 mouseListener 绘制

标签 java swing mouseevent mouselistener

我正在开发一个“绘画”程序。到目前为止,我有一个带有 1 个按钮“Ligne”和一个可绘制面板的 GUI。在我的Paint_Dessin类中,有一个方法调用TracerLigne()。此方法按照随机模式绘制线条。我想要做的是放置一个鼠标监听器,以便 x1,y1 = click1 和 x2,y2 = click 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;
    }
}  

最佳答案

为此,您需要向绘画 JPanel 添加一个 MouseListener(这可以在扩展 MouseAdapter 的单个类中进行编码)。然后,您将重写 mousePressed 和 mouseReleased (如果需要这些方法),并在这些方法中从传递给其中的 MouseEvent 对象获取鼠标位置 Point。然后,您将使用 Points 的值在 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
   }
}

关于java - 更改此处的随机绘制线以进行 mouseListener 绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4952997/

相关文章:

c++ - C++ Win32 中的哪个控件等于 C# WPF 中的 "Run"控件?

image-processing - 使用 matplotlib 检测图像中的鼠标事件

angular - 我可以用其他鼠标事件运行@HoSTListner 吗?

Java 给定整数的字符串排列

java - CompletableFuture——聚合 future 以快速失败

java - 需要删除csv中的空列

java - ActionListener问题

java - 打开 Eclipse juno 时出错

java - 我如何使用 LayoutManager 来获得这样的布局?

performance - 计时 JavaFX Canvas 应用程序