Öncelikle uzak bilgisayarda çalıştırmanız gereken programı derlemelisiniz. bu dosyayı Jar haline getirdikten sonra uzak bilgisayarda bir defa çalıştırmanız yeterlidir.
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.imageio.plugins.jpeg.JPEG;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.OutputStream;
import java.awt.Toolkit;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
public class resimgonder extends Thread implements Runnable {
private ServerSocket server=null;
public resimgonder() {
try{
server=new ServerSocket(1222);
}
catch(Exception ex){
ex.printStackTrace();
}
}
public void run(){
try{
Robot r=new Robot();
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension dim=tk.getScreenSize();
Rectangle rect=new Rectangle(dim);
while(true){
try{
Socket socket=server.accept();
OutputStream osw=socket.getOutputStream();
BufferedImage bi=r.createScreenCapture(rect);
JPEGEncodeParam enc=JPEGCodec.getDefaultJPEGEncodeParam(bi);
enc.setQuality(10,false);
JPEGImageEncoder ienc=JPEGCodec.createJPEGEncoder(osw);
ienc.encode(bi,enc);
osw.flush();
socket.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
catch(Exception es){
es.printStackTrace();
}
}
public static void main(String args[]){
try{
resimgonder r=new resimgonder();
r.start();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
şimdi sıra uzak bilgisayardaki görüntüyü kendi bilgisayarınıza taşımanıza geldi, programı çalıştırdığınız bilgisayarın IP'sini biliyorsanız aşağıdaki kodu çalıştırırken ilgili IP'yi yazarak görüntüyü alabilirsiniz.
Öncelikle bir resimkutusu sınıfı yaratalım
import java.awt.image.BufferedImage;
public class picturebox extends javax.swing.JPanel {
public javax.swing.ImageIcon bi=null;
public void setBi(javax.swing.ImageIcon bi){
this.bi=bi;
repaint();
}
public javax.swing.ImageIcon getBi(){
return this.bi;
}
public picturebox() {
}
@Override
public void paint(java.awt.Graphics g){
if(bi!=null){
g.drawImage(bi.getImage(),0,0,this.getWidth(),this.getHeight(),this);
}
else{
g.drawString("No Image",5,5);
}
}
}
daha sonra resimalma işlemini yapan sınıfı yazmallıyız
import java.net.Socket;
import java.io.InputStream;
import javax.swing.ImageIcon;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class resimal extends Thread implements Runnable {
private String host=null;
private picturebox picturebox1=null;
public resimal(String host,picturebox picturebox1) {
this.host=host;
this.picturebox1=picturebox1;
}
public synchronized void run(){
while(true){
try{
Socket socket=new Socket(host,1222);
InputStream isr=socket.getInputStream();
BufferedImage bi=ImageIO.read(isr);
ImageIcon ic=new ImageIcon(bi);
picturebox1.setBi(ic);
socket.close();
}
catch(Exception ex){
ex.printStackTrace();
}
}
}
}
şimdi sıra son uygulamamızda
import java.net.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class Form extends javax.swing.JFrame {
private javax.swing.JButton button;
private javax.swing.JTextField IP;
private picturebox picturebox1;
public Form() {
this.setLayout(null);
button = new javax.swing.JButton("GORUNTU IZLE");
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
resimCagir(evt);
}
});
IP = new javax.swing.JTextField();
picturebox1 = new picturebox();
picturebox1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
setDefaultCloseOperation(javax.swing********Constants.EXIT_ON_CLOSE);
button.setBounds(20,50,100,30);
IP.setBounds(130,50,200,30);
picturebox1.setBounds(20,50,640,480);
this.add(button);
this.add(IP);
this.add(picturebox1);
}
private void resimCagir(java.awt.event.ActionEvent evt) {
resimal r=new resimal(IP.getText(),picturebox1);
r.start();
}
public static void main(String args[]) {
Form f=new Form();
f.setSize(800,600);
f.setVisible(true);
}
}
![[Resim: 114ld.jpg]](http://b1112.hizliresim.com/s/c/114ld.jpg)