Develop Your First Exciting Java applet: running name


Hi,

in this tutorial we will see how to create simple java applet

we will create simple applet to show message in applet window

for that first create a class running name extending Applet as we are also using thread implement Runnable

Now our class is

  1. public class runningname extends Applet implements Runnable{
    …..
    here’s our code
    ……
    }

2. Now  its time to add libraries

for this java applet import following libraries.

 import java.awt.*;

import java.applet.*;

for beginners,

these import lines should be on top of the program.
3. Variables we are going to use

    String msg,msg1;
Thread t=null;
int state;
boolean stopflag;

4. Lets initialize our applet

public void init()
{
setBackground(Color.yellow);
setForeground(Color.red);
}

background color will be screen color of applet and Foreground will be our msg color.

5. We are using thread to move string so thread should be started lets write start method for thread

public void start()
{
msg=”hi!this is bhushan “;
msg1=”this is first applet program “;
if(msg==null||msg1==null)
msg=msg1=”plz write something in msg”;
msg=” “+msg;
t=new Thread(this);
stopflag=false;
t.start();
}

here we are staring thread t.

set stopflag to false.

because when stopflag is true then we will break run method to exit from program.

6. Now lets write run method

Thread executes what is written in run method

public void run()
{
int i=1;
char c,c1;
while(i>0)
{
try{
repaint();
Thread.sleep(200);
c=msg.charAt(0);
c1=msg1.charAt(0);
msg=msg.substring(1, msg.length());
msg1=msg1.substring(1, msg1.length());
msg+=c;
msg1+=c1;
if(stopflag)
break;
}catch(InterruptedException e)
{
System.out.println(“error in printing”);
}
}
}

Explanation:

Repaint will execute paint method again.

first character of msg is stored then msg is replaced by substring staring from 2nd character of msg to its length.

first character is put at last of msg.

this is continued…

so that it appears to be moving message.

7.To stop program thread should be terminated and stopflag should have true value

so lets write stop method

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

But in case of our program stop method is never executed because in run methos it calls repaint again ans again but never sets stopflag to true.

8. very important method of our program

public void paint(Graphics g)
{
g.drawString(msg, 50, 40);
g.drawString(msg1, 50, 80);
}

g.drawString will put string at given co-ordinates.

in applet programming there is no main method.

Now we are ready with our applet

here is our final ready applet

<blockquote>
import java.awt.*;
import java.applet.*;
public class runningname extends Applet implements Runnable{
String msg,msg1;
Thread t=null;
int state;
boolean stopflag;
public void init()
{
setBackground(Color.yellow);
setForeground(Color.red);
}
public void start()
{
msg="hi!this is bhushan";
msg1="this is first applet program";
if(msg==null||msg1==null)
msg1=msg="plz write something in msg";
msg=" "+msg;
t=new Thread(this);
stopflag=false;
t.start();
}
public void run()
{
int i=1;
char c,c1;
while(i>0)
{
try{
repaint();
Thread.sleep(200);
c=msg.charAt(0);
c1=msg1.charAt(0);
//System.out.println(c);
msg=msg.substring(1, msg.length());
msg1=msg1.substring(1, msg1.length());
// System.out.println(“ “+msg);
msg+=c;
msg1+=c1;
if(stopflag)
break;
}catch(InterruptedException e)
{
System.out.println("error in printing");
}
}
}
public void stop()
{
t=null;
stopflag=true;
}
public void paint(Graphics g)
{

g.setFont(new Font("Algerian",Font.PLAIN,25));
this.setSize(1400,700);
g.drawString(msg, 50, 40);
g.setColor(Color.CYAN);
g.drawString(msg1, 50, 80);
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s