A Scroll Banner is an applet which displays 2 scrolling messages across the applet抯 window. Since the scrolling of a message is a repetitive task, it is performed by a Separate thread, created by the applet when it is initialized. Here background color used for the applet is yellow and foreground color used for displaying the messages is red. The upper message is displayed at position x = 100 and y = 200. This message is scrolled from right to left. The lower message is displayed at position x = 100 and y = 50. This message is scrolled from left to right. The Browser displays the applet till new page comes into view. At this time the Applet stops itself.
Here is code for the program which I create but I am not able to create two thread
Simultaneously please correct it
import java.applet.*;
import java.awt.*;
// // AnimationApplet
// // AnimationApplet displays simple animation, generated
// by an animation thread. This applet is an example from
// "Unravelling threads" // //
public class AnimationApplet extends Applet implements Runnable
{ // Reference to animation thread
private Thread AnimationThread = null;
// String for text scrolling (DEFAULT VALUE)
private String m_AnimationString = "Welcome to my web page";
// Name of parameter
private final String PARAM_AnimationString = "AnimationString";
// Buffered graphics display
private Image bufferedDisplay = null;
// Boolean flag to see whether animation should continue
private boolean animate = true;
// AnimationApplet Constructor
public AnimationApplet()
{
// TODO: Add constructor code here
}
public String getAppletInfo()
{
return "Name: AnimationApplet\r\n" + "Author: David Reilly\r\n";
} // Return a list of parameters
public String[][] getParameterInfo()
{
String[][] info = { {
PARAM_AnimationString, "String", "String to be animated" }, };
return info; }
// Init method for applet initialization
public void init()
{
String param;
// Get parameter or use default if none present
param = "Avinash Kumar Sinha";
//param = getParameter(PARAM_AnimationString);
if (param != null) m_AnimationString = param;
// Resize applet if we're in appletviewer
resize(500, 250);
// Get an image for offscreen drawing
bufferedDisplay = createImage(500,250);
}
public void update(Graphics g) { paint(g); }
// Paint handler, called when applet display is updated
public void paint(Graphics g)
{
if ( bufferedDisplay != null)
g.drawImage(bufferedDisplay, 0,0, null);
}
// Start method when applet is displayed in browser, or page reloaded
public void start()
{ // Check to see if thread is active
if (AnimationThread == null)
{ // Pass our thread an instance of java.lang.Runnable (us)
AnimationThread = new Thread(this);
// Start the thread (new thread executes public void run() method
AnimationThread.start(); } }
// Stop method invoked when page containing applet not visible
public void stop() {
// If animation thread running....
if (AnimationThread != null)
{
// .... stop it, to prevent wasted CPU
AnimationThread.stop();
// Clear our reference to the thread, for automated garbage collection
AnimationThread = null;
}
// TODO: Place additional applet stop code here
}
// Thread code execution starts here
public void run() {
// Get dimensions of the applet
int width = size().width;
int height= size().height;
// Offset for text message (start offscreen)
int xOffset = width + 15;
int yOffset = height / 2;
// Get an instance of java.awt.Graphics to draw on image
Graphics display = bufferedDisplay.getGraphics();
// Set font for display
Font textFont = new Font("Arial", Font.PLAIN, 16);
display.setFont(textFont);
// Get width of text string
FontMetrics fm = display.getFontMetrics();
int strWidth = fm.stringWidth(m_AnimationString);
while (true)
{
try
{
if (animate == false)
{
Thread.sleep(1000); continue;
}
// Clear display to black
display.setColor (Color.yellow);
display.fillRect (0,0, width, height);
// Now draw text
display.setColor (Color.gray);
display.drawString (m_AnimationString, xOffset, yOffset);
// Decrement xOffset for scrolling effect
xOffset--;
if (xOffset < -strWidth)
xOffset = width+1;
repaint();
Thread.sleep(10);
}
catch (InterruptedException e)
{
stop();
}
}
}
public boolean mouseDown(Event evt, int x, int y)
{
// Suspend animation
animate = false;
return true;
}
public boolean mouseUp(Event evt, int x, int y)
{
// Resume animation
animate = true;
return true;
}
}
Here is the problem. I attempted but two thread not run successfully can anyone me.
Create 2 Thread classes.One Thread is Incrementor and has one variable cnt1 with initial Value 0. Incrementor thread increments value of cnt1 by 1 each time. The other thread is Decrementor which has variable cnt2 with initial value 100. Decrementor thread decrements value of cnt2 by 1 each time.
- Incrementor thread increments value of cnt1 by one and notifies the other thread about this value
- The decrementor thread decrements value of its variable cnt2 by one and compares values of cnt1 and cnt2. If values of cnt1 and cnt2 are different then notifies the Incrementor thread and above mentioned step is repeated.
- But if values of cnt1 and cnt2 are matching then following message is displayed
Matching Value : 50
import java.awt.*;
import java.applet.*;
public class increment extends Applet implements Runnable
{
int incr,decr;
Thread th1,th2;
boolean b1 = true;
boolean b2 = true;
public void init()
{
incr =0;
decr = 50;
}
public void start()
{
th1 = new Thread(this);
th1.start();
th2 = new Thread(this);
th2.start();
}
public void run()
{
while(b2)
{
//b1=false;
try
{
th2.sleep(100);
decr -= 2;
repaint();
notify();
synchronized(this)
{
while(b1)
wait();
}
}catch(InterruptedException e) {}
}
while(b1)
{
try
{
th1.sleep(100);
incr += 1;
repaint();
b2 = true;
notify();
synchronized(this)
{
while(b2)
wait();
}
}catch(InterruptedException e) {}
}
}
public void stop()
{
b1 = false;
b2 = false;
}
public void paint(Graphics g)
{
g.drawString("Value is:" +incr +"decr :="+decr,200,200);
if(incr == decr)
{
g.drawString("Value have matched: incr=" +incr + "decr: "+decr,200,250);
b1 = false;
b2 = false;
}
}
public void update()
{
repaint();
}
}
mail me on
avinash08@hotmail.com

New Topic/Question
Reply




MultiQuote




|