Wednesday, June 25, 2025

[JAVA ANDROID] WIFI CONNECT NO USE KEY

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class WifiAutoConnect {

    public static void connectToOpenWifi(Context context, String ssid) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = String.format("\"%s\"", ssid);
        wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        
        int netId = wifiManager.addNetwork(wifiConfig);
        wifiManager.disconnect();
        wifiManager.enableNetwork(netId, true);
        wifiManager.reconnect();
    }
}

[JAVA] ANIMATION

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

class MovingObject {
    int x, y, width, height;
    Color color;

    public MovingObject(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }

    public void move(int dx, int dy) {
        x += dx;
        y += dy;
    }

    public void draw(Graphics g) {
        g.setColor(color);
        g.fillRect(x, y, width, height);
    }
}

public class AnimationDemo extends JPanel implements ActionListener {
    private List<MovingObject> objects;
    private Timer timer;

    public AnimationDemo() {
        objects = new ArrayList<>();
        objects.add(new MovingObject(50, 50, 50, 50, Color.RED));
        objects.add(new MovingObject(150, 100, 50, 50, Color.BLUE));
        objects.add(new MovingObject(250, 150, 50, 50, Color.GREEN));

        timer = new Timer(30, this);
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(0, 0, getWidth(), getHeight());

        for (MovingObject obj : objects) {
            obj.draw(g);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        for (MovingObject obj : objects) {
            obj.move(1, 1);
            if (obj.x > getWidth()) obj.x = 0;
            if (obj.y > getHeight()) obj.y = 0;
        }
        repaint();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("2D Animation");
        AnimationDemo animation = new AnimationDemo();
        frame.add(animation);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

Monday, June 23, 2025

[JAVA] NO RISK FROM WATER

import java.util.ArrayList;
import java.util.List;

class SafetyMeasure {
    private String type;
    private String description;

    public SafetyMeasure(String type, String description) {
        this.type = type;
        this.description = description;
    }

    public String getType() {
        return type;
    }

    public String getDescription() {
        return description;
    }
}

class SafetyChecklist {
    private List<SafetyMeasure> measures;

    public SafetyChecklist() {
        measures = new ArrayList<>();
    }

    public void addMeasure(SafetyMeasure measure) {
        measures.add(measure);
    }

    public void displayChecklist() {
        System.out.println("Safety Checklist:");
        for (SafetyMeasure measure : measures) {
            System.out.println(measure.getType() + ": " + measure.getDescription());
        }
    }
}

public class SafetyManager {
    public static void main(String[] args) {
        SafetyChecklist checklist = new SafetyChecklist();

        checklist.addMeasure(new SafetyMeasure("Fire Alarm", "Install smoke detectors in all rooms."));
        checklist.addMeasure(new SafetyMeasure("Fire Extinguisher", "Place fire extinguishers in key locations."));
        checklist.addMeasure(new SafetyMeasure("Water Leak Detector", "Install water leak detectors in basements and kitchens."));
        checklist.addMeasure(new SafetyMeasure("Drainage System", "Ensure proper drainage to prevent flooding."));
        checklist.addMeasure(new SafetyMeasure("Fire Escape Plan", "Create and practice a fire escape plan with all occupants."));

        checklist.displayChecklist();
    }
}

[JAVA] HARDWARE COOL

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Timer;
import java.util.TimerTask;
public class HardwareCooling {
    public static void main(String[] args) {
        try {
            while (true) {
                double temperature = getCPUTemperature();
                if (temperature > 75.0) {
                    activateCoolingSystem();
                }
                Thread.sleep(5000); // Check every 5 seconds
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static double getCPUTemperature() throws Exception {
        Process process = Runtime.getRuntime().exec("sensors");
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        double temperature = 0.0;
        while ((line = reader.readLine()) != null) {
            if (line.contains("Core 0")) {
                String[] parts = line.split("\\s+");
                temperature = Double.parseDouble(parts[parts.length - 1].replace("°C", ""));
                break;
            }
        }
        return temperature;
    }

    private static void activateCoolingSystem() {
        System.out.println("Activating cooling system...");
        // Code to activate the cooling system


public class CoolingSystem {
    private boolean isCoolingActive;

    public CoolingSystem() {
        this.isCoolingActive = false;
    }

    public void activateCooling() {
        if (!isCoolingActive) {
            isCoolingActive = true;
            System.out.println("Cooling system activated.");
            // Additional logic to start the cooling system
        } else {
            System.out.println("Cooling system is already active.");
        }
    }

    public void deactivateCooling() {
        if (isCoolingActive) {
            isCoolingActive = false;
            System.out.println("Cooling system deactivated.");
            // Additional logic to stop the cooling system
        } else {
            System.out.println("Cooling system is already inactive.");
        }
    }

    public static void main(String[] args) {
        CoolingSystem coolingSystem = new CoolingSystem();
        coolingSystem.activateCooling();

        // Example of deactivating after a certain period
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                coolingSystem.deactivateCooling();
            }
        }, 5000); // Deactivate after 5 seconds
    }
}

    }
}

[JAVA] CHATTING SERVER CLIENT

import java.io.*;
import java.net.*;

public class ChatServer {
    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(12345)) {
            System.out.println("Server is listening on port 12345");
            while (true) {
                new ClientHandler(serverSocket.accept()).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class ClientHandler extends Thread {
    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;

    public ClientHandler(Socket socket) {
        this.socket = socket;
    }

    public void run() {
        try {
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            String message;
            while ((message = in.readLine()) != null) {
                System.out.println("Received: " + message);
                out.println("Echo: " + message);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public class ChatClient {
    public static void main(String[] args) {
        try (Socket socket = new Socket("localhost", 12345);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in))) {
             
            String userInput;
            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);
                System.out.println("Server response: " + in.readLine());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

[JAVA] JAVA DRAG

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

public class DragPanel extends JPanel implements MouseListener, MouseMotionListener {

    private List<DraggableShape> shapes = new ArrayList<>();
    private DraggableShape selectedShape = null;
    private double offsetX, offsetY;

    public DragPanel() {
        // Add a sample shape
        shapes.add(new DraggableShape(new Rectangle2D.Double(50, 50, 100, 75), Color.BLUE));

        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        for (DraggableShape shape : shapes) {
            g2.setColor(shape.getColor());
            g2.draw(shape.getShape());
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        double mx = e.getX();
        double my = e.getY();

        for (DraggableShape shape : shapes) {
            if (shape.getShape().contains(mx, my)) {
                selectedShape = shape;
                offsetX = mx - shape.getShape().getX();
                offsetY = my - shape.getShape().getY();
                break;
            }
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (selectedShape != null) {
            double newX = e.getX() - offsetX;
            double newY = e.getY() - offsetY;
            Rectangle2D currentShape = (Rectangle2D) selectedShape.getShape();
            currentShape.setFrame(newX, newY, currentShape.getWidth(), currentShape.getHeight());
            repaint();
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        selectedShape = null;
    }

    // Unused MouseListener/MouseMotionListener methods
    @Override public void mouseClicked(MouseEvent e) {}
    @Override public void mouseEntered(MouseEvent e) {}
    @Override public void mouseExited(MouseEvent e) {}
    @Override public void mouseMoved(MouseEvent e) {}

    // Helper class for draggable shapes
    private static class DraggableShape {
        private Shape shape;
        private Color color;

        public DraggableShape(Shape shape, Color color) {
            this.shape = shape;
            this.color = color;
        }

        public Shape getShape() { return shape; }
        public Color getColor() { return color; }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("Drag Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new DragPanel());
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

[JAVA] SERVER CLIENT UDP

import java.net.*;

public class UDPServer {
    public static void main(String[] args) throws Exception {
        DatagramSocket socket = new DatagramSocket(54321); // Listen on port 54321
        System.out.println("UDP Server listening on port 54321...");

        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

        socket.receive(packet); // Receive a UDP packet
        String receivedMessage = new String(packet.getData(), 0, packet.getLength());
        System.out.println("Received from client: " + receivedMessage);

        // Optional: Send a response
        InetAddress clientAddress = packet.getAddress();
        int clientPort = packet.getPort();
        String response = "UDP response from server!";
        byte[] responseData = response.getBytes();
        DatagramPacket responsePacket = new DatagramPacket(responseData, responseData.length, clientAddress, clientPort);
        socket.send(responsePacket);

        socket.close();
    }
}