Sunday, November 27

ANDROID GRAPHICS APPLICATION ~ BASIC CODES


 This post include:
  • Complete code for a simple graphics application in Android.
  • Concept of process in Android.
  • Use of Thread in Android programming.


Welcome friend,

 Here I'm posting the basic code for a simple Graphics application in Android ~ a "WELCOME-SCREEN"..!!

For any application, welcome screen is a must .(remember any game starts with an attractive welcome note,isnt it?).So chose to start graphics coding with craeting a welcome screen.

 summary of application::
  • When we open this application, a welcome screen will splash out and stay for a while.
  • We can also close this screen by pressing DOWN key.
  • Followed by this welcome screen we can place starting activity of our apllication.
Get..!! Set..!! GO..!!

Well,first create a new Android project in your Eclipse platform.
Let us start with MySpalsh.java

  1. public class MySplash extends Activity {
  2. private Thread myThread;
  3. protected int  SplashTime = 8000;

At first, Within the class we defined a thread <2> to display the welcome screen for a default time predefined by <3>

  1. public void onCreate(Bundle savedInstanceState) {
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.splash);
  4. final MySplash SplashProcess = this;

Within the onCreate function, (which will be active on starting) we set the screen as splash.xml<3>. we should link the welcome image to this xml file. we also created one process-instance <4> .

  1. myThread = new Thread() {
  2.          @Override
  3.           public void run() {
  4.               try {            
  5.                synchronized(this){
  6.                 wait(SplashTime);
  7.                }
  8.                 } catch(InterruptedException e) {}

Followed by this we define a thread to display the welcome image.<1> @override provision should also given<2>. we know that, in a Thread the code-fragment within run() will be executed<3>.Here<5> the keyword synchronised is used to get synchronisation among threads(i.e., at a time only one thread will be active).Each Thread will "wait" for the default wait-time<6>

  1.  finally {
  2.   finish();
  3.   Intent i = new Intent();
  4.                  i.setClass(SplashProcess, page2.class);
  5.            startActivity(i);
  6.               stop();
  7.              }
  8.          }
  9.         };
After this we define finally, specifying , after the working of Threads(i.e, when welcome image vanishes) what should do. finish() indicate<2> scope of Threads ends.Next activity/page (here page2.class)is started<5>. stop() is used to kill all the Threads created <6>

 myThread.start();

After Thraed definition we should call start() function to start Thread excecution.

  1. public boolean onTouchEvent(MotionEvent event) {
  2.     if (event.getAction() == MotionEvent.ACTION_DOWN) {
  3.      synchronized(myThread){
  4.       myThread.notifyAll();
  5.      }
  6.    }
  7.     return true;}
If we have to close welcome screen (without waiting till it ends) add this code. It detects whteher DOWN key is pressed<2>, if so, do notifyAll() function <4>.

The complete code is as follows:


MySplash.java


package com.VipinNarayan.splash;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.MotionEvent;
public class MySplash extends Activity {
protected int  SplashTime = 8000;
private Thread myThread;
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        final MySplash SplashProcess = this;
        myThread = new Thread() {
         @Override
          public void run() {
              try {            
               synchronized(this){
                wait(SplashTime);
               }
               } catch(InterruptedException e) {}
             finally {
                 finish();
                Intent i = new Intent();
                 i.setClass(SplashProcess, page2.class);
           startActivity(i);
                 stop();
            }
         }
        };
      myThread.start();
 }
@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
     synchronized(myThread){
      myThread.notifyAll();
     }
   }
 return true;
}
}


page2.java

package com.VipinNarayan.splash;
import android.app.Activity;
import android.os.Bundle;
public class page2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


splash.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<ImageView android:src="@drawable/android" android:layout_height="match_parent" android:layout_width="wrap_content" android:id="@+id/ImageView01"></ImageView>
</LinearLayout>

Thursday, November 24

Disable Java Update Notification





Fed up with getting prompted to update Java, or if you have some web application like Banner that requires a particular version??? You can use a simple registry hack to disable notification of available updates...!!!


(1). Open the Registry Editor by going to the Start button and typing in regedt32.

(2).Navigate through to the following key: HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Update\Policy

(3).Change the value of EnableAutoUpdateCheck to 0 and the value of EnableJavaUpdate to 0.
Java should no longer prompt you for the annoying updates.



Enjoy..!!!

Is this trick useful??  Comment here..
Thank you.!!  :)

Tuesday, November 22

5 PROCESSES TO CHECK ARMSTRONG AND REVERSE OF NUMBER~ Unix Program

Hi reader,
  Here our task is :

  • Create 5 processes.
  • Find whether a number is Armstong and Reverse the same using these processes.



A :Accept number.
C:Find whether it is armstrong.
D:Print Result.
B:Calculate Reverse of the Number.
E:Display Reverse.



Here is the complete Program.


#include<stdio.h>
#include<unistd.h>
main()
{             //initially in A

int num,f1,f2,f3,f4,p1[2],p2[2],p3[2],p4[2],arm,rev,flag=0;
pipe(p1);
pipe(p2);


f1=fork();  //A created B
if(f1>0)    //A is working ..
{
f2=fork();  //A created C

if(f2>0)     //A is working...
{
printf("Enter a number   :");
scanf("%d",&num);


close(p1[0]);
close(p2[0]);
write(p1[1],&num,sizeof(num));
write(p2[1],&num,sizeof(num));
}
if(f2==0)   //C is working..
{


pipe(p3);
f3=fork(); //C created D

if(f3>0)   //C is working..
{
close(p1[1]);
read(p1[0],&num,sizeof(num));
flag=0;

for(arm=0;num>0;num/=10)
arm+=(num%10)*(num%10)*(num%10);

if(num==arm)flag=1;
close(p3[0]);
write(p3[1],&flag,sizeof(flag));
}
else if (f3==0)  //D is working..
{

close(p3[1]);
read(p3[0], &flag, sizeof(flag));
(flag)?printf("It is Armstrong Number"):("It is Not Armstrong Number");

}
}
}
if(f1==0)   //B is working..

{
sleep(2);
pipe(p4);
f4=fork(); //B created E

if(f4>0)   //B is working..{
close(p2[1]);
read(p2[0],&num,sizeof(num));
for(rev=0;num>0;num/=10)
rev=10*rev+num%10;
close(p4[0]);
write(p4[1],&rev,sizeof(rev));
}
if(f4==0)   //E is working..

{
close(p4[1]);
read(p4[0],&rev,sizeof(rev));
printf("Reverse of Number is : %d\n",rev);
}
}
}





Output
Enter a number: 153
It is Armstrong Number
Reverse of Number is :351

Monday, November 21

Lex program ~ Read and copy file content


This lex program will read an input file and copy the content with line number to another file.






%{
#include<stdio.h>
#include<string.h>
char line[20];
int count=0,i=0;
FILE *out;
%}
%%
['\n'] { fprintf(out,"%d %s\n",++count,line);}
(.*) {
strcpy(line,yytext);
}
%%
main()
{
yyin=fopen("in.c","r");
out=fopen("out.c","w");
yylex();
getch();
}

int yywrap()
{
return 1;
}








input
main()
{
int i=9, j=900,k=115;
printf("%d,i+j+k);
}


output file


1 main()
2 {
3 int i=9, j=900,k=115;
4 printf("%d,i+j+k);
5 }
6 }

Read a file and copy content to other file~ C code




This C program will read an input file and copy the content to another file with Line numbers..!!


#include<stdio.h>
main()
{
      FILE *f1,*f2;
      int t=0;
      char line[20];
      f1=fopen("in.c","r");
      f2=fopen("out.c","w");
   
      while(!feof(f1)){
      fgets(line,20,f1);
      t++;
      fprintf(f2,"%d  %s",t,line);
      }
     getch();
      }

Lex- Collect Tokens and sort

Hello friend,
  Here I am with an interesting Lex program which sorts all the nubers from input file and sort them. If you are well-familiarised with lex paradigm, it is very easy task.

Things to remember:
  • The definition [0-9]+ can detect all the numbers from input
  • The number detected at present will be pointed by yytext and have length yyleng
  • Whenever a new token (here a number) is detected, yytext will be replaced with new value. 
Logic
  1. Read the input
  2. If a number is detected, store it in an array (we can use yytext and yyleng for this). Read each digit --> multiply with 10 --> add next digit -->continue upto last digit.
  for eg, to read 123
initialise num=0;
  • read 1--> num=num*10+1-->num=0+1-->num=1
  • read 2--> num=num*10+2-->num=10+2-->num=12
  • read 3--> num=num*10+3-->num=120+3-->num=123
store num in an array.
3. After reading all the numbers, sort the number within the array




%{
#include<stdio.h>
int t, a[20],i,j,n,num;
%}
%%
[0-9]+ {num=0;
 for(i=0;i<yyleng;i++)
num=num*10+(yytext[i]-'0');

a[n++]=num;
}
. ;
%%
main()
{
yyin=fopen("file.c","r");
yylex();
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++)
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
for(i=0;i<n;i++)
printf("%d\n",a[i]);

}

int yywrap()
{
return 1;
}





sample input (in file.c)
main()
{
int i=9, j=900,k=115;
printf("%d,i+j+k);
}




output
9
115
900

Sunday, November 20

Quadruple ~ C code

#include<stdio.h>
#include<string.h>


main()
{

 char line[20];
int s[20];
int t=1;
     
int i=0;     
printf("Enter string..  :");
gets(line);
for(i=0;i<20;i++)s[i]=0;

printf("op\ta1\ta2\tres\n");
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='/' || line[i]=='*')
{
                printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;

}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);

s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}


for(i=2;line[i]!='\0';i++)
{
if(line[i]=='+' || line[i]=='-')
{
                printf("\n");
if(s[i]==0)
{
if(s[i+1]==0)
{
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;

}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);

s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
printf("\n:=\tt%d\t\t%c",t-1,line[0]);




getch();
}





Output




Enter string..  :a=b*c-b*c


op      a1      a2      res
:=      c                t1
*       b       t1       t2
:=      c                t3
*       b       t3       t4
-       t2      t4       t5
:=      t5              a