Saturday, October 03, 2015

Java applet program to create a marquee with custom user defined text and number of counts.

      Hi there, Today I'm going to show you all how to make an applet that will generate a marquee using a custom user defined text and number of counts that it should run. For this I have used Java Applet class and awt controls and a thread to simulate marquee over applet. Following is the code for the Java Applet....

Marquee.java


import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code="Marquee.class" width=800 height=300></applet>*/
public class Marquee extends Applet implements ActionListener,ItemListener,Runnable{
int num;
int x,y;


int count;

String s;

Label l1,l2,l3,marlabel,lblcount;

TextField t1,t2;

Button bt;

Thread th;

Font myfont;

Checkbox cb1;

public void init()

{

th=null;

myfont=new Font(Font.SANS_SERIF,Font.BOLD,36);

s=new String();

x=-10;

y=10;

count=0;

l1=new Label("Enter a String:");

t1=new TextField("",50);

l2=new Label("Enter number of counts:");

t2=new TextField("",20);

l3=new Label("Marquee Count:");

lblcount=new Label("0");

bt=new Button("Start");

bt.addActionListener(this);

marlabel=new Label();

cb1=new Checkbox();

cb1.setLabel("Repeatitons");

cb1.addItemListener(this);

t2.setEnabled(false);

add(marlabel);

add(l1);

add(t1);

add(cb1);

add(l2);

add(t2);

add(l3);

add(lblcount);

add(bt);

setSize(800,300);

setBackground(Color.CYAN);

l1.setBackground(Color.CYAN);

l2.setBackground(Color.CYAN);

l3.setBackground(Color.CYAN);

cb1.setBackground(Color.CYAN);

lblcount.setBackground(Color.CYAN);

marlabel.setBackground(Color.blue);

marlabel.setForeground(Color.WHITE);

}


public void paint(Graphics g)

{

g.setColor(Color.blue);

g.fillRect(-10, 10, 850, 50);

marlabel.setFont(myfont);

marlabel.setText(s);

marlabel.setLocation(x, y);

marlabel.setBounds(x,y,s.length()*25,50);

l1.setLocation(10, 80);

cb1.setLocation(10, 110);

l2.setLocation(10, 140);

l3.setLocation(10, 180);

lblcount.setText(String.valueOf(count));

lblcount.setLocation(120, 180);

lblcount.setSize(50, 25);

t1.setLocation(180, 80);

t2.setLocation(180, 140);

bt.setLocation(180, 180);

}

public void actionPerformed(ActionEvent e)

{

String as=new String(e.getActionCommand());

if(as.equals("Start"))

{

if(!(t1.getText().isEmpty()))

{

bt.setLabel("Stop");

s=t1.getText();

if(t2.isEnabled())

{

num=Integer.parseInt(t2.getText());

count=0;

}

else

{

count=0;

num=0;

}

marlabel.setText(s);

start();

}

}

else

{

bt.setLabel("Start");

stop();

}

}

public void itemStateChanged(ItemEvent e)

{

if(cb1.getState())

{

t2.setEnabled(true);

}

else

{

t2.setEnabled(false);

}

}

public void start()

{

if(bt.getLabel().equals("Stop"))

{

if(th==null)

{

th=new Thread(this);

}

th.start();

}

}

public void stop()

{

th=null;

}

public void run()

{

// lower ThreadPriority

Thread.currentThread().setPriority(Thread.MIN_PRIORITY);


// run a long while (true) this means in our case "always"

while (bt.getLabel().equals("Stop"))

{ // repaint the applet

x++;

repaint();


if(x>800)

{

x=(s.length()*25)*-1;

count++;

if(num!=0 && count==num)

{

count=0;

bt.setLabel("Start");

}

}

try

{

Thread.sleep (5);

}

catch (InterruptedException ex)

{ // do nothing

}


// set ThreadPriority to maximum value

Thread.currentThread().setPriority(Thread.MAX_PRIORITY);

}

}
}

Hope you all like it....


No comments:

Post a Comment