javaswing:有没有一种方法可以在每次鼠标移动的时候都不检查鼠标在绘制的形状上的移动?

xcitsw88  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(358)

我有一张我画的画。我画的形状是存储在列表中的一些对象。我想在这些画出来的物体上注册鼠标。我现在正在做的是添加一个mousemotionlistener,它在每次鼠标移动时检查对象列表中是否有点击。这当然是相当低效一旦有很多对象。有没有比每次鼠标移动时都检查所有对象更好的方法来检查鼠标移动?
下面是一个简单的例子:
类ui.java:

public class Ui {
    private static JFrame frame;
    private static DrawPanel drawPanel;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> { createGui();
        });
    }
    private static void createGui() {
        frame = new JFrame("Test");
        drawPanel = new DrawPanel();
        frame.setContentPane(drawPanel);
        frame.pack();
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }
}

类someobject.java:

public class SomeObject {
    //class that represents some object that will be drawn. note that this is 
    //just a minmal example and that in my actual application, there are      
    //other aspects and functions to this class that have nothing to do with drawing. 
    //This is just kept small for the sake being a minimal example
    private String id;
    private Point2D origin;
    private int length;
    private int height;

    public SomeObject(String id, Point2D origin, int length, int height) {
        this.id = id;
        this.origin = origin;
        this.length = length;
        this.height = height;
    }

    public String getId() {
        return id;
    }

    public Point2D getOrigin() {
        return origin;
    }

    public int getLength() {
        return length;
    }

    public int getHeight() {
        return height;
    }
}

custommousemotionlistener.java类:

public class CustomMouseMotionListener implements java.awt.event.MouseMotionListener {
    public CustomMouseMotionListener() { }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        for (SomeObject object: DrawPanel.objectsToDraw) {
            Shape s = new Rectangle((int)object.getOrigin().getX(), (int)object.getOrigin().getY(), object.getLength(), object.getHeight());
            if (s.contains(e.getPoint())) {
                System.out.println("hit: " + object.getId());
            }
        }
    }
}

类drawpanel.java:

public class DrawPanel extends JPanel {
    public static List<SomeObject> objectsToDraw = new ArrayList<>();
    public DrawPanel() {
        objectsToDraw.add( new SomeObject("a", new Point2D.Float(20,1), 20,20));
        objectsToDraw.add( new SomeObject("b", new Point2D.Float(20,45), 20,20));

        addMouseMotionListener(new CustomMouseMotionListener());
        setFocusable(true);
        setVisible(true);
        grabFocus();
    }

    protected void paintComponent(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        super.paintComponent(g2D);
        for (SomeObject object: objectsToDraw) {
            g.drawRect((int)object.getOrigin().getX(), (int)object.getOrigin().getY(), object.getLength(), object.getHeight());

        }
    }
}
ymdaylpp

ymdaylpp1#

物体不仅仅是被画出来的,所以我想我不能用形状来代替它,
那你就保持沉默 Rectangle 示例作为 SomeObject 类,而不是点和长度/高度变量。
然后修改方法以从 Rectangle .
这将阻止您不断创建新的矩形示例,提高进程的内存效率并减少垃圾收集。
但是,您还应该能够扩展rectangle类并添加额外的功能,就像扩展jpanel以添加自定义绘制逻辑一样。

ocebsuys

ocebsuys2#

而不是用你的 SomeObject 类,我建议使用 Shape 或者 Area . 某个物体的全部目的似乎就是要变成一个形状,对吧?不仅如此,通过一个形状数组列表,你可以消除在每个鼠标移动中为你的形状创建矩形。
顺便说一句,几年前,我整理了一套处理像一级组件领域。您可以在这里看到:https://sourceforge.net/p/tus/code/head/tree/tjacobs/ui/shape/
(从areamanager和areamodel开始)。区域有一些优点和缺点:优点:易于作为一个组进行管理,如果它们重叠,则相当容易测试。缺点:您会丢失有关区域构造方式的信息(例如多边形点、圆半径等)
你已经走了很长一段路了,真是太好了。这个答案(a)回答了你关于效率的问题,但也指出了你可以选择的一些方法

相关问题