/** * Implements method for the <tt>MouseListener</tt> interface to draw the * selected shape into the drawing canvas. * * @param evt * The associated mouse event. **/ public void mouseClicked(MouseEvent evt) { if (m_selected == null) { return; } if (m_panel.contains(evt.getX(), evt.getY())) { ShapeComponent sc = new ShapeComponent(this, m_selected); sc.setBounds(evt.getX() - BOX / 2, evt.getY() - BOX / 2, BOX, BOX); m_panel.add(sc, 0); m_panel.validate(); m_panel.repaint(sc.getBounds()); } }
/** * If someone adds a mouseListener to the GlassPane or set a new cursor * we expect that he knows what he is doing and return the super.contains(x, y) otherwise * we return false to respect the cursors for the underneath components. */ @Override public boolean contains(int x, int y) { if (getMouseListeners().length == 0 && getMouseMotionListeners().length == 0 && getMouseWheelListeners().length == 0 && getCursor() == Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)) { return false; } return super.contains(x, y); } }
/** * First, implementatation of this method iterates through * glassPane's child components and returns {@code true} * if any of them is visible and contains passed x,y point. * After that it checks if no mouseListeners is attached to this component * and no mouse cursor is set, then it returns {@code false}, * otherwise calls the super implementation of this method. * * @param x the <i>x</i> coordinate of the point * @param y the <i>y</i> coordinate of the point * @return true if this component logically contains x,y */ public boolean contains(int x, int y) { for (int i = 0; i < getComponentCount(); i++) { Component c = getComponent(i); Point point = SwingUtilities.convertPoint(this, new Point(x, y), c); if (c.isVisible() && c.contains(point)) { return true; } } if (getMouseListeners().length == 0 && getMouseMotionListeners().length == 0 && getMouseWheelListeners().length == 0 && !isCursorSet()) { return false; } return super.contains(x, y); } }