Help With Class Project


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help With Class Project

  1. #1
    Join Date
    Jan 2006
    Posts
    4

    Help With Class Project

    For a class project, I need to write code for a simple game. I picked Match Sticks. However, I have a few general problems. When I use JOptionPane.showInputDialog, It goes into an unending loop. What am I doing wrong? Here's the code for the game as I've written it so far. Also, the background color won't work.

    import java.awt.*;
    import java.applet.*;
    import javax.swing.JOptionPane;

    public class Matchstix extends java.applet.Applet
    {
    public void setBackground(Color blue)
    {
    }

    public void drawMatchstix(Graphics g, int num)
    {

    int xcoordr=20;
    int xcoordo=18;
    for(int k = 1; k <= num; k++)
    {
    g.setColor(Color.yellow);
    g.fillRect(xcoordr,20,5,105);
    g.setColor(Color.red);
    g.fillOval(xcoordo,20,9,11);
    xcoordr +=20;
    xcoordo +=20;
    }
    }

    public void paint(Graphics g)
    {
    setBackground(Color.blue);
    {

    String nummatch = JOptionPane.showInputDialog("How many matches do you want to paly with?:");
    int nmatch = Integer.parseInt(nummatch);
    drawMatchstix(g, nmatch);
    }

    }
    }


    Any help or thoughts you can give me, would be greatly appreciated.
    Last edited by MRied; 05-23-2006 at 07:15 AM.

  2. #2
    Join Date
    Mar 2006
    Posts
    35
    I'm not really sure how the matchstick game is played but as far as drawing them on the screen you could use this.

    import java.awt.*;
    import javax.swing.JApplet;
    import javax.swing.JOptionPane;

    public class Matchstix extends JApplet implements Runnable
    {

    int xcoordr=20;
    int xcoordo=18;
    int nmatch = 0;
    String nummatch;
    public void init()
    {
    setBackground(Color.blue);

    nummatch = JOptionPane.showInputDialog("How many matches do you want to paly with?:");
    nmatch = Integer.parseInt(nummatch);


    }

    public void start()
    {

    }

    public void run()
    {



    }




    public void paint(Graphics g)
    {

    for(int k = 1; k <= nmatch; k++)
    {

    xcoordr +=20;
    xcoordo +=20;
    g.setColor(Color.blue);
    g.setColor(Color.yellow);
    g.fillRect(xcoordr,20,5,105);
    g.setColor(Color.red);
    g.fillOval(xcoordo,20,9,11);

    }

    xcoordr = 20;
    xcoordo = 18;


    }
    }

    There is more than one way to code that but it works just as good as the others unless you need to do animation then your going to have to use threads.

  3. #3
    Join Date
    Mar 2006
    Posts
    35
    I forgot to answer you orginal question what you were doing wrong was using your JOptionPane in the wrong section of your code. The way you used it was to initialize a variable and in applets anything that needs to be initialized by user input before the program starts should be done in the init() method. Same thing for the latter.

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2006, 05:20 AM
  2. Alarm class
    By Aristo in forum Java
    Replies: 6
    Last Post: 12-05-2005, 12:07 PM
  3. Replies: 0
    Last Post: 09-21-2005, 05:33 PM
  4. Replies: 0
    Last Post: 09-13-2000, 11:18 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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center