Results 1 to 17 of 17
  1. #1
    sya1912 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    creating the random number generator
    You need to read the API doc.
    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.

  3. #3
    sya1912 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Where do I Place this in my codes?
    Create the Random class object in the init code. Have rand be a class variable.
    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.

  5. #5
    sya1912 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    but still theres error.
    Copy and post them here if you want help with them.

  8. #8
    sya1912 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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?

  9. #9
    sya1912 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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?

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    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?

  11. #11
    sya1912 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    Illegal start of expression
    Where is the text of your error message? Don't edit it!!!! Copy as is.

  13. #13
    sya1912 is offline Member
    Join Date
    Aug 2008
    Posts
    7
    Rep Power
    0

    Default

    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?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    I think .wav is one of the formats that java can play.

  15. #15
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    About the random numbers, I would use a loop to generate them. :\
    Java Code:
    int[] rands = new int[amount]
    Random rand = new Random();
    for (int i = 0; i < amount; ++i) {
        rands[i] = rand.nextInt(45);
    }
    Oh, and for God's sake, use [ code] [ /code] tags
    I die a little on the inside...
    Every time I get shot.

  16. #16
    Join Date
    Sep 2008
    Posts
    1
    Rep Power
    0

    Default 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.

  17. #17
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,390
    Rep Power
    25

    Default

    You should start your own thread instead of adding to an existing one.
    If you Search for web.xml you'll get examples of how to use it.

Similar Threads

  1. What's wrong with my codes?
    By ayoood in forum New To Java
    Replies: 16
    Last Post: 09-01-2008, 03:57 AM
  2. Posting codes and help
    By Java_Man in forum New To Java
    Replies: 2
    Last Post: 02-16-2008, 03:15 AM
  3. Getting ASCII codes of character
    By gapper in forum New To Java
    Replies: 1
    Last Post: 02-02-2008, 09:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •