下面列出了怎么用java.awt.event.MouseMotionListener的API类实例代码及写法,或者点击链接到github查看源代码。
private void hoverEdge(TestEdge edge) {
// if the vertex is visible, then at least part of the edge will be
ensureVertexVisible(edge.getStart());
Point2D graphSpaceEdgePoint = findHoverPointInGraphSpace(edge);
Point viewPoint =
GraphViewerUtils.translatePointFromGraphSpaceToViewSpace(graphSpaceEdgePoint, viewer);
int mods = 0;
MouseEvent e = new MouseEvent(viewer, MouseEvent.MOUSE_MOVED, System.currentTimeMillis(),
mods, viewPoint.x, viewPoint.y, 0, false);
MouseMotionListener[] listeners = viewer.getMouseMotionListeners();
swing(() -> {
for (MouseMotionListener listener : listeners) {
listener.mouseMoved(e);
}
});
tooltipSpy.clearTooltipTriggered();
AbstractGTest.waitForCondition(() -> tooltipSpy.isTooltipTriggered(),
"Timed-out waiting for tooltip to appear");
waitForSwing();
}
/**
* Removes components that is only used when in game.
*/
public void removeInGameComponents() {
// remove listeners, they will be added when launching the new game...
KeyListener[] keyListeners = getKeyListeners();
for (KeyListener keyListener : keyListeners) {
removeKeyListener(keyListener);
}
MouseListener[] mouseListeners = getMouseListeners();
for (MouseListener mouseListener : mouseListeners) {
removeMouseListener(mouseListener);
}
MouseMotionListener[] mouseMotionListeners = getMouseMotionListeners();
for (MouseMotionListener mouseMotionListener : mouseMotionListeners) {
removeMouseMotionListener(mouseMotionListener);
}
for (Component c : getComponents()) {
removeFromCanvas(c);
}
}
/**
* Creates a new {@code FreeColMenuBar}. This menu bar will include
* all of the submenus and items.
*
* @param freeColClient The main controller.
* @param listener An optional mouse motion listener.
*/
public InGameMenuBar(FreeColClient freeColClient, MouseMotionListener listener) {
// FIXME: FreeColClient should not have to be passed in to
// this class. This is only a menu bar, it doesn't need a
// reference to the main controller. The only reason it has
// one now is because DebugMenu needs it. And DebugMenu needs
// it because it is using inner classes for ActionListeners
// and those inner classes use the reference. If those inner
// classes were in seperate classes, when they were created,
// they could use the FreeColClient reference of the
// ActionManger. So DebugMenu needs to be refactored to remove
// inner classes so that this MenuBar can lose its unnecessary
// reference to the main controller. See FreeColMenuTest.
//
// Okay, I lied.. the update() and paintComponent() methods in
// this MenuBar use freeColClient, too. But so what. Move
// those to another class too. :)
super(freeColClient);
// Add a mouse listener so that autoscrolling can happen in
// this menubar
this.addMouseMotionListener(listener);
reset();
}
/**
* Set whether the coordinate system is movable with the mouse, i.e.
* the scope of the system changes as the the mouse is clicked, held
* and dragged over the system.
*
* @param movable
* If true, move is possible.
*/
public void setMovable(boolean movable) {
if (this.movable && movable) return;
if (!this.movable && !movable) return;
if (movable) {
addMouseListener(mouseListener);
addMouseMotionListener((MouseMotionListener) mouseListener);
}
else {
removeMouseListener(mouseListener);
removeMouseMotionListener((MouseMotionListener) mouseListener);
}
movable = !movable;
}
private MainPanel() {
super(new BorderLayout());
// title, resizable, closable, maximizable, iconifiable
JInternalFrame immovableFrame = new JInternalFrame("immovable", false, false, true, true);
Component north = ((BasicInternalFrameUI) immovableFrame.getUI()).getNorthPane();
MouseMotionListener[] actions = north.getListeners(MouseMotionListener.class);
for (MouseMotionListener l: actions) {
north.removeMouseMotionListener(l);
}
// immovableFrame.setLocation(0, 0);
immovableFrame.setSize(160, 0);
desktop.add(immovableFrame);
immovableFrame.setVisible(true);
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
desktop.addComponentListener(new ComponentAdapter() {
@Override public void componentResized(ComponentEvent e) {
immovableFrame.setSize(immovableFrame.getSize().width, e.getComponent().getSize().height);
}
});
add(desktop);
add(createMenuBar(), BorderLayout.NORTH);
setPreferredSize(new Dimension(320, 240));
}
@Override
public void mouseDragged(MouseEvent e) {
SwingGraphicsBridge.component.mouseDragged(e);
if (active)
for (MouseMotionListener mouseMotion1 : mouseMotion)
mouseMotion1.mouseDragged(e);
}
@Override
public void mouseMoved(MouseEvent e) {
SwingGraphicsBridge.component.mouseMoved(e);
if (active)
for (MouseMotionListener mouseMotion1 : mouseMotion)
mouseMotion1.mouseMoved(e);
}
public JMapController(JMapViewer map) {
this.map = map;
if (this instanceof MouseListener)
map.addMouseListener((MouseListener) this);
if (this instanceof MouseWheelListener)
map.addMouseWheelListener((MouseWheelListener) this);
if (this instanceof MouseMotionListener)
map.addMouseMotionListener((MouseMotionListener) this);
}
/**
* Strips off the UI's mouse listeners attached to the associated toolbar
* and replaces them with this handler's listeners.
*/
private void installListeners() {
if (!ourVersionIsCompatible)
return;
ourToolBar.removePropertyChangeListener("UI", ourUIListener);
// Uninstall the current ui, collect the remaining listeners
// on the toolbar, and reinstall the ui...
final ComponentUI ui = ourToolBar.getUI();
ui.uninstallUI(ourToolBar);
final java.util.List mList = Arrays.asList(ourToolBar
.getListeners(MouseListener.class));
final java.util.List mmList = Arrays.asList(ourToolBar
.getListeners(MouseMotionListener.class));
ui.installUI(ourToolBar);
// ...then remove the listeners that were added by the ui...
final MouseListener[] ml = ourToolBar
.getListeners(MouseListener.class);
final MouseMotionListener[] mml = ourToolBar
.getListeners(MouseMotionListener.class);
for (int i = 0; i < ml.length; i++) {
if (!mList.contains(ml[i]))
ourToolBar.removeMouseListener(ml[i]);
}
for (int i = 0; i < mml.length; i++) {
if (!mmList.contains(mml[i]))
ourToolBar.removeMouseMotionListener(mml[i]);
}
// ...and add our listeners to the toolbar.
ourToolBar.addMouseListener(ourDragListener);
ourToolBar.addMouseMotionListener(ourDragListener);
ourToolBar.addPropertyChangeListener("UI", ourUIListener);
}
/**
* Creates a new {@code MapEditorMenuBar}. This menu bar will include
* all of the submenus and items.
*
* @param freeColClient The {@code FreeColClient} for the game.
* @param listener An optional mouse motion listener.
*/
public MapEditorMenuBar(final FreeColClient freeColClient, MouseMotionListener listener) {
super(freeColClient);
// Add a mouse listener so that autoscrolling can happen in this menubar
this.addMouseMotionListener(listener);
reset();
}
/**
* Maps {@code Component.addMouseMotionListener(MouseMotionListener)}
* through queue
*/
public void addMouseMotionListener(final MouseMotionListener mouseMotionListener) {
runMapping(new MapVoidAction("addMouseMotionListener") {
@Override
public void map() {
getSource().addMouseMotionListener(mouseMotionListener);
}
});
}
/**
* Maps
* {@code Component.removeMouseMotionListener(MouseMotionListener)}
* through queue
*/
public void removeMouseMotionListener(final MouseMotionListener mouseMotionListener) {
runMapping(new MapVoidAction("removeMouseMotionListener") {
@Override
public void map() {
getSource().removeMouseMotionListener(mouseMotionListener);
}
});
}
public void removeListener() {
for (MouseListener ml : com.getMouseListeners()) {
com.removeMouseListener(ml);
}
for (MouseMotionListener mml : com.getMouseMotionListeners()) {
com.removeMouseMotionListener(mml);
}
for (KeyListener kl : com.getKeyListeners()) {
com.removeKeyListener(kl);
}
reset();
com.repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaintDecorations(false);
if (mouseListener instanceof MouseMotionListener)
((MouseMotionListener) mouseListener).mouseDragged(e);
}
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaintDecorations(false);
if (mouseListener instanceof MouseMotionListener)
((MouseMotionListener) mouseListener).mouseMoved(e);
}
/**
* Instantiates a new char map4 grid.
*
* @param charMap4
*/
public CharMap4Grid(CharMap4 charMap4) {
super(); // initialize our superclass first (JPanel)
this.charMap4 = charMap4;
// Set class instance variables to undefined values that we will recognise
// if we are called before the layout and first "paint" is complete. */
cellCount = charCount = glyphCount = 0; // no chars or glyphs to display
clickIndex = NO_MOUSE; // cell index of clicked character
clickStartX = clickStartY = NO_MOUSE; // no starting coordinates for click
cornerIndex = 0; // cell index of top-left corner
fontData = null; // information about current display font
horizStep = 100; // horizontal offset from one cell to next
hoverIndex = NO_MOUSE; // cell index of mouse over character
lineAscent = 100; // number of pixels above baseline
lineHeight = 100; // height of each display line in pixels
maxWidth = 100; // maximum pixel width of all characters
panelColumns = 10; // number of complete text columns displayed
panelCount = -1; // saved value of <cellCount> used previously
panelFont = null; // saved font for drawing text on this panel
panelHeight = panelWidth = -1; // saved panel height and width in pixels
panelRows = 10; // number of complete lines (rows) displayed
vertiStep = 100; // vertical offset from one cell to next
/* Install our mouse and scroll listeners. */
this.addMouseListener((MouseListener) this);
this.addMouseMotionListener((MouseMotionListener) this);
this.addMouseWheelListener((MouseWheelListener) this);
// this.setFocusable(false); // we don't handle keyboard input, owner does
}
public JMapController(JMapViewer map) {
this.map = map;
if (this instanceof MouseListener)
map.addMouseListener((MouseListener) this);
if (this instanceof MouseWheelListener)
map.addMouseWheelListener((MouseWheelListener) this);
if (this instanceof MouseMotionListener)
map.addMouseMotionListener((MouseMotionListener) this);
}
/**
* Adds the specified mouse motion listener to receive mouse motion
* events from this component. If listener l is null, no exception is
* thrown and no action is performed.
*
* @param l the mouse motion listener
*/
public void addMouseMotionListener(MouseMotionListener l) {
if (frame != null) {
frame.addMouseMotionListener(l);
} else {
component.addMouseMotionListener(l);
}
}
/**
* Creates a new SeaGlassTabbedPaneMouseHandler object.
*
* @param originalMouseListener the original mouse handler.
*/
public SeaGlassTabbedPaneMouseHandler(MouseListener originalMouseListener) {
delegate = originalMouseListener;
delegate2 = (MouseMotionListener) originalMouseListener;
closeButtonHoverIndex = -1;
closeButtonArmedIndex = -1;
}
@Override
protected MouseMotionListener getMouseMotionListener() {
if (myMouseMotionListener == null) {
myMouseMotionListener = new MouseMotionListenerBase(getUIFacade(), getImplementation());
}
return myMouseMotionListener;
}
@Override public void updateUI() {
super.updateUI();
BasicInternalFrameUI ui = (BasicInternalFrameUI) getUI();
Component titleBar = ui.getNorthPane();
for (MouseMotionListener l: titleBar.getListeners(MouseMotionListener.class)) {
titleBar.removeMouseMotionListener(l);
}
DragWindowListener dwl = new DragWindowListener();
titleBar.addMouseListener(dwl);
titleBar.addMouseMotionListener(dwl);
}
private MainPanel() {
super(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane() {
private transient MouseMotionListener hoverHandler;
@Override public void updateUI() {
removeMouseMotionListener(hoverHandler);
super.updateUI();
hoverHandler = new MouseAdapter() {
@Override public void mouseMoved(MouseEvent e) {
JTabbedPane source = (JTabbedPane) e.getComponent();
int num = source.indexAtLocation(e.getX(), e.getY());
for (int i = 0; i < source.getTabCount(); i++) {
source.setForegroundAt(i, i == num ? Color.GREEN : Color.BLACK);
}
}
};
addMouseMotionListener(hoverHandler);
}
};
tabbedPane.addTab("11111", new JScrollPane(new JTree()));
tabbedPane.addTab("22222", new JScrollPane(new JLabel("asdfasdfsadf")));
tabbedPane.addTab("33333", new JScrollPane(new JTree()));
tabbedPane.addTab("44444", new JScrollPane(new JLabel("qerwqerqwerqwe")));
tabbedPane.addTab("55555", new JScrollPane(new JTree()));
add(tabbedPane);
setPreferredSize(new Dimension(320, 240));
}
protected MarkerBarListener lookupListsner(MarkerBar markerBar) {
MouseMotionListener[] listeners = markerBar.getMouseMotionListeners();
if (listeners != null) {
for (int counter = 0; counter < listeners.length; counter++) {
if (listeners[counter] instanceof MarkerBarListener) {
return (MarkerBarListener) listeners[counter];
}
}
}
return null;
}
private void render() {
// frame.getFrame().getContentPane().setBackground(Color.GREEN);
frame.addMouseMotionListener((MouseMotionListener) Mouse);
frame.addMouseListener(Mouse);
frame.addKeyListener(Key);
BufferStrategy bs = frame.getBufferStrategy();
if (bs == null) {
frame.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, frame.getWidth(), frame.getHeight());
g.setColor(new Color(0xFF660000));
g.fillRect(0, 0, WIDTH * 3, HEIGHT * 3);
g.setColor(new Color(0xFFFF9900));
g.setFont(font.getArial());
if (isGameOver()) {
g.drawString("GAME OVER... What will you do now?", 35, 30);
} else {
String name = (Game.getJdata_UserName().length() >= 1) ? WordUtils
.capitalizeFully(Game.getJdata_UserName()).toString()
: "Player";
g.drawString("Welcome to JavaGame " + name, 35, 30);
}
g.drawLine(0, HEIGHT * 3, 0, 0);
g.drawLine(0, 0, (WIDTH * 3), 0);
g.drawLine((WIDTH * 3), 0, (WIDTH * 3), (HEIGHT * 3));
g.drawLine(0, (HEIGHT * 3), (WIDTH * 3), (HEIGHT * 3));
// (LEFT,DOWN,WIDTH,HEIGHT)
paintButtons(isSelectedStart(), isSelectedExit(), g);
bs.show();
g.dispose();
}
private static void fireMouseMotion(MouseMotionListener listener, final MouseEvent event) {
switch (event.getID()) {
case MouseEvent.MOUSE_DRAGGED:
listener.mouseDragged(event);
case MouseEvent.MOUSE_MOVED:
listener.mouseMoved(event);
}
}
public static void addMouseMotionListener(Component c, MouseMotionListener l) {
c.addMouseMotionListener(l);
if (c instanceof Container) {
final Container container = (Container)c;
Component[] children = container.getComponents();
for (Component child : children) {
addMouseMotionListener(child, l);
}
}
}
public static void removeMouseMotionListener(final Component component, final MouseMotionListener motionListener) {
component.removeMouseMotionListener(motionListener);
if (component instanceof Container) {
Container container = (Container)component;
for (int i = 0; i < container.getComponentCount(); i++) {
removeMouseMotionListener(container.getComponent(i), motionListener);
}
}
}
protected MouseMotionListener getMouseMotionListener() {
return getHandler();
}
@Override
public synchronized void addMouseMotionListener(final MouseMotionListener l) {}
@Override
public synchronized void addMouseMotionListener(final MouseMotionListener l) {}