Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Can you solve part of the problem of below question

  Asked By: Craig    Date: Jul 18    Category: Java    Views: 736
  

A Scroll Banner is an applet which displays 2 scrolling messages
across the applet's 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;
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Can you solve part of the problem of below question Or get search suggestion and latest updates.




Tagged: