Old 12-25-2008   #1 (permalink)
Registered User
 

Join Date: Dec 2008
Posts: 2

Beginning Java Script Help, Please!

I'm self-studying computer science for enrichment, and I have to say the first thing I gained was a new-found respect for this field and those in it. With that said, I'm so dang confused on my last homework problem that concerns applets. I would appreciate any help I can get. But I'm sure what's hard for me is simple for you hehe.

The question asks to make an applet emulating a banner ad. The ad should display a message alternating "East or West" and "Java is Best" every 2 seconds.

Here is the prototype code I've been given:

// This applet displays a message moving horizontally
// across the screen.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Banner extends JApplet
implements ActionListener
{
private int xPos, yPos; // hold the coordinates of the banner

public void init()
{
Container c = getContentPane();
c.setBackground(Color.WHITE);
xPos = c.getWidth();
yPos = c.getHeight() / 2;
Timer clock = new Timer(30, this); // fires every 30 milliseconds
clock.start();
}

// Called automatically after a repaint request
public void paint(Graphics g)
{
super.paint(g);
g.drawString("Hello, World!", xPos, yPos);
}

// Called automatically when the timer fires
public void actionPerformed(ActionEvent e)
{
Container c = getContentPane();

// Adjust the horizontal position of the banner:
xPos--;
if (xPos < -100)
{
xPos = c.getWidth();
}

// Set the vertical position of the banner:
yPos = c.getHeight() / 2;

repaint();
}
}

Here are the hints the book includes (they didn't help me much )

At the top of your class define a variable that keeps track of which message is to be displayed. For example, private int msgID = 1;

In the method that processes the timer events, toggle msgID between 1 and -1: msgID = -msgID

Don't forget to call repaint.

In the method that draws the text, obtain the coordinates for placing the message:
Container c = getContentPane();
int xPos = c.getWidth() / 2 - 30;
int yPos = c.getHeight() / 2;

Then use a conditional statement to display the appropriate message:
if (msgID == 1)
{
...
}
else // if msgID == -1
{
...
}

THANK YOU AGAIN FOR YOUR HELP!
gradelover924 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 12-25-2008   #2 (permalink)
Janitor of Lunacy
 
Tamlin's Avatar
 

Join Date: May 2006
Location: Sitting in the Wishing Chair
Posts: 5,736
Blog Entries: 1
Images: 531

Battle Wins: 20 (rank: #1)
So what, specifically, is your question? Which particular part is confusing you?
__________________

"I might join your century, but only as a doubtful guest"
Tamlin is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 12-26-2008   #3 (permalink)
Registered User
 

Join Date: Dec 2008
Posts: 2

What throws me off is this msgID thing they're talking about in the hints. I understand the defining a variable, but the msgID = -msgID perplexed me. "Don't forget to call repaint" didn't help me at all. I think that's the source of my confusion.
gradelover924 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Old 12-29-2008   #4 (permalink)
Registered User
 
U'ziel's Avatar
 

Join Date: Sep 2005
Posts: 88
Images: 1

Let me start by saying I dont know Java but most code is more or less the same structure. msgID is used as a switch variable, the simplest example of this is binary code 1 and 0.

for example;
var msgID

if (msgID == 1) {
echo "hello world";

} else if (msgID == 0) {
echo "goodbyle world";

}

for example. If the switch is equal to 1 the code outputs hello world. if its 0 goodbye world. Simple.

repaint is a function you have right at the bottom of your sample code to change the text and position it where you have set it. So you would want something like;

if (msgID == 1) {
repaint(1);

} else if (msgID == -1) {
repaint(-1);

}

Your using the switch in the code to determine the action to take and the func to do the action and re-set the variable so it knows the next action to take, if that makes sense?

after this line "private int xPos, yPos; // hold the coordinates of the banner"
add

private int msgID = 1;

Hope this explains it a little and helps you on your way. Tell your tutor that silverlight would be a much better use of your time for programming a rotating banner add
U'ziel is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Script Help (Banner Ad Rotator) L4A HTML / PHP / ASP / JS 6 08-26-2008 01:18 AM
Php Script Miss-Joy HTML / PHP / ASP / JS 2 01-22-2008 10:44 PM
Character Modeling from the beginning onbrid Showrooms & Works In Progress 5 11-08-2004 06:22 PM
is there a script for this...? CrazyMee HTML / PHP / ASP / JS 2 08-04-2004 10:24 PM
just beginning, and stumped on the 1st Step. Wiicked Klown Adobe Photoshop 18 06-25-2004 08:57 PM


All times are GMT +1. The time now is 03:16 AM.

Powered by vBulletin Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1

Design & Content © BioRUST 2008 :: PRIVACY STATEMENT :: LEGAL INFORMATION :: ADVERTISING MEDIA KIT