Advertisement
Zuhairy_Harry

Sender.java

Jun 11th, 2024
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5. import java.net.*;
  6. import javax.imageio.ImageIO;
  7.  
  8. public class Sender extends JFrame {
  9.  
  10.     private JTextField textFieldSend;
  11.     private JButton buttonSend;
  12.     private JButton getImageButton;
  13.     private JLabel imageLabel;
  14.  
  15.     public Sender() {
  16.         createGUI();
  17.         startServer();
  18.     }
  19.  
  20.     private void createGUI() {
  21.         setLayout(new AbsoluteLayout());
  22.  
  23.         textFieldSend = new JTextField();
  24.         textFieldSend.setBounds(10, 10, 200, 25);
  25.         add(textFieldSend);
  26.  
  27.         buttonSend = new JButton("Send");
  28.         buttonSend.setBounds(10, 40, 100, 25);
  29.         buttonSend.addActionListener(new ActionListener() {
  30.             @Override
  31.             public void actionPerformed(ActionEvent e) {
  32.                 String message = textFieldSend.getText();
  33.                 try {
  34.                     sendString(message); // Call the method to send the string
  35.                 } catch (IOException ex) {
  36.                     // Handle the exception
  37.                     ex.printStackTrace();
  38.                 }
  39.             }
  40.         });
  41.         add(buttonSend);
  42.  
  43.         getImageButton = new JButton("Get Image");
  44.         getImageButton.setBounds(10, 70, 100, 25);
  45.         getImageButton.addActionListener(new ActionListener() {
  46.             @Override
  47.             public void actionPerformed(ActionEvent e) {
  48.                 receiveImage(); // Call the method to receive the image
  49.             }
  50.         });
  51.         add(getImageButton);
  52.  
  53.         imageLabel = new JLabel();
  54.         imageLabel.setBounds(10, 100, 200, 150);
  55.         add(imageLabel);
  56.  
  57.         setSize(250, 300);
  58.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  59.         setVisible(true);
  60.     }
  61.  
  62.     private void sendString(String message) throws IOException {
  63.         Socket socket = new Socket("127.0.0.1", 8080); // Connect to the Receiver
  64.         OutputStream outputStream = socket.getOutputStream();
  65.         outputStream.write(message.getBytes()); // Send the string
  66.         outputStream.close();
  67.         socket.close();
  68.     }
  69.  
  70.     private void receiveImage() {
  71.         try {
  72.             ServerSocket serverSocket = new ServerSocket(8081);
  73.             Socket socket = serverSocket.accept();
  74.             InputStream inputStream = socket.getInputStream();
  75.             BufferedImage image = ImageIO.read(inputStream);
  76.             // Display the received image in a JLabel or any other suitable component
  77.             imageLabel.setIcon(new ImageIcon(image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement