Postingan

Menampilkan postingan dari Maret, 2015

Display Image on Panel after transfered via Socket :: Java

import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.BufferedInputStream; import java.io.IOException; import java.net.Socket; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JPanel; /** * * @author ipopp */ public class imageClientPanel { public static BufferedImage image; public static JPanel panel; public static void main (String[] args) { JFrame frame = new JFrame("test"); frame.setSize(800,600); frame.add(panel = new JPanel() { @Override public void paintComponent(Graphics graphics) { super.paintComponents(graphics); if(image != null) { graphics.drawImage(image, 0, 0, this); } else { graphics.setColor(Color.black); graphics.fillRect(0,0,800,600); } } }); frame.setDefaultCloseOperation

Sending Image Through Socket with Java

SERVER import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.*; public class imageServer { public static void main(String[] args) throws IOException { File testF = new File("/home/ipopp/Public/smalltest/tes.jpg"); ServerSocket server = new ServerSocket(9090); try { System.out.println("reading file"); Socket socket = server.accept(); FileInputStream file = new FileInputStream(testF); BufferedInputStream ins = new BufferedInputStream(file); BufferedOutputStream outs = new BufferedOutputStream(socket.getOutputStream()); byte[] buffer = new byte[4096]; long bytesRead; while( (bytesRead = ins.read(buffer)) != -1) {