📄 moteplugin.java
字号:
graphics.setColor(SELECTED_COLOR); } else { graphics.setColor(BASIC_COLOR); } } else { if (moteSimObject.isSelected()) { graphics.setColor(SELECTED_COLOR_OFF); } else { graphics.setColor(BASIC_COLOR_OFF); } } MoteCoordinateAttribute coordinate = moteSimObject.getCoordinate(); int x = (int)cT.simXToGUIX(coordinate.getX()); int y = (int)cT.simYToGUIY(coordinate.getY()); // Drawing Mote itself based on coordinate int size = moteSimObject.getObjectSize(); int xl = x-(size/2); int yl = y-(size/2); graphics.fillOval(xl, yl, size, size); // Writing out MoteID int id = moteSimObject.getID(); graphics.setColor(Color.black); graphics.drawString(Integer.toString(id), xl, yl-1); // Drawing Leds MoteLedsAttribute leds = (MoteLedsAttribute)moteSimObject.getAttribute("net.tinyos.sim.MoteLedsAttribute"); if (leds.redLedOn()) { graphics.setColor(Color.red); graphics.fillRect(xl, y-(size/6), size/3, size/3); } if (leds.greenLedOn()) { graphics.setColor(Color.green); graphics.fillRect(xl+(size/3), y-(size/6), size/3, size/3); } if (leds.yellowLedOn()) { graphics.setColor(Color.yellow); graphics.fillRect(xl + (2*size/3), y-(size/6), size/3, size/3); } } } public String toString() { return "MotePlugin"; } private void loadLocationFile(File locfile) throws IOException { if (DEBUG) System.out.println("MOTEPLUGIN: Loading location file "+locfile); spatialReader = new SpatialReader(locfile); Iterator it = state.getMoteSimObjects().iterator(); while (it.hasNext()) { MoteSimObject mote = (MoteSimObject)it.next(); SpatialReader.SREntry sEntry = spatialReader.getEntry(mote.getID()); if (sEntry != null) { if (DEBUG) System.out.println("MOTEPLUGIN: Setting "+mote+" location to "+sEntry.getX()+","+sEntry.getY()); mote.addAttribute(new MoteCoordinateAttribute(sEntry.getX(), sEntry.getY())); mote.addCoordinateChangedEvent(); } } } private void saveLocationFile(File locfile) throws IOException { SpatialWriter sWriter = new SpatialWriter(locfile); Iterator it = state.getMoteSimObjects().iterator(); while (it.hasNext()) { MoteSimObject mote = (MoteSimObject)it.next(); MoteCoordinateAttribute coordAttrib = mote.getCoordinate(); sWriter.writeEntry(mote.getID(), coordAttrib.getX(), coordAttrib.getY()); } sWriter.done(); } private void doLayout() { if (DEBUG) System.out.println("MOTEPLUGIN: doing layout"); int nummotes = state.getMoteSimObjects().size(); if (DEBUG) System.out.println("MOTEPLUGIN: layout: "+nummotes+" motes"); //if (nummotes == 0) return; if (layout == LAYOUT_FILE) { if (spatialReader == null) return; Iterator it = state.getMoteSimObjects().iterator(); while (it.hasNext()) { MoteSimObject mote = (MoteSimObject)it.next(); SpatialReader.SREntry sEntry = spatialReader.getEntry(mote.getID()); if (sEntry != null) { mote.addAttribute(new MoteCoordinateAttribute(sEntry.getX(), sEntry.getY())); mote.addCoordinateChangedEvent(); } } return; } int num_rows = 1, num_columns = 1; double xspacing = 0, yspacing = 0; int MAX_SPACING = 4*SimConst.MOTE_OBJECT_SIZE; double realwidth = cT.getMoteScaleWidth() * 0.9; double realheight = cT.getMoteScaleHeight() * 0.9; double xbase = 0.0, ybase = 0.0; if (layout == LAYOUT_GRID || layout == LAYOUT_GRID_RANDOM) { int side = (int)(Math.ceil(Math.sqrt(nummotes * 1.0))); num_rows = side; num_columns = side; xspacing = realwidth / num_columns; yspacing = realheight / num_rows; if (xspacing > MAX_SPACING) xspacing = MAX_SPACING; if (yspacing > MAX_SPACING) yspacing = MAX_SPACING; if (nummotes > 2) { xbase = 50.0 - xspacing * ((side-1) / 2.0); ybase = 50.0 - yspacing * ((side-1) / 2.0); } else { xbase = 50.0 - xspacing * ((side-1) / 2.0); ybase = 50.0; } } Iterator it = state.getMoteSimObjects().iterator(); while (it.hasNext()) { MoteSimObject mote = (MoteSimObject)it.next(); if (layout == LAYOUT_RANDOM) { addRandomMoteCoordAttribute(mote); } else if (layout == LAYOUT_GRID || layout == LAYOUT_GRID_RANDOM) { int rownum = mote.getID() / num_columns; int colnum = mote.getID() % num_columns; double x = (xspacing * colnum) + xbase; double y = (yspacing * rownum) + ybase; if (layout == LAYOUT_GRID_RANDOM) { x += ((rand.nextDouble() * GRID_RANDOM_DEVIATION*2.0) - GRID_RANDOM_DEVIATION); y += ((rand.nextDouble() * GRID_RANDOM_DEVIATION*2.0) - GRID_RANDOM_DEVIATION); } mote.addAttribute(new MoteCoordinateAttribute(x, y)); } mote.addCoordinateChangedEvent(); } } public void addRandomMoteCoordAttribute(MoteSimObject mote) { double x = Math.random()*(cT.getMoteScaleWidth() * 0.8); double y = Math.random()*(cT.getMoteScaleHeight() * 0.8); x += (cT.getMoteScaleWidth() * 0.1); y += (cT.getMoteScaleHeight() * 0.1); mote.addAttribute(new MoteCoordinateAttribute(x, y)); } public void setLayout(int thelayout) { if (thelayout != LAYOUT_GRID && thelayout != LAYOUT_RANDOM && thelayout != LAYOUT_GRID_RANDOM && thelayout != LAYOUT_FILE) { throw new IllegalArgumentException("Invalid setting for layout: "+layout); } this.layout = thelayout; doLayout(); } class bPowerListener implements ActionListener { public void actionPerformed(ActionEvent e) { Iterator it = state.getSelectedMoteSimObjects().iterator(); try { while (it.hasNext()) { MoteSimObject mote = (MoteSimObject)it.next(); // Toggle power status for each mote if (mote.getPower()) { simComm.sendCommand(new TurnOffMoteCommand((short)mote.getID(), 0L)); mote.setPower(false); } else { simComm.sendCommand(new TurnOnMoteCommand((short)mote.getID(), 0L)); mote.setPower(true); } motePanel.refresh(); } } catch (IOException ioe) { System.err.println("Cannot send command: "+ioe); } } } class menuListener implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Grid")) { layout = LAYOUT_GRID; doLayout(); } else if (e.getActionCommand().equals("Random")) { layout = LAYOUT_RANDOM; doLayout(); } else if (e.getActionCommand().equals("Grid + Random")) { layout = LAYOUT_GRID_RANDOM; doLayout(); } else if (e.getActionCommand().equals("File Load")) { int oldlayout = layout; layout = LAYOUT_FILE; JFileChooser fc = new JFileChooser(System.getProperty("user.dir")); fc.addChoosableFileFilter(new MotePositionsFileFilter()); int returnVal = fc.showOpenDialog(motePanel); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); try { loadLocationFile(file); } catch (IOException exception) { tv.setStatus("Error reading location file "+file.getName()); layout = oldlayout; } } else { layout = oldlayout; } } else if (e.getActionCommand().equals("File Save")) { JFileChooser fc = new JFileChooser(System.getProperty("user.dir")); fc.addChoosableFileFilter(new MotePositionsFileFilter()); int returnVal = fc.showSaveDialog(motePanel); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); try { saveLocationFile(file); } catch (IOException ioException) { tv.setStatus("Error writing spatial file "+file.getName()); System.out.println(ioException); ioException.printStackTrace(); } } } motePanel.refresh(); } protected class MotePositionsFileFilter extends javax.swing.filechooser.FileFilter { public MotePositionsFileFilter() {} public boolean accept(File f) { if (f.isDirectory()) { return true; } String name = f.getName(); int index = name.lastIndexOf('.'); if (index < 0) {return false;} String extension = name.substring(index); if (extension != null) { if (extension.equals(".mps")) { return true; } else { return false; } } return false; } public String getDescription() { return "*.mps"; } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -