Latest Products

How to create RMI Program in Java

import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Computeapp extends Remote
{
Object executeTask(int i)throws RemoteException;
}


import java.rmi.*;
import java.rmi.server.*;
public class ComputeappEngine extends UnicastRemoteObject implements Computeapp
{
    public ComputeappEngine()throws RemoteException
    {
        super();
    }
    public Object executeTask(int i)throws RemoteException
    {
        return i * i;
    }

   
}


import java.rmi.*;

public class Client{

    public static void main(String args[]){
        try{
            Computeapp comp=(Computeapp)Naming.lookup("//localhost/Computeapp");
            System.out.println(comp.executeTask(4));
       
        }
        catch(Exception e){
        System.err.println("computeappEngine Exception"+e.getMessage());
            e.printStackTrace();   
        }
    }
}


import java.rmi.*;
import java.rmi.server.*;
class Server1 extends UnicastRemoteObject{

public Server1()throws RemoteException
    {
       
    }
    public static void main(String args[])throws RemoteException
    {
       

        String names = "//localhost/Computeapp";
        try{

            Computeapp engine = new ComputeappEngine();
            Naming.rebind(names, engine);
            System.out.println("computeappEngine bound");
        }
        catch(Exception e){
            System.err.println("computeappEngine Exception"+e.getMessage());
            e.printStackTrace();
        }
    }
}


Reading a file using Java

import java.io.*;
import java.lang.String;
class FileInputStreamDemo{
public static void main(String args[])throws IOException{
int size;

InputStream f=new FileInputStream("FileInputStreamDemo.java");
System.out.println("Total available byte"+(size=f.available()));
int n=size/40;
OutputStream f0=new FileOutputStream("File1.txt");
System.out.println("first"+n+"byte of file read");
for(int i=0;i<n;i++){
System.out.println((String source[i]=(char)f.read()));

byte buf[i]=source.getBytes();

f0.write(buf);
}
f.close();
f0.close();

}
}

How to load an image in applet

import java.applet.*;
import java.awt.*;
/*<applet code="DrawImage1" width=280 height=280>
<param name="file1" value="Blue hills.jpg">

</applet>
*/

public class DrawImage1 extends Applet {
    Image[] image=new Image[1];
    public void init(){
        image[0]=getImage(getDocumentBase(),getParameter("file1"));
       
    }
    public void paint(Graphics g){

        g.drawImage(image[0],0,0,this);
    }
       
}

How to handle with Jtextfield,JButton,JRadioButton,JList and MouseEvents


 import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

class Assignment extends JFrame implements ItemListener,ActionListener
{
 JButton b1;
 JButton b2;
 JButton b3;
 JButton b4;
 JButton b5;
 JButton b6;
 Object o;
 CheckboxGroup ch;
 Checkbox c1;
 Checkbox c2;
 JTextField jt;
 Label lb;
 JList jl1;
 JList jl2;
 JScrollPane js1;
 JScrollPane js2;
 DefaultListModel listModel1;
 DefaultListModel listModel2;
 public Assignment()
 {
  Container c = getContentPane();
  listModel1 = new DefaultListModel();
  listModel2 = new DefaultListModel();
  c.setLayout(null);
  b1 = new JButton(">");
  b2 = new JButton(">>");
  b3 = new JButton("<");
  b4 = new JButton("<<");
  b5 = new JButton("clear");
  b6 = new JButton("clear");
  ch = new CheckboxGroup();
  c1 = new Checkbox("Indian",ch,false);
  c2 = new Checkbox("Non Indian",ch,false);
  lb = new Label("Name");
  jt = new JTextField("Your name here",30);
  jl1 = new JList(listModel1);
  jl2 = new JList(listModel2);
  js1 = new JScrollPane(jl1);
  js2 = new JScrollPane(jl2);
  c.add(b1);
  c.add(b2); 
  c.add(b3);
  c.add(b4);
  c.add(b5);
  c.add(b6); 
  c.add(c1);
  c.add(c2);
  c.add(lb);
  c.add(jt);
  c.add(jl1);
  c.add(jl2);
  c.add(js1);
  c.add(js2);
  c1.addItemListener(this);
  c2.addItemListener(this);
  b1.addActionListener(this);
  b2.addActionListener(this);
  b3.addActionListener(this);
  b4.addActionListener(this);
  b5.addActionListener(this);
  b6.addActionListener(this);
  lb.setBounds(250,50,50,20);
  jt.setBounds(300,50,200,20);
  c1.setBounds(300,100,70,40);
  c2.setBounds(400,100,80,40);
  jl1.setBounds(100,200,150,200);
  jl2.setBounds(550,200,150,200);
  b1.setBounds(350,200,100,20);
  b2.setBounds(350,250,100,20);
  b3.setBounds(350,300,100,20);
  b4.setBounds(350,350,100,20);
  b5.setBounds(130,450,100,30);
  b6.setBounds(580,450,100,30);
 }
 public static void main(String s[])
 {
  Assignment a = new Assignment();
  a.setTitle("Assignment");
  a.setBounds(100,100,800,800);
  a.show();
 }
 /*class CheckboxHandler implements ItemListener
 {*/
  public void itemStateChanged(ItemEvent e)
  {
   if(e.getSource()==c1)
   {
    String s;
    s=jt.getText();
    listModel1.addElement(s);
   }
   if(e.getSource()==c2)
   {
    String s1;
    s1=jt.getText();
    listModel2.addElement(s1);
   }
  }
  public void actionPerformed(ActionEvent e)
  {  
   if(e.getSource()==b1)
   {
  
    listModel2.addElement(jl1.getSelectedValue());
    listModel1.removeElement(jl1.getSelectedValue());
   }
   if(e.getSource()==b2)
   {
    String s1;
       int t = jl1.getLastVisibleIndex();
     
       for (int i = 0; i <= t; i++)
       {
           o = jl1.getModel().getElementAt(i);
           s1 = o.toString();

           listModel2.addElement(s1);
       }
      listModel1.clear();
    
   }
   if(e.getSource()==b4)
   {
    String s1;
    int t = jl2.getLastVisibleIndex();
    for(int i=0;i<=t;i++)
    {
     o=jl2.getModel().getElementAt(i);
     s1=o.toString();
     listModel1.addElement(s1);
    }
   listModel2.clear();
   }
   if(e.getSource()==b3)
   {
    listModel1.addElement(jl2.getSelectedValue());
    listModel2.removeElement(jl2.getSelectedValue());
   }
   if(e.getSource()==b5)
    listModel1.clear();
   if(e.getSource()==b6)
    listModel2.clear();
  }
  //}
 //}
  
}

Inheritance in java



Deriving the base class property to child class. In java we using the keyword extends.

Syntax :
<modifier> class  baseclassname extends childclassname

Example

class First
{
 int a;
 First()
 {
  a=5;
  System.out.println("From class First's constructor ...");
 }
 void hai()
 {
  System.out.println("From class First , a = " + a);
 }
}

class Inheritance  extends First
{
 Inheritance()
 {
  super(); //calling the base class constructor.
 }  
 public static void main(String args[])
 {
  int a=5; 
  First f = new First();
  f.hai();
  System.out.println("From class Inheritance , a = " + 5);
 }
}
 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. TECHBEAN stars - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger