放假了跟着导师做NS2的项目,在运行leach协议的时候涉及到一个节点的设置,带我们小组的研究生让我们写个节点生成器,这样就可以省了自己定义节点X,Y坐标了。于是晚上回来就自己写了个,虽然比较丑陋,但是还是很实用的,主要用到了两个类,一个是随机位置产生类,以及调用这个类的显示类。
随机节点类:
RandomNode.java package com.imis.node; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; import java.util.Random; public class RandomNode { public boolean generateNode(int nodeNum) { Random random; File file; Writer writer = null; try { file = new File(nodeNum + "nodes.txt"); writer = new FileWriter(file); for (int n = 0; n < 2 * nodeNum; n++) { random = new Random(); String after = ""; String before = ""; for (int i = 0; i < 15; i++) { after = after + random.nextInt(10); } before = before + random.nextInt(100); if (n % 2 == 0) { writer.write(before + "." + after + " "); } else { writer.write(before + "." + after + "\n"); } } writer.flush(); return true; } catch (IOException e) { e.printStackTrace(); return false; } finally { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
显示类:
enerateGUI.java package com.imis.node; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; public class GenerateGUI extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; public static void main(String[] args) { new GenerateGUI(); } RandomNode randomNode; JPanel jp1, jp2; JTextField jtf; JButton jb1, jb2; JLabel jl1; public GenerateGUI() { this.setTitle("节点位置随机产生器"); // 设置窗体大小 this.setSize(300, 200); // 窗体不可更改大小 this.setResizable(false); this.setLayout(new GridLayout(2, 1)); // 获取屏幕宽度和高度 int width = Toolkit.getDefaultToolkit().getScreenSize().width; int height = Toolkit.getDefaultToolkit().getScreenSize().height; // 设置窗体位置 this.setLocation((width - 300) / 2, (height - 200) / 2); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); jp1 = new JPanel(null); jp2 = new JPanel(null); jtf = new JTextField(); jtf.setBounds(75, 50, 150, 23); jl1 = new JLabel("请输入节点个数:"); jl1.setBounds(75, 20, 130, 23); jb1 = new JButton("确定"); jb1.setBounds(80, 24, 60, 25); jb1.addActionListener(this); jb2 = new JButton("退出"); jb2.setBounds(150, 24, 60, 25); jb2.addActionListener(this); jp1.add(jl1); jp1.add(jtf); jp2.add(jb1); jp2.add(jb2); this.add(jp1); this.add(jp2); randomNode = new RandomNode(); } @Override public void actionPerformed(ActionEvent arg0) { if (arg0.getSource() == jb1) { if (jtf.getText().trim().equals("")) { JOptionPane.showMessageDialog(this, "请输入节点个数!"); } try { int i = Integer.parseInt(jtf.getText().trim()); boolean success = randomNode.generateNode(i); if (success) { JOptionPane.showMessageDialog(this, "生成成功!"); jtf.setText(""); } } catch (Exception e) { JOptionPane.showMessageDialog(this, "请输入整数!"); jtf.setText(""); } } if (arg0.getSource() == jb2) { System.exit(0); } } }运行完后会产生一个节点个数+nodes.txt,例如100nodes.txt。