📄 visualizer2d.java
字号:
/* * Copyright (c) 2006, Swedish Institute of Computer Science. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: Visualizer2D.java,v 1.9 2007/04/02 14:14:26 fros4943 Exp $ */package se.sics.cooja.plugins;import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;import org.apache.log4j.Logger;import se.sics.cooja.*;import se.sics.cooja.interfaces.*;/** * Visualizer2D is an abstract mote visualizer for simulations. All motes are * painted in the XY-plane, as seen from positive Z axis. * * An implementation of this class must colorize the different motes, each mote * has two different colors; inner and outer. * * By right-clicking the mouse on a mote a popup menu will be displayed. From * this menu mote plugins can be started. or the mote can be moved. Each * implementation may also register its own actions to be accessed from this * menu. * * A Visualizer2D observers both the simulation and all mote positions. * * @author Fredrik Osterlind */@ClassDescription("2D Mote Visualizer")@PluginType(PluginType.SIM_PLUGIN)public abstract class Visualizer2D extends VisPlugin { private static final long serialVersionUID = 1L; private static Logger logger = Logger.getLogger(Visualizer2D.class); private double factorXCoordToPixel; private double factorYCoordToPixel; private double smallestXCoord; private double smallestYCoord; private Simulation simulation = null; private final JPanel canvas; private Visualizer2D myPlugin; private static final int CANVAS_BORDER_WIDTH = 25; private static final int MOTE_RADIUS = 8; private boolean moteIsBeingMoved = false; private long moteMoveBeginTime = -1; private Mote moteToMove = null; private Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR); private Observer simObserver = null; // Watches simulation changes private Observer posObserver = null; // Watches position changes public interface MoteMenuAction { public boolean isEnabled(Mote mote); public String getDescription(Mote mote); public void doAction(Mote mote); } private class MoveMoteMenuAction implements MoteMenuAction { public boolean isEnabled(Mote mote) { return true; } public String getDescription(Mote mote) { return "Move " + mote; } public void doAction(Mote mote) { moteMoveBeginTime = -1; beginMoveRequest(mote); } }; private class ButtonClickMoteMenuAction implements MoteMenuAction { public boolean isEnabled(Mote mote) { return mote.getInterfaces().getButton() != null && !mote.getInterfaces().getButton().isPressed(); } public String getDescription(Mote mote) { return "Click button on " + mote; } public void doAction(Mote mote) { mote.getInterfaces().getButton().clickButton(); } }; private Vector<MoteMenuAction> menuActions = new Vector<MoteMenuAction>(); /** * Registers as an simulation observer and initializes the canvas. * * @param simulationToVisualize * Simulation to visualize */ public Visualizer2D(Simulation simulationToVisualize, GUI gui) { super("Visualizer2D", gui); myPlugin = this; // Set initial bounds of frame this.setBounds(150, 150, 300, 300); setVisible(true); simulation = simulationToVisualize; // Create "canvas" to paint on canvas = new JPanel() { private static final long serialVersionUID = 1L; public void paintComponent(Graphics g) { super.paintComponent(g); visualizeSimulation(g); } }; canvas.setPreferredSize(new Dimension(getSize().width - 16, getSize().height - 38)); canvas.setBorder(BorderFactory.createLineBorder(Color.GREEN, 2)); canvas.setBackground(Color.WHITE); calculateTransformations(); this.setContentPane(canvas); // Detect general simulation changes posObserver = new Observer() { public void update(Observable obs, Object obj) { calculateTransformations(); canvas.repaint(); } }; simulation.addObserver(simObserver = new Observer() { public void update(Observable obs, Object obj) { canvas.setPreferredSize(new Dimension(getSize().width - 16, getSize().height - 38)); // Register (or reregister) as observer on all mote positions for (int i = 0; i < simulation.getMotesCount(); i++) { Position posIntf = simulation.getMote(i).getInterfaces() .getPosition(); if (posIntf != null) { posIntf.addObserver(posObserver); } } calculateTransformations(); canvas.repaint(); } }); simObserver.update(null, null); canvas.addMouseMotionListener(new MouseMotionListener() { public void mouseMoved(MouseEvent e) { myPlugin.handleMoveRequest(e.getPoint().x, e.getPoint().y, false); } public void mouseDragged(MouseEvent e) { myPlugin.handleMoveRequest(e.getPoint().x, e.getPoint().y, false); } }); // Detect mouse events canvas.addMouseListener(new MouseListener() { public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) myPlugin.handlePopupRequest(e.getPoint().x, e.getPoint().y); else if (SwingUtilities.isLeftMouseButton(e)){ //myPlugin.handleMoveRequest(e.getPoint().x, e.getPoint().y, false); beginMoveRequest(e.getPoint().x, e.getPoint().y); } } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) myPlugin.handlePopupRequest(e.getPoint().x, e.getPoint().y); else { myPlugin.handleMoveRequest(e.getPoint().x, e.getPoint().y, true); } } public void mouseEntered(MouseEvent e) { if (e.isPopupTrigger()) myPlugin.handlePopupRequest(e.getPoint().x, e.getPoint().y); } public void mouseExited(MouseEvent e) { if (e.isPopupTrigger()) myPlugin.handlePopupRequest(e.getPoint().x, e.getPoint().y); } public void mouseClicked(MouseEvent e) { if (e.isPopupTrigger()) myPlugin.handlePopupRequest(e.getPoint().x, e.getPoint().y); } }); // Detect component events addComponentListener(new ComponentListener() { public void componentMoved(ComponentEvent ce) { // NOP } public void componentShown(ComponentEvent ce) { // NOP } public void componentHidden(ComponentEvent ce) { // NOP } public void componentResized(ComponentEvent ce) { canvas.setPreferredSize(new Dimension(getSize().width - 16, getSize().height - 38)); calculateTransformations(); canvas.repaint(); } }); // Add menu action for moving motes addMoteMenuAction(new MoveMoteMenuAction()); // Add menu action for clicking mote button addMoteMenuAction(new ButtonClickMoteMenuAction()); try { setSelected(true); } catch (java.beans.PropertyVetoException e) { // Could not select } } /** * Add new mote menu action. * * @see MoteMenuAction * @param menuAction Menu action */ public void addMoteMenuAction(MoteMenuAction menuAction) { menuActions.add(menuAction); } private void handlePopupRequest(final int x, final int y) { final Vector<Mote> foundMotes = findMotesAtPosition(x, y); if (foundMotes == null || foundMotes.size() == 0) return; JPopupMenu pickMoteMenu = new JPopupMenu(); pickMoteMenu.add(new JLabel("Select action:")); pickMoteMenu.add(new JSeparator()); // Add 'show mote plugins'-actions for (final Mote mote : foundMotes) { final Point pos = new Point(canvas.getLocationOnScreen().x + x, canvas .getLocationOnScreen().y + y); pickMoteMenu.add(simulation.getGUI().createMotePluginsSubmenu(mote)); } // Add the rest of the actions for (final MoteMenuAction menuAction : menuActions) { for (final Mote mote : foundMotes) { if (menuAction.isEnabled(mote)) { JMenuItem menuItem = new JMenuItem(menuAction.getDescription(mote)); menuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { menuAction.doAction(mote);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -