有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将鼠标侦听器添加到用图形绘制的线条中。抽绳

正如标题所说,我希望能够右键单击我在Jpanel上绘制的线条。由于这些线不是组件,我不能简单地向它们添加MouseListener。目前,我正在使用以下代码在我的Jpanel上绘制线条:

@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;
    for (UserDrawnLine line : userDrawnLines) {
        g.setColor(new Color(line.colorRValue,line.colorGValue, line.colorBValue));
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setStroke(new BasicStroke(line.thickness));
        g.drawLine(line.startPointX, line.startPointY, line.endPointX, line.endPointY);
    }
}

这是我的UserDrawnLine类:

public class UserDrawnLine {
    public int startPointX;
    public int startPointY;
    public int endPointX;
    public int endPointY;
    public int colorRValue;
    public int colorGValue;
    public int colorBValue;
    public float thickness;



    public UserDrawnLine(int startPointX, int startPointY, int endPointX, int endPointY, int colorRValue,int colorGValue,int colorBValue, float thickness) {
        this.startPointX = startPointX;
        this.startPointY = startPointY;
        this.endPointX = endPointX;
        this.endPointY = endPointY;
        this.colorRValue=colorRValue;
        this.colorBValue=colorBValue;
        this.colorGValue=colorGValue;
        this.thickness=thickness;
    }
}

我一直在考虑存储线路经过的点,然后在用户点击其中一个点上的Jpanel时做出相应的反应。然而,这似乎不是最好的解决方案。有更好的吗


共 (0) 个答案