Saturday, May 31, 2025

[JAVA] MAXIMAZE SIZE FORM

     import javax.swing.JFrame;
     import java.awt.Frame;

     public class MaximizeJFrame {
         public static void main(String[] args) {
             JFrame frame = new JFrame("My Frame");
             frame.setSize(400, 300); // Initial size
             frame.setVisible(true);

             // Maximize the frame
             frame.setExtendedState(Frame.MAXIMIZED_BOTH);
         }
     }

     import javax.swing.JFrame;
     import java.awt.Toolkit;
     import java.awt.Dimension;

     public class MaximizeJFrame {
         public static void main(String[] args) {
             JFrame frame = new JFrame("My Frame");
             frame.setVisible(true);

             // Get screen dimensions
             Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
             frame.setSize(screenSize); // Set frame size to screen size
         }
     }

[JAVA] Upload file URL

    import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class FileUploader {

    public static void main(String[] args) {
        String uploadUrl = "YOUR_UPLOAD_URL";
        File fileToUpload = new File("YOUR_FILE_PATH");

        try {
            URL url = new URL(uploadUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");

            try (OutputStream outputStream = connection.getOutputStream();
                 FileInputStream fileInputStream = new FileInputStream(fileToUpload)) {
                
                // Write the multipart form data boundary
                String boundary = "----WebKitFormBoundary7MA4YWxkTrZu0gW";
                outputStream.write(("--" + boundary + "\r\n").getBytes());
                outputStream.write(("Content-Disposition: form-data; name=\"file\"; filename=\"" + fileToUpload.getName() + "\"\r\n").getBytes());
                outputStream.write(("Content-Type: application/octet-stream\r\n\r\n").getBytes());

                // Write file content
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                // Write the closing boundary
                outputStream.write(("\r\n--" + boundary + "--\r\n").getBytes());
            }
            
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
                System.out.println("File uploaded successfully.");
            } else {
                System.out.println("File upload failed.");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

[JAVA] CAMERA

import com.github.sarxos.webcam.Webcam;
    import javax.imageio.ImageIO;
    import java.io.File;
    import java.io.IOException;

    public class CameraExample {
        public static void main(String[] args) throws IOException {
            Webcam webcam = Webcam.getDefault();
            webcam.open();
            ImageIO.write(webcam.getImage(), "PNG", new File("capture.png"));
            webcam.close();
        }
    }

[JAVA] VOLUME AUDIO

import javax.sound.sampled.*;

public class VolumeControl {
    public static void main(String[] args) {
        try {
            // Load an audio file
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(VolumeControl.class.getResource("audio.wav"));
            // Get a clip
            Clip clip = AudioSystem.getClip();
            // Open the clip
            clip.open(audioInputStream);
            // Get the master gain control
            FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);

            // Set the volume (example: 50% of the maximum)
            float maxGain = gainControl.getMaximum();
            float minGain = gainControl.getMinimum();
            float range = maxGain - minGain;
            float volume = minGain + (range * 0.5f); // 50%
            gainControl.setValue(volume);

            // Start playing
            clip.start();

            // Keep the program running while the audio plays
            Thread.sleep(clip.getMicrosecondLength() / 1000);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

[JAVA] PROVIDER

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

[JAVA] PROXY

   Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy_host_name", proxy_port_number));
    URL url = new URL("your_url");
    URLConnection urlConnection = url.openConnection(proxy);

[JAVA] READ HEX from File

import java.io.FileInputStream;
import java.io.IOException;

public class HexFileReader {

    public static byte[] readFileBytes(String filePath) throws IOException {
        FileInputStream fis = new FileInputStream(filePath);
        byte[] fileBytes = fis.readAllBytes();
        fis.close();
        return fileBytes;
    }

    public static String bytesToHex(byte[] bytes) {
      StringBuilder hexString = new StringBuilder();
      for (byte b : bytes) {
          hexString.append(String.format("%02x", b & 0xFF));
      }
      return hexString.toString();
    }

    public static void main(String[] args) {
        String filePath = "path/to/your/file.txt"; // Replace with your file path
        try {
            byte[] fileBytes = readFileBytes(filePath);
            String hexString = bytesToHex(fileBytes);
            System.out.println("Hexadecimal representation:\n" + hexString);
        } catch (IOException e) {
            System.err.println("Error reading file: " + e.getMessage());
        }
    }
}