Thursday, December 12, 2013

A Transparent screen to lock the Desktop.

In this code i am sharing the code to lock the PC screen by using a transparent JFrame so as to restrict others to use it.The frame disappears  only when the user enters the right username and password.This enables the user to let the background tasks going on and lock the screen.This has the only problem that it can be closed by the task manager.If you find a solution to it please let me know.

Code:

package com.oksbwn.systeminteraction;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.microedition.io.Connection;
import javax.microedition.io.Connector;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.oksbwn.popUp.popMe;
import com.oksbwn.voiceEnable.Voice;

public class Hibernate
{
   JFrame frame2=new JFrame();
//public static void main(String args[]) throws Exception
 public void doit() throws IOException
    {
 try {
        Connection connection = Connector.open( "btgoep://" +"04A82A5A62FB" + ":9");
           connection.close();
  //int x=1/0;//The condition which will trigger the frame to come up
       } 
    catch (Exception e) {
     //r.exec("shutdown -h");//s for shutdown r restart
 // Runtime rt = Runtime.getRuntime();
    // rt.exec("C:\\Windows\\System32\\rundll32.exe user32.dll,LockWorkStation");
     
     Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();// size of the screen
   frame2.setBounds(0,0,scrSize.width, scrSize.height);
     frame2.getContentPane().setLayout(null);
     frame2.setAlwaysOnTop(true);
     frame2.setUndecorated(true);
      frame2.setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),1));
      frame2.getContentPane().setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),50));
     ((JComponent)frame2.getContentPane()).setBorder(    
              BorderFactory.createMatteBorder( 2, 2, 2, 2, Color.black ) );
     frame2.setType(javax.swing.JFrame.Type.UTILITY);
     frame2.setAlwaysOnTop(true);
     
     final JTextField textField = new JTextField();
     textField.addKeyListener(new KeyAdapter() {
     @Override
     public void keyPressed(KeyEvent arg0) { 
       frame2.repaint();
     }
    });
     textField.setBounds(scrSize.width-150, scrSize.height-100, 140, 30);
     textField.setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),1));
     frame2.getContentPane().add(textField);
     textField.setColumns(10);
   
     final JPasswordField passwordField = new JPasswordField();
     passwordField.addKeyListener(new KeyAdapter() {
     @Override
     public void keyPressed(KeyEvent arg0) { 
       frame2.repaint();
     }
    });
     frame2.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
     passwordField.setBounds(scrSize.width-150, scrSize.height-65,140,30);
     passwordField.setBackground(new Color(Color.black.getRed(), Color.black.getGreen(),Color.black.getBlue(),1));
     frame2.getContentPane().add(passwordField);
     
    JButton btnLogIn = new JButton("login");
    btnLogIn.setBounds(scrSize.width-127,scrSize.height-30,77,24);
       frame2.getContentPane().add(btnLogIn);
        btnLogIn.addMouseListener(new MouseAdapter() {
     @Override
     public void mouseClicked(MouseEvent arg0) 
     {
      @SuppressWarnings("deprecation")
      String pass=passwordField.getText();
      logClicked(pass,textField.getText());
      passwordField.setText(null);
      textField.setText(null);
      //frame2.dispose();
     }
    });
     
        final JLabel Snap = new JLabel();
           Snap.setForeground(Color.RED);
           Snap.setBounds(scrSize.width/2-100, scrSize.height/2-100,200,200);
     frame2.getContentPane().add(Snap);
     
     frame2.addMouseListener(new MouseAdapter() {
       @Override
       public void mouseClicked(MouseEvent arg0) {
     final Runnable runnable = (Runnable) Toolkit.getDefaultToolkit().getDesktopProperty("win.sound.exclamation");
     //default 
      if (runnable != null)
        runnable.run();
      Snap.setText("OOOOps   You are Not Bikash");
         }
     });
     frame2.setVisible(true);
     }
    }
protected  void logClicked(String pass, String user) 
{
 String aPass="******";//Password
 String aUser="oksbwn";//Username
 if (aPass.compareTo(pass) == aUser.compareTo(user)==true)
    {
   //sucess message
  }
 else {
     //failure message
      }
 }
}

No comments:

Post a Comment