Saturday, May 31, 2025

[JAVA] INHERITANCE

class Animal {
    String name;
    public void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    public void bark() {
        System.out.println("Dog is barking");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "Buddy";
        dog.eat(); // Inherited from Animal
        dog.bark(); // Specific to Dog
    }
}

[JAVA] PAINT EXAMPLE SIMPLE

import java.util.Arrays;

public class SortString {
    public static void main(String[] args) {
        String inputString = "javaexample";
        char[] charArray = inputString.toCharArray();

        Arrays.sort(charArray);

        String sortedString = new String(charArray);
        System.out.println("Original String: " + inputString);
        System.out.println("Sorted String: " + sortedString);
    }
}

[JAVA] PAUSE AUDIO

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

public class AudioPlayer {

    private Clip clip;
    private long clipTime;

    public void loadAudio(String filePath) throws LineUnavailableException, IOException, UnsupportedAudioFileException {
        File audioFile = new File(filePath);
        AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
        clip = AudioSystem.getClip();
        clip.open(audioStream);
    }

    public void play() {
        clip.start();
    }

    public void pause() {
        clipTime = clip.getMicrosecondPosition();
        clip.stop();
    }

    public void resume() {
        clip.setMicrosecondPosition(clipTime);
        clip.start();
    }

    public void stop() {
        clip.stop();
        clip.close();
    }

    public static void main(String[] args) {
        AudioPlayer player = new AudioPlayer();
        try {
            player.loadAudio("path/to/your/audio.wav"); // Replace with your audio file path
            player.play();

            // Simulate pausing after 2 seconds
            Thread.sleep(2000);
            player.pause();
            System.out.println("Audio paused");

            // Simulate resuming after 2 seconds
            Thread.sleep(2000);
            player.resume();
            System.out.println("Audio resumed");

            // Let the audio play for 5 seconds
            Thread.sleep(5000);
            player.stop();
            System.out.println("Audio stopped");

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

[JAVA] SORT by A to Z from String

import java.util.Arrays;

public class SortString {
    public static void main(String[] args) {
        String inputString = "javaexample";
        char[] charArray = inputString.toCharArray();

        Arrays.sort(charArray);

        String sortedString = new String(charArray);
        System.out.println("Original String: " + inputString);
        System.out.println("Sorted String: " + sortedString);
    }
}

[JAVA] JAVA AUTO DRAWING IMAGE FROM IMAGE

import javax.imageio.ImageIO;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class AutoDrawImage {

    public static void main(String[] args) {
        try {
            // Load the source image
            BufferedImage sourceImage = ImageIO.read(new File("path/to/your/source/image.jpg"));

            // Create a destination image
            BufferedImage destinationImage = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), BufferedImage.TYPE_INT_ARGB);

            // Get the graphics context
            Graphics2D g2d = destinationImage.createGraphics();

            // Draw the source image onto the destination
            g2d.drawImage(sourceImage, 0, 0, null);
            g2d.dispose();

            // Save the result
            ImageIO.write(destinationImage, "png", new File("path/to/your/output/image.png"));
            System.out.println("Image drawn and saved successfully!");

        } catch (IOException e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}

[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();
        }
    }
}