首先是GUI类主要实现界面的搭建,要求是在此代码中添加代码实现水平和垂直滚动条的添加和鼠标键盘的监听和操作
package npu.wsn.fan.SULS;
import java.awt.Scrollbar;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.border.LineBorder;
import javax.swing.tree.DefaultMutableTreeNode;
import npu.wsn.fan.SULS.ScreenCapture;
import npu.wsn.fan.SULS.DataBase;
/**
* @author Fan.xx
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class GUI extends JFrame implements UpdateControl {
public GUI(MutiNodes mNodeValue){
this.mNode = mNodeValue;
self = this;
saveFileDialog = new JFileChooser(this.getClass().getResource("/").toString().substring(6));
runt = Runtime.getRuntime();
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
setResizable(false);
setJMenuBar(menuBar);
setTitle("SULS 1.0");
setLocation(250,50);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
store = new JButton("Store");
store.setMnemonic('S');
store.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
cal = Calendar.getInstance();
d = cal.getTime();
String name = null;
int i =0;
try{
database.InitData();
while(i < mNode.getNameList().size()){
name = (String)mNode.getNameList().get(i);
database.StoreData(mNode.getNode(name),d.toLocaleString());
i++;
}
}catch(Exception ea){
ea.printStackTrace();
}
ScreenCapture scp = new ScreenCapture(getX()+182,getY()+55,756,607);
paint();
scp.captureScreen();
try {
String command = "re.exe";
Process process = runt.exec(command);
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
update = new JButton("Update");
update.setMnemonic('U');
update.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
paint();
nodeTree = new JTree(GetTreeNode.getTree(mNode));
nodeTree.expandRow(1);
nodeTreePane.getViewport().add(nodeTree);
}
});
// menuItem init
saveDataItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int val = saveFileDialog.showSaveDialog(self);
File f = saveFileDialog.getSelectedFile();
if(f!= null){
try {
OutputStream os = new FileOutputStream(f);
os.write(text.getText().getBytes());
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
});
saveImgItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int val = saveFileDialog.showSaveDialog(self);
File f = saveFileDialog.getSelectedFile();
if(f!= null){
ScreenCapture scp = new ScreenCapture(getX()+182,getY()+55,756,607);
paint();
scp.captureScreen(f);
}
}
});
exitItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_X,InputEvent.CTRL_MASK));
exitItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
if(optionPane.showConfirmDialog(self,"Sure to quit ?","Confirmation",JOptionPane.WARNING_MESSAGE)==0){
System.exit(0);
}
}
});
setMenuItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_P,InputEvent.CTRL_MASK));
setMenuItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String comm = optionPane.showInputDialog(self , "Set coordinate of node ( Named: new1 Coordinate:0,0,0 )./n" +
"e.g. P PC new1 0 0 0","Setting",JOptionPane.QUESTION_MESSAGE);
if(comm != null){
try {
serialOut.write(new String(comm+"/r/n").getBytes());
paint();
} catch (IOException e1) {
// TODO 自动生成 catch 块
e1.printStackTrace();
}
}
}
});
getDataItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_D,InputEvent.CTRL_MASK));;
getDataItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
isGetData = !isGetData;
}});
helpItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
try {
String dir = this.getClass().getResource("/").toString().substring(6);
Process process = runt.exec("notepad "+dir+"readme.txt");
} catch (Exception e1) {
e1.printStackTrace();
}
}
});
aboutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
optionPane.showMessageDialog(self , "Self-Update Localization System/n" +
"Edition:1.0/n" +
"(c) 2008 Northwestern Polytechnical University WSN Lab","About",JOptionPane.INFORMATION_MESSAGE);
self.repaint();
}
});
zoomInItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_I,InputEvent.CTRL_MASK));;
zoomInItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
zoom +=0.5;
if(zoom>2.0)
zoom=2.0;
}
});
zoomOutItem.setAccelerator(KeyStroke.getKeyStroke (KeyEvent.VK_O,InputEvent.CTRL_MASK));;
zoomOutItem.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO 自动生成方法存根
zoom -=0.5;
if(zoom <1.0)
zoom=1.0;
}
});
setMenuItem.setBackground(Color.WHITE);
getDataItem.setBackground(Color.WHITE);
startMenu.add(setMenuItem);
startMenu.addSeparator();
startMenu.add(getDataItem);
startMenu.setMnemonic('T');
saveDataItem.setBackground(Color.WHITE);
saveImgItem.setBackground(Color.WHITE);
exitItem.setBackground(Color.WHITE);
fileMenu.add(saveDataItem);
fileMenu.add(saveImgItem);
fileMenu.addSeparator();
fileMenu.add(exitItem);
fileMenu.setMnemonic('F');
zoomInItem.setBackground(Color.WHITE);
zoomOutItem.setBackground(Color.WHITE);
viewMenu.add(zoomInItem);
viewMenu.add(zoomOutItem);
viewMenu.setMnemonic('V');
helpItem.setBackground(Color.WHITE);
aboutItem.setBackground(Color.WHITE);
helpMenu.add(helpItem);
helpMenu.addSeparator();
helpMenu.add(aboutItem);
helpMenu.setMnemonic('H');
menuBar.add(fileMenu);
menuBar.add(startMenu);
menuBar.add(viewMenu);
menuBar.add(helpMenu);
text = new JTextArea("---------------------------------------------------------------------------------/n" +
" Welcome to SULS ! /n" +
" Please check the nodes first and search the detail here . /n" +
"--------------------------------------------------------------------------------/n", 6, 68);
text.setBackground(Color.PINK);
text.setEditable(false);
nodeTree = new JTree(GetTreeNode.getTree(mNode));
nodeTreePane = new JScrollPane();
nodeTreePane.getViewport().add(nodeTree);
knownListener = new JTextField("",6);
knownBeacon = new JTextField("",6);
beacon = new JLabel("Beacon ");
listener = new JLabel("Listener ");
buttonPane = new JPanel();
buttonPane.setLayout(new GridLayout(3,2,20,10));
buttonPane.add(beacon);
buttonPane.add(knownBeacon);
buttonPane.add(listener);
buttonPane.add(knownListener);
buttonPane.add(update);
buttonPane.add(store);
buttonPane.setBackground(Color.LIGHT_GRAY);
drawpane = new JPanel();
drawpane.setSize(760,400);
drawpane.setBackground(Color.WHITE);
paneBack.setIcon(new ImageIcon("img.jpg"));
//drawpane.add(paneBack);
drawpane.setBorder(new LineBorder(Color.LIGHT_GRAY,5));
pane = new JScrollPane(text);
pane.setBorder(new LineBorder(Color.GRAY,2));
infopane = new JPanel();
infopane.setBackground(Color.LIGHT_GRAY);
infopane.setBorder(new LineBorder(Color.GRAY,0));
infopane.add(pane,BorderLayout.CENTER);
infopane.add(buttonPane,BorderLayout.EAST);
/* scr=new Scrollbar();
scr.setBounds(756, 607, 10, 100);
scr.setBackground(null);
scr.addMouseWheelListener(new MouseWheelListener)*/
Container contentPanel = getContentPane();
contentPanel.setBackground(Color.LIGHT_GRAY);
contentPanel.add(nodeTreePane,BorderLayout.WEST);
contentPanel.add(drawpane, BorderLayout.CENTER);
contentPanel.add(infopane, BorderLayout.SOUTH);
this.setVisible(true);
//optionPane.showMessageDialog(self, "Please first change the default distance between beacons(2m)","Warning",JOptionPane.WARNING_MESSAGE);
//paint();
}
/* (非 Javadoc)
* @see npu.wsn.fan.SULS.UpdateControl#newMsg(npu.wsn.fan.SULS.SignNode)
*/
public void newMsg(SignNode signNode) {
// TODO 自动生成方法存根
if(signNode.getMode().equalsIgnoreCase("L")){
text.append("Node : ID="+signNode.getSpaceID()+" , MD="+signNode.getMode()
+" , CD=("+signNode.getX()+","
+signNode.getY()+","
+signNode.getZ()+")"+" , "
//+signNode.getData().toString()
+"/n");
text.setCaretPosition(text.getText().length());
}
mNode.storeNode(signNode);
/*if(mNode.storeNode(signNode)==0){
optionPane.showMessageDialog(self , "Here comes the new node!/n" +
" " +signNode.getSpaceID()+" at "+
signNode.getX()+" "+
signNode.getY()+" ","New",JOptionPane.INFORMATION_MESSAGE);
}*/
paint();
}
private void paint(){
int beaconNum = mNode.getModeNum("B");
int listenerNum = mNode.getModeNum("L");
double radius = 7;
knownBeacon.setText(new Integer(beaconNum).toString());
knownListener.setText(new Integer(listenerNum).toString());
Graphics2D g = (Graphics2D) drawpane.getGraphics();
Rectangle2D rect = new Rectangle2D.Double(5, 5, 750, 600);//rect为Rectangle.Double类的一个实例,5为左上角的坐标x,y;750,600为宽度和高度
g.setPaint(Color.GRAY);//框的颜色
g.draw(rect);//绘制一个rect图像
Rectangle2D recttem = new Rectangle2D.Double(5, 5, 750, 600);
g.setPaint(Color.WHITE);//背景的颜色
g.fill(recttem);//注意fill和draw的区别,fill绘制一个填充的对象
//draw the net of showArea
g.setPaint(Color.GRAY);//线的颜色
Line2D line = new Line2D.Double();
for(int row = 0;row<605;){
line.setLine(5,50+row,755,50+row);
g.draw(line);
row+=100;
}
for(int lin = 0;lin<755;){
line.setLine(25+lin,5,25+lin,605);
g.draw(line);
lin+=100;
}
//show the BeaconNode
if(null != mNode){
Ellipse2D circle = new Ellipse2D.Double();
SignNode temp = null;
String nn;
int i =0;
while(i < mNode.getNameList().size()){
nn = (String)mNode.getNameList().get(i);
temp = mNode.getNode(nn);
if(temp.getMode().equalsIgnoreCase("B")){
g.setPaint(Color.BLUE);
}
else{
g.setPaint(Color.RED);
}
//前两个为中心点坐标,后两个为角点坐标,由两者共同设置此形状
circle.setFrameFromCenter(125+zoom*temp.getX(), 550-zoom*temp.getY(), 125+zoom*temp.getX()+ radius, 550-zoom*temp.getY()+ radius);
g.fill(circle);
g.setPaint(Color.DARK_GRAY);
//drawString()显示出指定的SpaceID,也就是显示出节点的号
g.drawString(temp.getSpaceID(),125+(int)(zoom*temp.getX()), 520-(int)(zoom*temp.getY()));
if(isGetData){//布尔型
g.setPaint(Color.BLUE);
g.drawString(temp.getData().toString(),125+(int)(zoom*temp.getX()), 540-(int)(zoom*temp.getY()));
//绘制指定矩形的边框
g.drawRect(160+(int)(zoom*temp.getX()), 533-(int)(zoom*temp.getY()), 20, 5);
int n = (temp.getData().getData()*20)/temp.getData().maxValue;
g.setPaint(Color.BLUE);
g.fillRect(160+(int)(zoom*temp.getX()), 533-(int)(zoom*temp.getY()), n, 5);
}
i++;
}
}
}
public void setSerialOut(OutputStream serialOut) {
this.serialOut = serialOut;
}
private MutiNodes mNode = null;
private JTextArea text;
private JTextField knownListener;
private JTextField knownBeacon;
private JLabel listener;
private JLabel beacon;
private JButton update;
private JButton store;
private JPanel buttonPane;
private JPanel drawpane;
private JPanel infopane;
private JScrollPane pane;
private JScrollPane nodeTreePane;
private JFrame self;
private JTree nodeTree;
private JLabel paneBack = new JLabel();
private JOptionPane optionPane = new JOptionPane();
private JMenuBar menuBar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenu startMenu = new JMenu("Tools");
private JMenu helpMenu = new JMenu("Help");
private JMenu viewMenu = new JMenu("View");
private JMenuItem setMenuItem = new JMenuItem("Setting",new ImageIcon("icon/set.png"));
private JMenuItem getDataItem = new JMenuItem("Get Data",new ImageIcon("icon/get.png"));
private JMenuItem saveDataItem = new JMenuItem("Save Data",new ImageIcon("icon/data.png"));
private JMenuItem saveImgItem = new JMenuItem("Save Image",new ImageIcon("icon/image.png"));
private JMenuItem exitItem = new JMenuItem("Exit",new ImageIcon("icon/exit.png"));
private JMenuItem aboutItem = new JMenuItem("About",new ImageIcon("icon/about.png"));
private JMenuItem helpItem = new JMenuItem("Help",new ImageIcon("icon/help.png"));
private JMenuItem zoomInItem = new JMenuItem("Zoom In" , new ImageIcon("icon/zoomin.png"));
private JMenuItem zoomOutItem = new JMenuItem("Zoom Out",new ImageIcon("icon/zoomout.png"));
private JFileChooser saveFileDialog ;
private Runtime runt =null;
private DataBase database = new DataBase();
private Calendar cal=null;
private Date d = null;
private OutputStream serialOut = null;
private double zoom = 1.0;
private boolean isGetData=false;
public final int DEFAULT_WIDTH = 943;
public final int DEFAULT_HEIGHT = 790;
}
其中引用的ScreenCapture类如下:
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ScreenCapture {
public ScreenCapture(int x, int y, int width,int height){
try{
robot = new Robot();
}catch(Exception e){
e.printStackTrace();
}
rect = new Rectangle(x,y,width,height);
}
public void captureScreen(){
BufferedImage bufImage = null;
String fn = "E:/web/www/pc.jpg";
try{
File f= new File(fn);
bufImage = robot.createScreenCapture(rect);
ImageIO.write(bufImage,"jpg",f);
}catch(Exception e){
e.toString();
}
}
public void captureScreen(File fn){
BufferedImage bufImage = null;
try{
bufImage = robot.createScreenCapture(rect);
ImageIO.write(bufImage,"jpg",fn);
}catch(Exception e){
e.toString();
}
}
private Robot robot;
private Rectangle rect;
}
引用的database为数据库,请高手指点啊谢谢帮忙了