Friday, October 29, 2010

What is Multithreading in java examples

Multithreading,as the name itself tells that it is regarding,multi tasks(thread).In short Multithreading is a specialized form of multi tasking.
what is a thread?
Threads are separate parts of execution which are functionally independent of each other.

What is multitasking?
Single user multi process processed by a single processor.
Now what is a thread in java ?
In simple terms, a thread is a program's path of execution.Programs written without multithreading  run as a single thread, causing problems when multiple events or actions need to occur at the same time.
For instance, a program is not capable of drawing pictures while reading keystrokes. The program must give its full attention to the keyboard input lacking the ability to handle more than one event at a time. The ideal solution to this problem is the seamless execution of two or more sections of a program at the same time. Threads allows us to do this. 
How to create a thread in java ?

Monday, October 25, 2010

Java applet tag parameters turtorials for beginners

 Pram tag in applet is used to pass parameter from applet tag to program..(See basic applet)
import java.awt.*;                                //importing awt,applet packages
import java.applet.*;
/*
<applet code="AppleParam" width="200" height="300">          
<param name=fontname value=Applet>                                       //passing parameter
<param name=fontsize value=>
</applet>
*/

Saturday, October 23, 2010

How to make a java applet banner

As discussed Applet is small application accessed over internet.
Applet is a class in java,using extends keyword. (see how to extend Applet)
Applets can used to develop banner on your web page.
Lets see it with an example....
//Java Applet banner program
import java.awt.*;                                      //importing applet and awt package
import java.applet.*;
/*
<applet code="AppletBanner" width=300 height=50>
</applet>
*/
public class AppletBanner extends Applet implements Runnable{  //Multithreading is used

Thursday, October 21, 2010

Introducing Java applet tutorial with examples

What is Applet?
Applet is a small application that are accessed on an internet server,transported over the internet,automatically installed and run as part of a web document.
Security by applet:
After an applet arrives on the client,it has limited access to resources so thatit can produce a GUI and run complex computation without introducing the risk of viruses or breaching data integrity.
Starting Applet
There are two varieties of Applets.One that uses AWT to provide GUI,and second that uses SWINGS for GUI.
   All applets are subclasses of Applet.Applet is not standalone it runs on web browser or on Appletviewer.  (See ROBOT Class in java)
appletviewer is provided by JDK.

java program to move mouse

A sample application which makes your mouse to move automatically randomly on screen with high speed.

Robot Mouse code:

//Program starts here 
import java.awt.Robot;
public class MouseControl
{
public static void main(String[] hj)
{
   try
    {
       //to move mouse with more speed use this for loop

how to use robot class in java

/*Here is another Robot java code for you,with explanation....
 this program when you run it writes something for you...mean this program will communicate with you!
  Just kidding.
About Robot:
Robot is a class in Java, where it controls mouse, keyboard with the help of its methods.
Lets start with example...*/
 //Program starts here

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class RobotExp {

 public static void main(String[] args){

//code to open a notepad
  try{

 ProcessBuilder proc=new ProcessBuilder("notepad.exe","AM-ROBOT");
  proc.start();
   }
  catch(Exception k){
  System.out.println(k);
  }

//code to write message
 try {
  Robot robot = new Robot();
  robot.delay(5000); 
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_I);  
  robot.keyPress(KeyEvent.VK_A);
  robot.keyPress(KeyEvent.VK_M);
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_R);
  robot.keyPress(KeyEvent.VK_O);
  robot.keyPress(KeyEvent.VK_B);
  robot.keyPress(KeyEvent.VK_O);
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_T);
  robot.keyPress(KeyEvent.VK_A);
  robot.keyPress(KeyEvent.VK_L);
  robot.keyPress(KeyEvent.VK_K);
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_T); 
  robot.keyPress(KeyEvent.VK_O);
  robot.keyPress(KeyEvent.VK_SPACE);
  robot.keyPress(KeyEvent.VK_M);
  robot.keyPress(KeyEvent.VK_E);
  } 
  catch (AWTException e) {
                 e.printStackTrace();
   }
  }
}
//program completed 


Now compile and run the program.See how compile java in Eclipse  


For better result Just click yes when a notepad opens..

Explanation:


 It is very simple all you have to know is Robot class and its methods.
here in    robot.keyPress(KeyEvent.VK_O);
robot is object created in above program,KeyPress() is a method,and keyEvent is parameter.
VK is virtual key and O is character to be displayed.
you can write any message.With SPACE for gap.


NOTE:

      USE ONLY Single character and Capital,Or else you will get compile time error as compiler  will not understand it.

SEE ROBOT MOUSE which moves mouse pointer

Tags----      Packages in Java     ,       Access specifier      ,     Java history

Sunday, October 17, 2010

How to import packages

Importing packages

,simply include  import command in the program you want to import, that`s all you can use all those features in the package.
Not clear!
Lets see an example it will be cleared..(am giving very simple programs as example so that it will be cleared)
example:
 import Mypack1.*;                 //see from where this Mypack1 came.
class Package extends Pack         
//here Pack is name of class I used  in Mypack1
{       
public static void main(String s[])

{
  Pack p=new Pack();     //here creating object for that class
  int k=90;
  p.show();                          //access it through dot operator
 }
}

What are Access specifiers in java with examples

Here is a table showing the effects of

access specifiers

for class members. “
Y---yes
blank---no

Specifierclasssubclasspackageworld
privateY


protectedYYY
publicYYYY
(none)Y
Y

If a variable is declared protected, then the class itself can access it, its subclass can access it, and any class in the same package can also access it, but otherwise a class cannot access it.
If a class memeber doesn't have any access specifier (the “none” row in above), its access level is sometimes known as “package”.
Here's a example.

Java package tutorial for beginners

Packages in Java

,are containers for classes that are used to keep the class name space compartmentalized.For example,a package allows you to create a class named Xyz (any name given by you),which you can store in your own package with out any concern that it will collide with other packages.

Define Package:
                              Package is, a collection of .class files (know what is .class file),created by the user or predefined.

How to create a package:
                                      It is very simple, simply include package command as the first statement in a Java source file.Any classes declared in that file will belong to the specified package.
  

Friday, October 15, 2010

How to start with java programming

A simple program on Java

,that displays a  message.
                                                                             
class FirstProgram
{
     public static void main(String args[])    //main method
     {
          System.out.println(“Welcome to Java 4r beginners”); /* will be printed on output*/
          System.out.println(“This Simple Java program”)
      }
}
 
/*



now save this program with “FirstProgram.java” as class name is
FirstProgram.
-Program should be saved with its class name.

About java language

Java

is OOPS language, it supports OOPS features… go2 first page
Encapsulation is the mechanism that binds the code and the data it manipulates, keeps safe from both out side inheritance and misuse.
  • Inheritance
Inheritance is the process by which one object`s properties are acquired and used by other object. In short code re-usability. It is achieved by extends keyword in Java.
  • Polymorphism
In short Polymorphism mean many forms, one interface will be used for many tasks.This will be explained detailed with example.
…These are few among them..

Java program starts with class and main method will be included in the class.
Unlike C,C++ in java main method will pass String.Lets go to an example.

Thursday, October 14, 2010

Know what is history of java

Java

, as all know it`s a programming language developed by James Gosling and his team.
Java is still a relatively new language, so it is quite amusing to some to think that Java has a "history" behind it. One of the most frequent questions I've been getting lately though is about the origins of Java.

The first publicly available version of Java, however, was as Java applets, in the original HotJava browser. From there, Java grew to what it is today.