Results 1 to 17 of 17
Thread: Assistant on my codes. SOS!!
- 08-12-2008, 09:04 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
Assistant on my codes. SOS!!
Hi,
I've already created the layout. I am creating a game call "Toto". User will click on the Button Winning Numbers and a random winning number will appear above the button "Winning Numbers". The problem is Im stuck in creating the random number generator. Need help please..
Thank You.
Here is my codes which I have done.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.awt.Color;
import java.util.Random; // For random number generator
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Toto extends JApplet
{
JTextField tf1, tf2, tf3, tf4, tf5, tf6; // User to place his bet numbers.
JLabel lbl1, lbl2, lbl3, lbl4, lbl5, lbl6, lbl7, lbl8, lbl9, lbl10, lbl11;
JButton bWinningNumbers, bBet, bReset;
JPanel p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, pp1;
JCheckBox jcbPopcorn, jcbDrinks;
Vector v = new Vector();
public void init()
{
//p1 = new JPanel(new GridLayout(8,1));
p1 = new JPanel();
p1.setLayout(new FlowLayout());
lbl1 = new JLabel("********************************");
p2 = new JPanel();
lbl2 = new JLabel("***** TOTO BETTING MACHINE *****");
lbl2.setFont(new Font("TimesRoman", Font.BOLD, 12));
// lbl2.setForeGround(Color.BLUE);
p3 = new JPanel();
lbl3 = new JLabel("********************************");
p1.add(lbl1);
p2.add(lbl2);
p3.add(lbl3);
p5 = new JPanel();
p5.setLayout(new FlowLayout());
bWinningNumbers = new JButton("WINNING NUMBER!!!!");
// bWinningNumbers.addActionListener(new Select());
p5.add(bWinningNumbers);
p6 = new JPanel();
p6.setLayout(new FlowLayout());
lbl5 = new JLabel("===== Each Bet Cost $5 =====");
lbl5.setFont(new Font("TimesRoman", Font.ITALIC, 12));
p6.add(lbl5);
p7 = new JPanel();
p7.setLayout(new FlowLayout());
lbl6 = new JLabel("- Please Input Numbers Between 1 to 45 -");
lbl6.setFont(new Font("TimesRoman", Font.ITALIC, 13));
p7.add(lbl6);
p8 = new JPanel(); p8.setLayout(new FlowLayout());
tf1 = new JTextField(3);
tf1.setBackground(Color.RED);
tf1.setForeground(Color.CYAN);
tf2 = new JTextField(3);
tf2.setBackground(Color.YELLOW);
tf2.setForeground(Color.RED);
tf3 = new JTextField(3);
tf3.setBackground(Color.GREEN);
tf3.setForeground(Color.BLACK);
tf4 = new JTextField(3); tf4.setBackground(Color.RED);
tf4.setForeground(Color.CYAN);
tf5 = new JTextField(3); //User place betting number
tf5.setBackground(Color.YELLOW);
tf5.setForeground(Color.RED);
tf6 = new JTextField(3); //User place betting number
tf6.setBackground(Color.GREEN);
tf6.setForeground(Color.BLACK);
p8.add(tf1); //Textfield is added in the panel(p8)
p8.add(tf2); //Textfield is added in the panel(p8)
p8.add(tf3); //Textfield is added in the panel(p8)
p8.add(tf4); //Textfield is added in the panel(p8)
p8.add(tf5); //Textfield is added in the panel(p8)
p8.add(tf6); //Textfield is added in the panel(p8)
p9 = new JPanel();
p9.setLayout(new FlowLayout());
bBet = new JButton("BET"); //For button "BET"
lbl7 = new JLabel("Click On Winning Numbers"); //Between Button Bet and Rest
bReset = new JButton("RESET"); //For button "RESET"
p9.add(bBet);
p9.add(lbl7);
p9.add(bReset);
p10 = new JPanel();
p10.setLayout(new FlowLayout(FlowLayout.LEFT));
lbl8 = new JLabel("Money available: $ ");
p10.add(lbl8);
//lbl10 = new JLabel("");
//p10.add(lbl10);
p11 = new JPanel();
p11.setLayout(new FlowLayout(FlowLayout.LEFT));
lbl9 = new JLabel("Accumulated Bet Amount: $ ");
p11.add(lbl9);
//lbl11 = new JLabel();
//p11.add(lbl11);
pp1 = new JPanel(); // The main Panel for the programme.
pp1.add(p1); // Individual panel in the main panel.
pp1.add(p2); // Individual panel in the main panel.
pp1.add(p3); // Individual panel in the main panel.
pp1.add(p5); // Individual panel in the main panel.
pp1.add(p6); // Individual panel in the main panel.
pp1.add(p7); // Individual panel in the main panel.
pp1.add(p8); // Individual panel in the main panel.
pp1.add(p9); // Individual panel in the main panel.
pp1.add(p10); // Individual panel in the main panel.
pp1.add(p11); // Individual panel in the main panel.
add(pp1);
setVisible(true); //Add pp1 for the main panel.
}
}// end of programme
- 08-12-2008, 02:00 PM #2
You need to read the API doc.creating the random number generator
Random rand = new Random(seed); // do this one time in your program
int aNbr = rand.nextInt(); // get next random int
You need to describe what kind and the range of numbers you want to generate.
- 08-12-2008, 05:39 PM #3
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
Hi,
I appreciate your help. But I dont know where i supposed to place the codes u gave.
- Where do I Place this in my codes?
- I want to make random number maximun to only 45.
Random rand = new Random(seed); // do this one time in your program
int aNbr = rand.nextInt(); // get next random int
- 08-12-2008, 08:16 PM #4
Create the Random class object in the init code. Have rand be a class variable.Where do I Place this in my codes?
Read the doc on how to use the Random class methods. One of them will: Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
Use the method call where you need the number.
- 08-13-2008, 06:05 AM #5
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
Im so noob about this. Could you please add in the codes. Ive tried doing but it kept saying error. Ive tried so many ways to do it but still theres error. I dont understand it. Could u help me please.
-
again, read the docs referred to above and give it your best shot. Show us your attempt at using a Random object (I haven't seen any of your attempts posted here yet). In other words, why not try it first and show us what you've tried. It is after all your homework, not ours.
- 08-13-2008, 02:02 PM #7
Copy and post them here if you want help with them.but still theres error.
- 08-14-2008, 06:42 AM #8
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
Ok ive figure out about the random numebers. Heres how ive dont it.
class WinningNumbersListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//handle the button pressing events
Random rand = new Random(); // do this one time in your program
int randonInt1 = rand.nextInt(45);
int randonInt2 = rand.nextInt(45);
int randonInt3 = rand.nextInt(45);
int randonInt4 = rand.nextInt(45);
int randonInt5 = rand.nextInt(45);
int randonInt6 = rand.nextInt(45);
wNum = wNum + randonInt1 + "-" + randonInt2 + "-" + randonInt3 + "-" + randonInt4 + "-" + randonInt5 + "-" + randonInt6;
------------------------------------------------------------------------------------------------------------------------
I have another question. Im doing a button which play music .wav format when click but i have error doing it. It says cannot find symbol method bMusicPlayer();
heres my codes:
class MusicPlayerListener implements ActionListener{
public void init(){
// bMusicPlayer = new Button("MUSIC PLAY");
add(bMusicPlayer);
bMusicPlayer.addActionListener(new MusicPlayerListener());
// stop = new Button(" Stop ");
//add(stop);
// stop.addActionListener(this);
audioClip = getAudioClip(getCodeBase(), "how_six_songs_collide.wav");
}
public void actionPerformed(ActionEvent ae){
Button source = (Button)ae.getSource();
if (source.getLabel() == "MUSIC PLAY "){
audioClip.bMusicPlayer(); <<<<<<<<<<<<<<<<<< Here is the error.
}
//else if(source.getLabel() == " Stop "){
//audioClip.stop();
//}
}
}
Why it kept saying that error?
- 08-14-2008, 06:43 AM #9
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
Sorry bout the previous post, typo errors.
-----------------------------------------------------------------------
Ok ive figure out about the random numbers. Heres how ive done it.
class WinningNumbersListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
//handle the button pressing events
Random rand = new Random(); // do this one time in your program
int randonInt1 = rand.nextInt(45);
int randonInt2 = rand.nextInt(45);
int randonInt3 = rand.nextInt(45);
int randonInt4 = rand.nextInt(45);
int randonInt5 = rand.nextInt(45);
int randonInt6 = rand.nextInt(45);
wNum = wNum + randonInt1 + "-" + randonInt2 + "-" + randonInt3 + "-" + randonInt4 + "-" + randonInt5 + "-" + randonInt6;
------------------------------------------------------------------------------------------------------------------------
I have another question. Im doing a button which play music .wav format when click but i have error doing it. It says cannot find symbol method bMusicPlayer();
heres my codes:
class MusicPlayerListener implements ActionListener{
public void init(){
// bMusicPlayer = new Button("MUSIC PLAY");
add(bMusicPlayer);
bMusicPlayer.addActionListener(new MusicPlayerListener());
// stop = new Button(" Stop ");
//add(stop);
// stop.addActionListener(this);
audioClip = getAudioClip(getCodeBase(), "how_six_songs_collide.wav");
}
public void actionPerformed(ActionEvent ae){
Button source = (Button)ae.getSource();
if (source.getLabel() == "MUSIC PLAY "){
audioClip.bMusicPlayer(); <<<<<<<<<<<<<<<<<< Here is the error.
}
//else if(source.getLabel() == " Stop "){
//audioClip.stop();
//}
}
}
Why it kept saying that error?
- 08-14-2008, 02:15 PM #10
I can't help you because I don't know what class the audioClip object is. How is it defined?
The compiler can NOT find the method bMusicPlayer() in that class.
What class is bMusicPlayer a method in?
- 08-14-2008, 04:43 PM #11
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
What does Illegal start of expression means? Im having this error which I dont understand.
Heres my codes.
---------------------------------------------------------------------------------------------
class WinningNumbersListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(money < = 0) <<<<<< Error. Illegal start of expression
{
(lbl99 = new JLabel("Game Over!! You have $0/- left. Have a Nice Day."));
}
//handle the button pressing events
Random rand = new Random(); // do this one time in your program
int randonInt1 = rand.nextInt(45);
int randonInt2 = rand.nextInt(45);
int randonInt3 = rand.nextInt(45);
int randonInt4 = rand.nextInt(45);
int randonInt5 = rand.nextInt(45);
int randonInt6 = rand.nextInt(45);
wNum = wNum + randonInt1 + "-" + randonInt2 + "-" + randonInt3 + "-" + randonInt4 + "-" + randonInt5 + "-" + randonInt6;
lbl4.setText(wNum);
//lbl4.setFont(new Font("TimesRoman", Font.BOLD, 16));
money = money - bet;
String sMoney = "" + money;
lbl10.setText(sMoney);
wNum = "";
}
}
-------------------------------------------------------------------------------------
Really aprreciate ur help. Thank You.
- 08-14-2008, 05:58 PM #12
Where is the text of your error message? Don't edit it!!!! Copy as is.Illegal start of expression
- 08-15-2008, 08:02 AM #13
Member
- Join Date
- Aug 2008
- Posts
- 7
- Rep Power
- 0
Norm i really appreciate your replies. Ive solved the problem, there shouldnt be no spacing.
What ive type was if(money < = 0). It should be if(money<=0). And also take away the outer bracket for the next line.
Norm, I have a Question, can music in .wav format play in Applet?
- 08-15-2008, 01:39 PM #14
I think .wav is one of the formats that java can play.
- 08-31-2008, 07:46 PM #15
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
About the random numbers, I would use a loop to generate them. :\
Oh, and for God's sake, use [ code] [ /code] tagsJava Code:int[] rands = new int[amount] Random rand = new Random(); for (int i = 0; i < amount; ++i) { rands[i] = rand.nextInt(45); }I die a little on the inside...
Every time I get shot.
- 09-01-2008, 08:15 AM #16
Member
- Join Date
- Sep 2008
- Posts
- 1
- Rep Power
- 0
configure servlet class in tomcat 5.5
Hi all,
I want to know how to configure servet class in web.xml in tomcat 5.5? I am eagerly waiting for reply.
- 09-01-2008, 02:23 PM #17
Similar Threads
-
What's wrong with my codes?
By ayoood in forum New To JavaReplies: 16Last Post: 09-01-2008, 03:57 AM -
Posting codes and help
By Java_Man in forum New To JavaReplies: 2Last Post: 02-16-2008, 03:15 AM -
Getting ASCII codes of character
By gapper in forum New To JavaReplies: 1Last Post: 02-02-2008, 09:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks