我正在通过rmi创建桌面共享小工具,除了它不响应关键事件之外,每件事都很好。我不明白我缺少的地方..
这是服务器代码..
public class ServerInterface extends UnicastRemoteObject implements System_Behav {
private static final long serialVersionUID = 1L;
public static Robot robo=null;
public static int height;
public static int width;
protected ServerInterface() throws RemoteException {
super();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
if(System.getSecurityManager()==null)
System.setSecurityManager(new RMISecurityManager());
height=Toolkit.getDefaultToolkit().getScreenSize().height;
width=Toolkit.getDefaultToolkit().getScreenSize().width;
try{
System_Behav system_server=new ServerInterface();
Registry r=LocateRegistry.createRegistry(1099);
r.bind("system_server", system_server);
GraphicsEnvironment g=GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd=g.getDefaultScreenDevice();
robo=new Robot(gd);
System.out.println("server bound");
}catch(Exception e)
{
System.err.println("Computeappengine exception"+e.getMessage());
e.printStackTrace();
}
}
public void MouseMove(int x,int y,int w,int h) throws RemoteException {
// TODO Auto-generated method stub
x=x*(height/h);
y=y*(width/w);
robo.mouseMove(x, y);
}
public void Mousepress(int a) throws RemoteException {
// TODO Auto-generated method stub
robo.mousePress(a);
}
public void MouseRelease(int a) throws RemoteException {
// TODO Auto-generated method stub
robo.mouseRelease(a);
}
@Override
public void KeyPress(int a) throws RemoteException {
robo.keyPress(a);
}
@Override
public void KeyRelease(int a) throws RemoteException {
// TODO Auto-generated method stub
robo.keyRelease(a);
}
@Override
public byte[] getScreen() throws RemoteException {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle rectangle = new Rectangle(dim);
BufferedImage bi= robo.createScreenCapture(rectangle);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try {
ImageIO.write(bi, "PNG", bytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bytes.toByteArray();
}
}
这是客户端代码
public class ClientInterface{
/**
*
*/
private static final long serialVersionUID = 1L;
public static JPanel contentPane;
public static System_Behav sb=null;
/**
* Launch the application.
*/
public static void main(String[] args) {
if(System.getSecurityManager()==null)
System.setSecurityManager(new RMISecurityManager());
try{
String name="rmi://192.168.1.5:1099/system_server";
sb=(System_Behav)Naming.lookup(name);
try {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
frame.setContentPane(contentPane);
frame.setVisible(true);
new screen(contentPane,sb);
new command(contentPane,sb);
} catch (Exception e) {
e.printStackTrace();
}
}catch(Exception e)
{
System.err.println("Computeappengine exception"+e.getMessage());
e.printStackTrace();
}
}
public ClientInterface() {
}
}
public class command implements KeyListener,
MouseMotionListener,MouseListener {
public JPanel cPanel=null;
public System_Behav system_behav=null;
//public byte by[];
public command()
{
}
public command(JPanel j,System_Behav b)
{
system_behav=b;
cPanel=j;
cPanel.addKeyListener(this);
cPanel.addMouseListener(this);
cPanel.addMouseMotionListener(this);
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
int a=e.getButton();
if (a==3)
a=4;
else a=16;
try {
system_behav.Mousepress(a);
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
int a=e.getButton();
//System.out.println(a);
if (a==3)
a=4;
else a=16;
try {
system_behav.MouseRelease(a);
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
try {
system_behav.MouseMove(e.getX(),e.getY(),cPanel.getWidth(),cPanel.getHeight());
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
try {
system_behav.KeyRelease(e.getKeyCode());
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
try {
system_behav.KeyPress(e.getKeyCode());
} catch (RemoteException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
public class screen extends Thread {
public JPanel cPanel=null;
public System_Behav system_behav=null;
//public byte by[];
public screen()
{
}
public screen(JPanel j,System_Behav b)
{
system_behav=b;
cPanel=j;
start();
}
public void run()
{
byte[] by=null;
while(true){
try {
Thread.sleep(100);
by=system_behav.getScreen();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedImage bi=null;
try {
bi=ImageIO.read(new ByteArrayInputStream(by));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageIcon imageIcon=new ImageIcon(bi);
Image image = imageIcon.getImage();
System.out.println(cPanel.getWidth());
System.out.println(cPanel.getHeight());
image = image.getScaledInstance(cPanel.getWidth(),cPanel.getHeight()
,Image.SCALE_FAST);
//Draw the recieved screenshot
Graphics graphics = cPanel.getGraphics();
graphics.drawImage(image, 0, 0, cPanel.getWidth(),cPanel.getHeight(),cPanel);
}
}
}
请帮助!!