The Artima Developer Community
Sponsored Link

Java Answers Forum
Help

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ernie Douglas

Posts: 18
Nickname: y2j
Registered: Nov, 2002

Help Posted: Nov 26, 2002 9:37 AM
Reply to this message Reply
Advertisement
could someone help me with the rest of this coding please
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class guessnumber extends Applet implements Runnable, ActionListener
{
String msg;
Thread t = null;
int state;
boolean stopflag;
private Label l1, l2;
private TextField t1, t2;
private Button b1;
private int num, mynum, diff, red, blue, green, chance = 0;
private Color c;

public void init()
{
c = new Color( 0, 80, 0 );
setForeground( c );
l1 = new Label("I have kept a number between 1 and 1000.");
l2 = new Label("Can you guess this number?");
t1 = new TextField(10);
t2 = new TextField(10);
b1 = new Button("Play Again");
t2.setEditable(false);
add(l1);
add(l2);
add(t1);
add(t2);
add(b1);

t 1.addActionListener(this);
b1.addActionListener(this);
num = 1 + (int) (Math.random()*1000);
}

public void start()
{
msg = getParameter( "message" );
if(msg == null)
{
msg = "Message not found";
}
msg = " " + msg;
t = new Thread( this );
stopflag = false;
t.start();
}

public void run()
{
char ch;
for( ; ; )
{
try
{
repaint();
Thread.sleep(200);
ch = msg.charAt(0);
msg = msg.substring( 1, msg.length() );
msg += ch;
if(stopflag)
{
break;
}
}
catch( InterruptedException e)
{
System.out.println( "Exception" + e );
}
}
}

public void stop()
{
stopflag = true;
t = null;
}

public void paint(Graphics g)
{
g.drawString( msg, 70, 130 );
}

public void actionPerformed( ActionEvent ae )
{

if( ae.getSource() == b1 )
{
red = 255;
blue = 255;
green = 255;
c = new Color(red, green, blue);
setBackground(c);
num = 0;
num = 1 + (int) (Math.random()*1000);
chance = 0;
showStatus( "" );
t1.setEditable(true);
t2.setEditable(false);
t1.setText("");
t2.setText("");
}
else
{
mynum = Integer.parseInt(t1.getText());
chance++;
if( chance == 1 )
{
showStatus( chance + " chance over" );
}
else
{
showStatus( chance + " chances over" );
}
if( chance >= 9 )
{
t1.setText( String.valueOf(num) );
showStatus( "Your 9 chances are up" );
t1.setEditable( false );
}
diff = num - mynum;
if(diff == 0)
{
setBackground(Color.pink);
t1.setEditable(false);
t2.setText("You've done it");
t2.setEditable(false);
}
else if(Math.abs(diff)>200)
{
if(diff>0)
{
t2.setText("Too Low");
}
else
{
t2.setText("Too High");
}
}
else if(Math.abs(diff)<200)
{
if(diff>0)
{
red = 255-diff;
green = 0;
blue = 0;
c = new Color(red, green, blue);
t2.setText("");
setBackground(c);
}
else if(diff<0)
{
red = 0;
green = 0;
blue = 255 + diff;
c = new Color(red, green, blue);
t2.setText("");
setBackground(c);
}
}
}
}
}

i need to know how to

Topic: write-method in my class Previous Topic   Next Topic Topic: why does my code print out the same word 12 times

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use