NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

NevLabs

Under Construction

Showing posts with label Applet. Show all posts
Showing posts with label Applet. Show all posts

Sunday, October 21, 2012

Java program to animate ball or moving ball using frame


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class movball extends Frame implements Runnable
{ int x,y;
  Thread t;
  boolean flag;
  public movball()
  { x=0;
    y=250;
    addWindowListener(new mwa(this));
    t=new Thread(this);
    t.start();
    flag=false;
  }
  public void run()
  { for(;;)
    { try
      { repaint();
        x++;
        Thread.sleep(100);
      }
      catch(InterruptedException e)
      {
      }
      if(flag)
        break;
    }
  }
  public void paint(Graphics g)
  { g.drawOval(x,y,30,30);
  }
  public static void main(String s[])
  { movball b=new movball();
    b.setSize(new Dimension(1000,600));
    b.setTitle("Running Ball");
    b.setVisible(true);
  }
}
class mwa extends WindowAdapter
{ movball a;
  public mwa(movball f)
  { a=f;
  }
  public void windowClosing(WindowEvent e)
  { a.flag=true;
    a.t=null;
    System.exit(0);
  }
}
Output

Java program to create student registration form


import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class student extends Frame implements ActionListener
{String msg;
  Button b1=new Button("save");
  Label l11=new Label("Student details",Label.CENTER);
  Label l1=new Label("Name:",Label.LEFT);
  Label l2=new Label("age:",Label.LEFT);
  Label l3=new Label("Sex(M/F):",Label.LEFT);
  Label l4=new Label("Address:",Label.LEFT);
  Label l5=new Label("Course:",Label.LEFT);
  Label l6=new Label("Semester:",Label.LEFT);
  Label l7=new Label("",Label.RIGHT);
  TextField t1=new TextField();
  Choice c1=new Choice();
  CheckboxGroup cbg=new CheckboxGroup();
  Checkbox ck1=new Checkbox("Male",false,cbg);
  Checkbox ck2=new Checkbox("Female",false,cbg);
  TextArea t2=new TextArea("",180,90,TextArea.SCROLLBARS_VERTICAL_ONLY);
  Choice course=new Choice();
  Choice sem=new Choice();
  Choice age=new Choice();
public student()
 {addWindowListener(new myWindowAdapter());
  setBackground(Color.cyan);
  setForeground(Color.black);
  setLayout(null);
  add(l11);
  add(l1);
  add(l2);
  add(l3);
  add(l4);
  add(l5);
  add(l6);
  add(l7);
  add(t1);
  add(t2);
  add(ck1);
  add(ck2);
  add(course);
  add(sem);
  add(age);
  add(b1);
  b1.addActionListener(this);
  add(b1);
  course.add("BSc c.s");
  course.add("BSc maths");
  course.add("BSc physics");
  course.add("BA English");
  course.add("BCOM");
  sem.add("1");
  sem.add("2");
  sem.add("3");
  sem.add("4");
  sem.add("5");
  sem.add("6");
  age.add("17");
  age.add("18");
  age.add("19");
  age.add("20");
  age.add("21");
  l1.setBounds(25,65,90,20);
  l2.setBounds(25,90,90,20);
  l3.setBounds(25,120,90,20);
  l4.setBounds(25,185,90,20);
  l5.setBounds(25,260,90,20);
  l6.setBounds(25,290,90,20);
  l7.setBounds(25,260,90,20);
  l11.setBounds(10,40,280,20);
  t1.setBounds(120,65,170,20);
  t2.setBounds(120,185,170,60);
  ck1.setBounds(120,120,50,20);
  ck2.setBounds(170,120,60,20);
  course.setBounds(120,260,100,20);
  sem.setBounds(120,290,50,20);
  age.setBounds(120,90,50,20);
  b1.setBounds(120,350,50,30);
}
public void paint(Graphics g)
{g.drawString(msg,200,450);}
public void actionPerformed(ActionEvent ae)
{if(ae.getActionCommand().equals("save"))
  {msg="Student details saved!";
   setForeground(Color.red); }
}
public static void main(String g[])
{student stu=new student();
 stu.setSize(new Dimension(500,500));
 stu.setTitle("student registration");
 stu.setVisible(true);
}
}
 class myWindowAdapter extends WindowAdapter
{public void windowClosing(WindowEvent we)
 {
  System.exit(0);
 }
}
Output

Java program to animated text or moving text


import java.applet.*;
import java.awt.*;
/*<applet code="runtext" width=450 height=400>
</applet>*/

public class runtext extends Applet implements Runnable
 {
   Thread t;
   int x,y;
  boolean stopflag;
            public void init()
             {
               setBackground(Color.black);
               setForeground(Color.white);
               x=120;y=130;
              
             }

            public void start()
              {
                stopflag=false;
                t=new Thread(this,"one");
                t.start();
                       
              }

            public void paint(Graphics g)
              {showStatus("name of college");
                g.drawString("SS College",x,y);
              }

            public void stop()
              {
                stopflag=true;
                t=null;
              }

            public void run()
              {
                for(; ;)
                        {
                          try
                            {
                                    y++;x++;
                                    repaint();
                                    Thread.sleep(50);
                                    if(stopflag) break;
                            }
                         catch(InterruptedException e)
                          {
                                    System.out.println(e);
                          }
                        }
             }
}
Output

Java program to illustrate menubar with the help of applet


import java.applet.*;
import java.awt.*;
 class mymenu extends Frame
{
            MenuBar mb;
            Menu filemenu,editmenu,viewmenu;
            MenuItem open,newmi,save,cut,copy;
            CheckboxMenuItem toolbar,layer;

public mymenu()
{
            mb=new MenuBar();
            setMenuBar(mb);
           
            filemenu=new Menu("File");
            editmenu=new Menu("Edit");
            viewmenu=new Menu("View");

            open=new MenuItem("Open");
            newmi=new MenuItem("New");
            save=new MenuItem("Save");
            cut=new MenuItem("Cut");
            copy=new MenuItem("Copy");

            toolbar=new CheckboxMenuItem("Toolbar",true);
            layer=new CheckboxMenuItem("Layer Window",false);

            filemenu.add(open);
            filemenu.add(newmi);
            filemenu.add(save);

            editmenu.add(cut);
            editmenu.add(copy);

            viewmenu.add(toolbar);
            viewmenu.add(layer);

            mb.add(filemenu);
            mb.add(editmenu);
            mb.add(viewmenu);
}

public static void main(String s[])
{
            mymenu mn=new mymenu();
             mn.setSize(new Dimension(500,500));
            mn.setTitle("Menu Bar");
            mn.setVisible(true);
}
}
Output

Java program to create login form using applet


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Loginform extends Frame implements ActionListener
{ Label l1=new Label("user name");
  Label l2=new Label("password");
  Label l3=new Label(" ");
  TextField t1=new TextField();
  TextField t2=new TextField();
  Button b= new Button("Submit");
  public Loginform()
  { add(l1);
    add(t1);
    add(l2);
    add(t2);
    add(b);
    add(l3);
    l1.setBounds(20,45,70,20);
    t1.setBounds(180,45,200,20);
    l2.setBounds(20,95,70,20);
    t2.setBounds(180,95,200,20);
    b.setBounds(310,145,70,20);
    b.addActionListener(this);
    t2.setEchoChar('*');
    addWindowListener(new mwa());
  }
  public void actionPerformed(ActionEvent e)
  { l3.setText("Welcome "+t1.getText());}
  public static void main(String s[])
  { Loginform l=new Loginform();
    l.setSize(new Dimension(600,600));
    l.setTitle("Login");
    l.setVisible(true);
  }
}
class mwa extends WindowAdapter
{ public mwa(){}
  public void windowClosing(WindowEvent e)
  { System.exit(0);
  }
}
Output

Java program to print fibonacci series upto a given number

import java.io.*;
class Fibonacci
{ int next,curr,prev,num;
            Fibonacci(int a)
  { next=1;
     curr=1;
    prev=0;
    num=a;  }
  void calc()
  { while(prev<=num)
    { System.out.println(prev);
      prev=curr;
      curr=next;
      next=prev+curr;  }
  }
  public static void main(String s[])throws IOException
  { BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter the limit");
    int a=Integer.parseInt(br.readLine());
    Fibonacci f=new Fibonacci(a);
    f.calc(); }
}
Output