简体   繁体   English

如何将Mouselistener添加到图形Java中的行

[英]how to add Mouselistener to line in graphics java

public Line2D line2d;
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    g2 = (Graphics2D) g;
    g2.setColor(Color.BLACK);
    g2.setStroke(new BasicStroke(2.0F));
    line2d = new Line2D.Double(40, 0, 400, 400);
    g2.draw(line2d);
}

@Override
public void mouseClicked(MouseEvent e) {
    if(line2d.contains(e.getX(), e.getY())) {
        System.out.println("Line clicked");
    }
}

This variant is not working. 此变体无法正常工作。 Is there any other way of adding MouseListener to the line? 还有其他方法可以将MouseListener添加到该行吗?


I found solution. 我找到了解决方案。 I think it is better add invisible Polygon there. 我认为最好在其中添加不可见的多边形。 and catching its Mouseevents. 并捕获其Mouseevents。 because line doesn't have clickable area unlike polygons.See the code below: 因为线没有与多边形不同的可点击区域。请参见以下代码:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MyPanel extends JPanel implements MouseListener {

    public Line2D line2d;
    public Polygon pol;
    public int x1 = 40;
    public int y1 = 0;
    public int x2 = 400;
    public int y2 = 400;
    public int margin = 3;

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setPreferredSize(new Dimension(500, 500));
        frame.getContentPane().add(new MyPanel());
        frame.pack();
        frame.setVisible(true);
    }

    public MyPanel(){

        setBackground(Color.WHITE);
        addMouseListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);


        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(2.0F));
        line2d = new Line2D.Double(x1, y1, x2, y2);
        g2.draw(line2d);
        int xPoints[] = { x1 - margin, x1 + margin, x2 + margin, x2 - margin };
        int yPoints[] = { y1 + margin, y1 - margin, y2 - margin, y2 + margin };
        pol = new Polygon(xPoints, yPoints, yPoints.length);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if (pol.contains(e.getX(), e.getY())) {
            System.out.println("Line clicked");
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        // TODO Auto-generated method stub

    }

}

MouseListener can be add to a Component, for example: JPanel, JTextfield etc., or your self-defined class with extending Component. 可以将MouseListener添加到Component中,例如:JPanel,JTextfield等,或带有扩展Component的自定义类。 Please see the below example. 请参见以下示例。

public class T extends JComponent {

    public T(){
        addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO: do your business.
            }
        });
    }

    public Line2D line2d;
    Graphics2D g2;

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g2 = (Graphics2D) g;
        g2.setColor(Color.BLACK);
        g2.setStroke(new BasicStroke(2.0F));
        line2d = new Line2D.Double(40, 0, 400, 400);
        g2.draw(line2d);
    }
}

you can use bessenheim (dunno how to properly pronounce) algorith to create a line... 您可以使用bessenheim(不知道如何正确发音)algorith来创建线...

then you merely have to check if you click into that line... 那么您只需检查是否单击该行...

public class Line {

    public static ArrayList<Point> getLine(Point start, Point target) {
        ArrayList<Point> ret = new ArrayList<Point>();

        int x0 =  start.x;
        int y0 =  start.y;

        int x1 = target.x;
        int y1 = target.y;

        int sx = 0;
        int sy = 0;

        int dx =  Math.abs(x1-x0);
        sx = x0<x1 ? 1 : -1;
        int dy = -1*Math.abs(y1-y0);
        sy = y0<y1 ? 1 : -1; 
        int err = dx+dy, e2; /* error value e_xy */

        for(;;){  /* loop */
            ret.add( new Point(x0,y0) );
            if (x0==x1 && y0==y1) break;
            e2 = 2*err;
            if (e2 >= dy) { err += dy; x0 += sx; } /* e_xy+e_x > 0 */
            if (e2 <= dx) { err += dx; y0 += sy; } /* e_xy+e_y < 0 */
        }

        return ret;
    }

}

NOTE - this algorith is not my idea, it was developed by some guy named ?bessenheim? 注意-这个算法不是我的主意,它是由一个名叫“ bessenheim”的人开发的。 ^^ hehe much fun googeling ^^ ^^呵呵,太好玩了^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM