Tuesday, May 8, 2012

String Reverse Example in Java Programming

It is a sample string reverse example. In this program read array of string from user and give output array of reverse string.
Here use StringBuffer class and create object name[] of StringBuffer class. Use method reverse() to reverse inserted string.

Program Code

//developed by Om Prakash
import java.util.*;
class StringReverse
{
public static void main(String argv[])
{
int size;
Scanner console=new Scanner(System.in);
System.out.println("*******STRING REVERSE*******");
System.out.print("How many string do you want to enter : ");
size=console.nextInt();
StringBuffer name[]=new StringBuffer[size+1];
String temp;
int i;
System.out.println("Enter "+size+" string : ");
for(i=0;i<=size;i++)
{
temp=console.nextLine();
name[i]=new StringBuffer(temp);
}
System.out.println("String after Reverse");
for(i=0;i<=size;i++)
{
name[i].reverse();
System.out.println(name[i]);
}
}
}

Output





Monday, May 7, 2012

Java AWT vs SWING

Difference between Java AWT and SWING
1. AWT(Abstract Window Toolkit) is a heavy weight but Swing is a light weight.

2. Swing feel and look better than AWT components.

3. Swing is a pure java components but AWT native components.

4. AWT not have complex components but Swing has additional components like JTable, JProgressBar, JSlider, JTree etc.

5. Applet list has scrollbar but Swing JList does not support scrolling but this can be done using scrollPane.

6. Applet not support MDI(multiple document interface) window but Swing support MDI.

7. Applet default layout is FlowLayout but Swing default layout is BorderLayout.

8. AWT menu item cannot have images or radio buttons or checkboxes but Swing menu item can have images or radio buttons or checkboxes.

9. AWT do not have JMV(Java Model Viewport) but All swing components have JMV.

10. In AWT, components can be added directly on the Frame or window but in Swing while adding components on Frame or window, they have to be added on its content pane.

11. Swing has more powerful and flexible components than AWT.


Applets vs Applications - How Applets Differ From Applications


Java Applets and Java applications have many similarities but there are some differences between applets and applications.
1. Applets not use main() method but applications have main() method.

2. Applets execute in web browser or it embedded with web browser but java applications execute normally.

3. Applets are small programs to do specific task but applications are large programs.

4. Applets must extends java.applet.Applet class but java applications start execution with main() method.

5. Applets can not use libraries from other languages such as C or C++ but applications supports this by native methods.

6. Applet can not run independently. It run in webpage but application runs independently.

7. Applets not able to read or write files from local computer but applications can do such task.

8. Applets can not communicate with other servers on the network.

9. Applets have a life cycle with init(), start(), stop(), paint(), destroy() methods but applications not have such methods.



Saturday, April 21, 2012

Java Applet Animation Program for Moving Car

Hello friends, this is a Java applet program of moving car. In this java program draw a car. Car moves left to right direction on the screen continuously. Here use concepts of applet life cycle, multithreading.

Program Code: Applet_Animation.java
//Java Applet Animation Program  
//Developed by: Om Prakash Vishnoi
import java.awt.*; //import package
import java.applet.*; // import package
/*
<applet code=Applet_Animation width=1000 height =500>
</applet>
*/
public class Applet_Animation extends Applet implements Runnable
{
 int col,col1, row;
 int angle;
 Thread th;
 int x,y;
 public void init()   //initilize applet cycle
 {
  col=50;
  col1=col+220;
  row=400;
  angle=0;
  repaint();
 }
 public void start()   //start applet
 {
  th=new Thread(this);
  th.start();
 }
 public void run()  //run applet
 {
  for(int i=0;;i++)
  {
  try
  {

   Thread.sleep(100);
   repaint();
  }
  catch(InterruptedException e){}
  }
 }
 public void paint(Graphics g)  //draw on screen
 {

  Color c1=new Color(255,255,255);
  Color black = new Color(0,0,0);
  Color c2=new Color(255,0,0);
  Color c3=new Color(0,255,0);
  Color c4=new Color(0,0,255);
  Color c5=new Color(150,150,150);
  Color c6=new Color(190,160,160);
  g.setColor(c2);
  g.drawString("Developed By: Om Prakash Vishnoi",100,100);
  g.setColor(c1);  
  g.setColor(black);
//Car Designing  
  g.drawLine(col+47, row-4, col1-10, row-4); //connection line of wheel cover
  g.drawLine(col-47, row-4, col-10, row-4); //back wheel cover to back line- engine down line
  g.drawLine(col1+10, row-4, col1+80, row-4); //front wheel cover to front line
  g.drawLine(col-47, row-4, col-47, row-60); //back panel line
  //silencer
  g.drawLine(col-47, row-7,col-57,row-7 );
  g.drawLine(col-47, row-15,col-57,row-15 );  
  g.drawArc(col-58, row-15, 4, 8, 0, 360);

  g.drawLine(col1+80, row-4, col1+80, row-60); //engine front line
  g.drawLine(col-47, row-60, col, row-60); //back panel line
  g.drawLine(col1+20, row-60, col1+80, row-60); //engine up line
  g.drawArc(col, row-130, 240,140,15,165);  //uper arc
  g.drawLine(col1+18,321, col1+18,340);
//red light
  if(col%5==0)
   g.setColor(c2);
  else
   g.setColor(c1);

  g.fillArc(col+150, row-145, 15,40, 0,180); 
  g.setColor(black);
  g.drawArc(col+40, row-100, 50, 80, 0, 180); //back mirror
  g.drawLine(col+40, row-60, col+90, row-60);  //back mirror down line
  g.drawArc(col1-70, row-100, 50,80,0,180); //front mirror
  g.drawLine(col1-70, row-60,col1-20, row-60); //front mirror back line
  
//back wheel cover
  g.fillArc(col-9, row-28, 58,50, 0, 180);
  g.setColor(c1);
  g.fillArc(col-5, row-25, 50,50, 0, 180);
  
//front wheel cover
  g.setColor(black);
  g.fillArc(col1-9, row-28, 58,50, 0, 180);
  g.setColor(c1);
  g.fillArc(col1-5, row-25, 50,50, 0, 180);
 
//back wheel
  g.setColor(c3);
  g.drawOval(col,row-20,40,40);

//back wheel spikes
  g.setColor(c2);
  x=(col+20)+(int)(20*Math.sin(angle));
  y=(row)+(int)(20*Math.cos(angle));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+45));
  y=(row)+(int)(20*Math.cos(angle+45));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+90));
  y=(row)+(int)(20*Math.cos(angle+90));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+100));
  y=(row)+(int)(20*Math.cos(angle+100));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+135));
  y=(row)+(int)(20*Math.cos(angle+135));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+145));
  y=(row)+(int)(20*Math.cos(angle+145));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+180));
  y=(row)+(int)(20*Math.cos(angle+180));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+190));
  y=(row)+(int)(20*Math.cos(angle+190));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+225));
  y=(row)+(int)(20*Math.cos(angle+225));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+235));
  y=(row)+(int)(20*Math.cos(angle+235));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+280));
  y=(row)+(int)(20*Math.cos(angle+280));
  g.drawLine(col+20, row, x, y);
  x=(col+20)+(int)(20*Math.sin(angle+325));
  y=(row)+(int)(20*Math.cos(angle+325));
  g.drawLine(col+20, row, x, y);

//front wheel
  g.setColor(c3);
  g.drawOval(col1,row-20,40,40);
//front wheel spikes
  g.setColor(c2);
  x=(col1+20)+(int)(20*Math.sin(angle));
  y=(row)+(int)(20*Math.cos(angle));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+45));
  y=(row)+(int)(20*Math.cos(angle+45));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+90));
  y=(row)+(int)(20*Math.cos(angle+90));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+100));
  y=(row)+(int)(20*Math.cos(angle+100));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+135));
  y=(row)+(int)(20*Math.cos(angle+135));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+145));
  y=(row)+(int)(20*Math.cos(angle+145));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+180));
  y=(row)+(int)(20*Math.cos(angle+180));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+190));
  y=(row)+(int)(20*Math.cos(angle+190));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+225));
  y=(row)+(int)(20*Math.cos(angle+225));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+235));
  y=(row)+(int)(20*Math.cos(angle+235));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+280));
  y=(row)+(int)(20*Math.cos(angle+280));
  g.drawLine(col1+20, row, x, y);
  x=(col1+20)+(int)(20*Math.sin(angle+325));
  y=(row)+(int)(20*Math.cos(angle+325));
  g.drawLine(col1+20, row, x, y);
  String str=x+"  "+y;
  if(col%3==0)
  {
   g.setColor(c6);
   g.fillOval(col-90, row-30, 5,8);
   g.fillOval(col-90, row-15, 5,8);
   g.fillOval(col-90, row, 5,8);
  }
  if(col%3==1)
  {
   g.setColor(c5);
   g.fillOval(col-80, row-20, 3,5);
   g.fillOval(col-80, row-5, 3,5);
  }
  if(col%3==2)
  {
   g.setColor(black);
   g.fillOval(col-68, row-10, 3,4);
  }
  angle+=1;
  if(col==1500)
  {
   col=-300;
   col1=col+220;
  }
  else
  {
   col+=1;
   col1+=1;
  }
 }
}

Output
Java Program Name: Applet_Animation.java