Search This Blog

Thursday, June 17, 2021

Motion Detector in Java (Using Sarxos.Webcam)

 Motion Detector in Java



This link can be used to download the library used to make this project:


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

import com.github.sarxos.webcam.Webcam;
import com.github.sarxos.webcam.WebcamMotionDetector;
import com.github.sarxos.webcam.WebcamMotionEvent;
import com.github.sarxos.webcam.WebcamMotionListener;


public class DetectMotion implements WebcamMotionListener {

public DetectMotion() {

WebcamMotionDetector detector = new WebcamMotionDetector(Webcam.getDefault());
detector.setInterval(200); // one check per 500 ms
detector.addMotionListener(this);
detector.start();
}

@Override
public void motionDetected(WebcamMotionEvent wme) {
System.out.print("beep ");
}

public static void main(String[] args) {




//Detector
new DetectMotion();
System.in.read(); // keep program open
}
}


Every time a motion is detected by the webcam of your PC 'beep' will be printed.

Use the famous book 'Head First Java' to learn Java in the best way possible, Download the free e-book now.

You can also follow this tutorial by Genuine Coder Youtube channel to build basic programs using this library.


Friday, June 4, 2021

Basic Calculator Using 'GUI' in java

Calculator Using GUI



********************************

 package com.company;


import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;


public class GUI_calculator implements ActionListener {

    float first=0;

    float second=0;

    float third=0;

   private JFrame frame;

   private JPanel panel;

    private JButton b;

   private JButton b1;

   private JButton b2;

   private JButton b3;

   private JButton b4;

    private JButton b5;

    private JButton b6;

    private JButton b7;

   private JLabel label;

    private JLabel label1;

    private JLabel label2;

    public GUI_calculator(){

        frame=new JFrame();

        label=new JLabel("0");

        b=new JButton("++");

        b1=new JButton("+");

        b2=new JButton("-");

        b3=new JButton("*");

        b4=new JButton("/");

        label1=new JLabel("0");

        b5=new JButton("++");

        b6=new JButton("=");

        b7=new JButton("C");

        label2=new JLabel("0");

        b1.addActionListener(this);

        b2.addActionListener(this);

        b3.addActionListener(this);

        b4.addActionListener(this);

        b.addActionListener(this);

        b5.addActionListener(this);

        b6.addActionListener(this);

        b7.addActionListener(this);


        //Panel Content

        panel=new JPanel();

        panel.setBorder(BorderFactory.createEmptyBorder(30,30,10,30));


        panel.add(label);

        panel.add(b);

        panel.add(label1);

        panel.add(b5);

        panel.add(b7);

        panel.add(b1);

        panel.add(b2);

        panel.add(b3);

        panel.add(b4);

        panel.add(b6);

        panel.add(label2);



        panel.setLayout(new GridLayout(0,1));

        frame.add(panel,BorderLayout.CENTER);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setTitle("Calculator");

        frame.pack();

        frame.setVisible(true);

    }

    public static void main(String[] args) {

        new GUI_calculator();

    }


    @Override

    public void actionPerformed(ActionEvent e) {

if (e.getSource()==b){

    first++;

    label.setText(""+first);

}

else { }

if (e.getSource()==b5){

    second++;

    label1.setText(""+second);

}

else { }

if (e.getSource()==b1){

    third=first+second;

}

else if (e.getSource()==b2){

    third=first-second;

}

else if (e.getSource()==b3){

    third=first*second;

}

else if (e.getSource()==b4){

    third=first/second;

}

else if (e.getSource()==b7){

    first=0;

    second=0;

    label1.setText(""+second);

    label.setText(""+first);

}

if (e.getSource()==b6){

    label2.setText(""+third);

}

    }

}


Wednesday, June 2, 2021

Library Management System in Java

 Library Management System

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

class Library{
    Scanner sc=new Scanner(System.in);
    List<String> Books=new ArrayList<String>();
    List<String> IssuedBooks=new ArrayList<String>();

    public void addBook(){
        System.out.print("Enter the name of the book to be added: ");
        Books.add(sc.nextLine());
    }
    
    public void showAvailableBooks(){
        if (Books.size()==0){
            System.out.println("No books in the library");
        }
        else {
            System.out.println("Here is a list of books available in our library: ");
            for (int a = 1; a <= Books.size(); a++) {
                System.out.println(a + " " + Books.get(a - 1));
            }
        }
    }

    public void issueBook(){
        System.out.print("Enter the serial no. of the book you want to issue: ");
        int b=sc.nextInt();
        IssuedBooks.add(Books.get(b-1));
        System.out.println("You have issued: "+Books.get(b-1));
        Books.remove(b-1);
    }

    public void showIssuedBooks(){
        if (IssuedBooks.size()==0){
            System.out.println("No books issued!");
        }
        else {
            System.out.println("Here is a list of books issued: ");
            for (int a=1;a<=IssuedBooks.size();a++){
                System.out.println(a+" "+IssuedBooks.get(a-1));
            }
        }

    }

    public void returnBook(){
        System.out.print("Enter the serial no. of the book to be returned from the issued books: ");
        int b= sc.nextInt();
        Books.add(IssuedBooks.get(b-1));
        System.out.println("You have returned: "+IssuedBooks.get(b-1));
        IssuedBooks.remove(b-1);
    }

}
public class online_library {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        //Implement a library using java class library
        //Method: addBook, issueBook, returnBook, showAvailableBooks,showIssuedBooks,
        //Properties: Array to store the available books, Array to store the issued books
        Library jnrm=new Library();
        int a=0;
        System.out.println("Add book to library->add\nShow available books->showl\nIssue book->issue\nShow issued books->showi\nReturn book->return");
    while (a==0){

        String b= sc.next();
        if (b.equals("add")){
            jnrm.addBook();
        }
        else if (b.equals("showl")){
            jnrm.showAvailableBooks();
        }
        else if (b.equals("issue")){
            jnrm.issueBook();
        }
        else if (b.equals("showi")){
            jnrm.showIssuedBooks();
        }
        else if (b.equals("return")){
            jnrm.returnBook();
        }
        else {
            System.out.println("Something went wrong\nCheck the syntax!!!");
        }
    }
    }
}


Trending Articles