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

时间:2014-09-28 05:36:01

标签: java graphics mouselistener

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");
    }
}

此变体不起作用。有没有其他方法可以将MouseListener添加到该行?


我找到了解决方案。我认为最好在那里添加不可见的Polygon。并抓住它 Mouseevents。因为与多边形不同,行没有可点击区域。请参阅下面的代码:

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

    }

}

2 个答案:

答案 0 :(得分:0)

可以将MouseListener添加到Component,例如:JPanel,JTextfield等,或者带有扩展Component的自定义类。请参阅以下示例。

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);
    }
}

答案 1 :(得分:0)

你可以使用bessenheim(dunno如何正确发音)算法来创建一条线......

然后你只需检查你是否点击该行...

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

}

注意 - 这个算法不是我的想法,它是由一个名叫?bessenheim的人开发的? ^^ hehe很有趣googeling ^^