Error in Java Programming

Error in Java Programming

Page 1 of 1

1 Replies - 4929 Views - Last Post: 22 December 2006 - 09:09 AM Rate Topic: -----

#1 avinash08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-December 06

Error in Java Programming

Posted 21 December 2006 - 02:37 AM

will you solve part of the problem of below question

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

Is This A Good Question/Topic? 0
  • +

Replies To: Error in Java Programming

#2 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: Error in Java Programming

Posted 22 December 2006 - 09:09 AM

you could use two seperate threads one for left moving text and one for right moving text, e.g.
import java.applet.*;
import java.awt.*;
import java.util.*;

// // AnimationApplet
// // AnimationApplet displays simple animation, generated
// by an animation thread. This applet is an example from
// "Unravelling threads" // //
public class AnimationApplet1 extends Applet 
{ // Reference to animation thread
Thread AnimationThread = null;
// String for text scrolling (DEFAULT VALUE)
String m_AnimationString = "Welcome to my web page";
// Name of parameter
private final String PARAM_AnimationString = "AnimationString";
// Buffered graphics display
Image bufferedDisplay = null;
// Boolean flag to see whether animation should continue
boolean animate = true;
// AnimationApplet Constructor
public AnimationApplet1()
{
	// 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)
		   { // create two Threads on going left one going right and start them
			AnimationThread thread1 = new AnimationThread(this, true);
			thread1.start();  
			AnimationThread thread2 = new AnimationThread(this, false);
			thread2.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
	}


	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;
	}
	}

// this is the Thread class
class AnimationThread extends Thread
{
	AnimationApplet1 applet;			  // parent class
	boolean rightLeft;					// indicates if text is moving left or right

	// constructor - pass in parement and indicate if left or right
	AnimationThread(AnimationApplet1 applet, boolean rightLeft)
	   {this.applet=applet; this.rightLeft=rightLeft;}


	// Thread code execution starts here
	public void run() {
	// Get an instance of java.awt.Graphics to draw on image
	Graphics display = applet.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(applet.m_AnimationString);
	 // Get dimensions of the parenet applet
	int width = applet.size().width;
	int height= applet.size().height/2;
	int xOffset, yOffset;
	// Offset for text message (start offscreen)
	if(rightLeft) { xOffset = width + 15; yOffset=0;	   }  // right to left -  top of applet
	else		  { xOffset = -strWidth;  yOffset= height; }  // left to right - bottom of applet
   while (true)
	{
		try
		{
			if (applet.animate == false)
		{
		Thread.sleep(1000); continue;
		}
	// Clear display to black
	display.setColor (Color.yellow);
	display.fillRect (0,yOffset, width, height);	// clear half of applet and draw the string
	// Now draw text
	display.setColor (Color.gray);
	display.drawString (applet.m_AnimationString, xOffset, yOffset+height/2);
	// Decrement or increment xOffset for scrolling effect
	if(rightLeft) xOffset--; else xOffset++;
	if (rightLeft && xOffset < -strWidth)  xOffset = width+1;
	if (!rightLeft && xOffset == width )  xOffset =-strWidth;
	applet.repaint();
	Thread.sleep(10);
	}
	catch (InterruptedException e)
	{
		stop();
	}
	}
}
  }


This post has been edited by horace: 22 December 2006 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1