Results 1 to 13 of 13
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Simple Problem with Object-Oriented Programming...

    Greetings, everyone. Just as the title suggests, I'm having a small problem with this project of mine.

    I'm making a Dice program that's supposed to randomize a number from 1-6 and output a picture of a dice corresponding to the number (e.g. if number is 3, a picture of 3-dotted die should be shown). I generally have that working, but the problem is focused around my 'If' statements.

    I have two classes, as shown:
    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
     
    public class Dice
    {
     
      private ImageIcon imgDice;
      private ImageIcon imgDie1;
      private ImageIcon imgDie2;
      private ImageIcon imgDie3;
      private ImageIcon imgDie4;
      private ImageIcon imgDie5;
      private ImageIcon imgDie6;
     
      private ImageIcon img1;
     
      private Random rnd;
     
      public Dice()
      {
        imgDice = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        imgDie1 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die1.png");
        imgDie2 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die2.png");
        imgDie3 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die3.png");
        imgDie4 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die4.png");
        imgDie5 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die5.png");
        imgDie6 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die6.png");
        img1 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        rnd = new Random();
       
      }
     
      public int roll()
      {
        int num1 = rnd.nextInt(6) + 1;
        int num2 = rnd.nextInt(6) + 1;
       
        return num1;
      }
     
      public int getWidth()
      {
        int width = 105;
        return width;
      }
     
      public int getHeight()
      {
        int height = 107;
        return height;
      }
       
      public Image getDice()
      {
       
        Dice c = new Dice();
       
        if (c.roll() == 1)
        {
          img1 = imgDie1;
        }
        else if (c.roll() == 2)
        {
          img1 = imgDie2;
        }
        else if (c.roll() == 3)
        {
          img1 = imgDie3;
        }
        else if (c.roll() == 4)
        {
          img1 = imgDie4;
        }
        else if (c.roll() == 5)
        {
          img1 = imgDie5;
        }
        else if (c.roll() == 6)
        {
          img1 = imgDie6;
        }
       
        return img1.getImage();
      }
     
    }
    And here's the 2nd class:

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    
    public class DiceGame extends JFrame
    {
      public static void main (String[]args)
      {
        Dice d = new Dice();
        
        new DiceGame();
        new Dice(); 
        
        System.out.println ("You rolled a " + d.roll() + "!");
      }
      
      public DiceGame()
      {
        setTitle ("Dice Program");
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
      }
      
      public void paint (Graphics g)
      {
        Dice d = new Dice();
        super.paint(g);
        Graphics g2 = (Graphics2D) g;
      
        g2.drawImage(d.getDice(), getWidth()/2 - d.getWidth()/2, getHeight()/2 - d.getHeight()/2, this);
        
      }
      
      
    }
    Excuse my untidy programming, I haven't learned yet how to use arrays and I know some of my declarations are unnecessary or could be used in a more efficient manner.

    Moving on to the problem with the program.. I get no errors when running, but as you can see in the DiceGame class, I output a message to see what number I rolled 'System.out.println ("You rolled a " + d.roll() + "!");' And it shows the correct number. But the picture that corresponds with it is usually random. (If the output says "You rolled a 5!", the picture of the die will be a 6, for example).

    Could someone help me or explain to me why this is happening? If you need any clarifications let me know, this is my first post.
    Last edited by CuppaCoffee; 01-03-2012 at 09:30 AM.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Simple Problem with Object-Oriented Programming...

    in short: inside the main() method you make a call to d.roll() and inside getDice() you call roll() again and this will change the number. to get your dice change the method to getDice(int dice) and replace all if (c.roll() with if (dice

  3. #3
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Simple Problem with Object-Oriented Programming...

    I did so, and now I get an error in the DiceGame class on line 33: g2.drawImage(d.getDice(), getWidth()/2 - d.getWidth()/2 - 60, getHeight()/3 - d.getHeight()/2, this);

    The error says "The method getDice(int) in the type Dice is not applicable for the arguments ()". What corrections would I have to make on that line to allow it to work? Generally I want the roll() method to randomize a number, send that number to the getDice() method, which will determine which picture to generate depending on the randomized number, and I want getDice() method to send that picture to paint(Graphics g) method to output it.

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

    Default Re: Simple Problem with Object-Oriented Programming...

    Show your latest iteration of the code please.

    Also,
    • you've changed the "signature" of the getDice(...) method to now require a parameter, which means every time you call this method, your method call must match the new signature. In other words, if you've changed the method so that it takes an int parameter, have you made sure that every time you call this method you pass in an int?
    • do you save the results returned by the roll method into a variable? You'll need this number -- know why?
    • As an unrelated issue, you shouldn't draw directly in a JFrame via its paint method. For my money, the easiest way to show in ImageIcon is to simply put it into a JLabel that's already in the GUI by calling the label's setIcon(...) method.
    Last edited by Fubarable; 01-04-2012 at 12:29 AM.

  5. #5
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Simple Problem with Object-Oriented Programming...

    Quote Originally Posted by Fubarable View Post
    Show your latest iteration of the code please.
    I've come to another conclusion without the parameter. It looks like this:

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    
    public class Dice
    {
      
      private ImageIcon imgDice;
      private ImageIcon imgDie1;
      private ImageIcon imgDie2;
      private ImageIcon imgDie3;
      private ImageIcon imgDie4;
      private ImageIcon imgDie5;
      private ImageIcon imgDie6;
      
      private ImageIcon img1;
      
      private Random rnd;
      
      public Dice()
      {
        imgDice = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        imgDie1 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die1.png");
        imgDie2 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die2.png");
        imgDie3 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die3.png");
        imgDie4 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die4.png");
        imgDie5 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die5.png");
        imgDie6 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die6.png");
        img1 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        rnd = new Random();
       
      }
      
      public int roll()
      {
        int num1 = rnd.nextInt(6) + 1;
        System.out.println ("You rolled a " + num1 + "!");
        
        return num1; 
      }
      
      public int getWidth()
      {
        int width = 105;
        return width;
      }
      
      public int getHeight()
      {
        int height = 107;
        return height;
      }
       
      public Image getDice()
      {
        
        Dice c = new Dice();
        int dice = c.roll();
        
        
        if (dice == 1)
        {
          img1 = imgDie1;
        }
        else if (dice == 2)
        {
          img1 = imgDie2;
        }
        else if (dice == 3)
        {
          img1 = imgDie3;
        }
        else if (dice == 4)
        {
          img1 = imgDie4;
        }
        else if (dice == 5)
        {
          img1 = imgDie5;
        }
        else if (dice == 6)
        {
          img1 = imgDie6;
        }
        
        return img1.getImage();
      }
      
    }
    DiceGame class

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class DiceGame extends JFrame
    {
      
      public static void main (String[]args)
      {
        new DiceGame();
        Dice d = new Dice();
        
      }
      
      public DiceGame()
      {
        setTitle ("Dice Program");
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
      }
      
      public void paint (Graphics g)
      {
        Dice d = new Dice();
        super.paint(g);
        Graphics g2 = (Graphics2D) g;
      
        g2.drawImage(d.getDice(), getWidth()/2 - d.getWidth()/2 - 60, getHeight()/3 - d.getHeight()/2, this);
        
        
        Font f = new Font ("Stencil", Font.PLAIN, 20);
        FontMetrics fm = getFontMetrics(f);
        int strWidth = fm.stringWidth("Click ROLL to begin!");
        g2.setFont(f);
        g2.drawString ("Click ROLL to begin!", getWidth()/2 - strWidth/2, 270);
        
      }
    }
    Other than some minor additions to the paint method, I've moved the System.out.println from the main method of the DiceGame class and put it into the roll() method of the Dice class. I've also declared an int variable called dice in the getDice() class which equals c.roll(). I get no errors when I compile this.. and it generally runs OK. The only issue now is that it runs 2-3 times.. Example, when I click run, I'll get something like this

    You rolled a 6!
    You rolled a 2!
    You rolled a 3!
    The images on the frame also change to the picture corresponding to the number (which is a good start). So the only problem I'm having now is how many times the roll() method is called.. Any way to correct it to make sure it only outputs once?


    Also,
    • As an unrelated issue, you shouldn't draw directly in a JFrame via its paint method. For my money, the easiest way to show in ImageIcon is to simply put it into a JLabel that's already in the GUI by calling the label's setIcon(...) method.
    Thanks for your 2 cents, Fubarable. I'll be sure to take that into account after I manage these small problems
    Last edited by CuppaCoffee; 01-04-2012 at 01:07 AM.

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

    Default Re: Simple Problem with Object-Oriented Programming...

    You've changed your getDice() back to accepting no parameter! Don't do that since the first advice was good.

    You should have this method accept an int and return the corresponding image for that int.
    Last edited by Fubarable; 01-04-2012 at 01:09 AM.

  7. #7
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Simple Problem with Object-Oriented Programming...

    If that advice was good then I'll try sticking with it..

    I've re-added the parameter to the method now to this:

    Java Code:
    public Image getDice(int dice)
      {
        
        Dice c = new Dice();
        dice = c.roll();
        
        
        if (dice == 1)
        {
          img1 = imgDie1;
        }
        else if (dice == 2)
        {
          img1 = imgDie2;
        }
        else if (dice == 3)
        {
          img1 = imgDie3;
        }
        else if (dice == 4)
        {
          img1 = imgDie4;
        }
        else if (dice == 5)
        {
          img1 = imgDie5;
        }
        else if (dice == 6)
        {
          img1 = imgDie6;
        }
        
        return img1.getImage();
      }
    When I compile the Dice class, it's fine, but then I come back to my previous problem in the DiceGame class. Originally in the paint method I have this:

    Java Code:
    g2.drawImage(d.getDice(), getWidth()/2 - d.getWidth()/2 - 60, getHeight()/3 - d.getHeight()/2, this);
    To which I got the error "Error: The method getDice(int) in the type Dice is not applicable for the arguments ()". Now you're telling me I've changed the "signature" of my getDice(...) method, and anything else involving that method must be changed, like so. If I'm doing what you said correctly, then when calling the getDice method, the same code should look like this?

    Java Code:
    g2.drawImage(d.getDice(dice), getWidth()/2 - d.getWidth()/2 - 60, getHeight()/3 - d.getHeight()/2, this);
    Only problem now is that "dice cannot be resolved to a variable", meaning Java can't recognize that integer within the DiceGame class. How could I fix that? I also apologize in advance for so many problems arising that I should be able to fix..

    edit: Could I get some example code as to how to call a method with a parameter?
    Last edited by CuppaCoffee; 01-04-2012 at 01:23 AM.

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

    Default Re: Simple Problem with Object-Oriented Programming...

    OK, I'm going to change my recommendation (sorry), but regardless, all of our recommendations may need to be thrown out depending on what your actual requirements state (and you'll want to post them here verbatim).

    First of all, Dice represents two Die, and I think that you should subdivide things further, that you should create a Die class, one that has an int to represent its value, and one that returns an image that represents the appropriate image for the Die's current value. So this class would have all the necessary Images as you're currently doing, should have a roll() method that randomizes the value held by the Die object, should have a getValue() method that returns this value and a getImage() method that takes no parameter but that returns the image that corresponds to the Die's current value.

    Then you could give your main method two Die objects, call roll() on them, get their values by calling getValue() on them, and then get each of their images by calling getImage() on them. This seems the most "OOP" way of doing this.

    For further refinement, the images should be "constants" meaning that they're held in final static variables that are initialized in a static initializer block. This last part isn't 100% necessary, but would make the most sense to me.

  9. #9
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Simple Problem with Object-Oriented Programming...

    Yeah, I know Dice consists of two die. I was planning one working on one dice to test if it works (to which I came here for help) and when I succeeded in so, I'd modify the program to work with two die. I was thinking of having two roll() methods and two getDice() methods to randomize two numbers and calling two images (that's why in my first post, under the roll() method, there are two integers, num1 and num2).

    I'm trying not to create another class all together because I'm trying to follow the requirements and format in this PDF file:

    http://www.dpcdsb.org/NR/rdonlyres/C...Dice_Class.pdf

  10. #10
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Simple Problem with Object-Oriented Programming...

    I figured out the problem (or rather I came to a new conclusion).

    Instead of dealing with all the hooblah of my previous problems.. I've declared num1 as a public static integer in the field of the Dice class... Here's my latest code. You'll notice I went ahead and added two new methods; roll2() and getDice2(). Fubarable, what you said is generally correct, I was going to need to roll 2 die, but I was planning on working with one dice first and check if it worked (to which I came to you guys for help). Then I was going to modify the program to work with two die. I'd want to refrain from making a whole new class because the requirements I'm working with don't instruct me to do so. Anyways, here's the two updated classes:

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    
    public class Dice
    {
      
      private ImageIcon imgDice;
      private ImageIcon imgDie1;
      private ImageIcon imgDie2;
      private ImageIcon imgDie3;
      private ImageIcon imgDie4;
      private ImageIcon imgDie5;
      private ImageIcon imgDie6;
      
      private ImageIcon img1;
      private ImageIcon img2;
      
      public static int num1;
      public static int num2;
      
      private Random rnd;
      
      public Dice()
      {
        imgDice = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        imgDie1 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die1.png");
        imgDie2 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die2.png");
        imgDie3 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die3.png");
        imgDie4 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die4.png");
        imgDie5 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die5.png");
        imgDie6 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die6.png");
        img1 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        img2 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        rnd = new Random();
        
      }
      
      public int roll()
      {
        num1 = rnd.nextInt(6) + 1;
       
        return num1; 
      }
      
      public int roll2()
      {
        num2 = rnd.nextInt(6) + 1;
        
        return num2;
      }
      
      public int getWidth()
      {
        int width = 105;
        return width;
      }
      
      public int getHeight()
      {
        int height = 107;
        return height;
      }
       
      public Image getDice()
      {
        int dice = num1;
        
        if (dice == 1)
        {
          img1 = imgDie1;
        }
        else if (dice == 2)
        {
          img1 = imgDie2;
        }
        else if (dice == 3)
        {
          img1 = imgDie3;
        }
        else if (dice == 4)
        {
          img1 = imgDie4;
        }
        else if (dice == 5)
        {
          img1 = imgDie5;
        }
        else if (dice == 6)
        {
          img1 = imgDie6;
        }
        
        return img1.getImage();
      }
      
      public Image getDice2()
      {
        int dice2 = num2;
        
        if (dice2 == 1)
        {
          img2 = imgDie1;
        }
        else if (dice2 == 2)
        {
          img2 = imgDie2;
        }
        else if (dice2 == 3)
        {
          img2 = imgDie3;
        }
        else if (dice2 == 4)
        {
          img2 = imgDie4;
        }
        else if (dice2 == 5)
        {
          img2 = imgDie5;
        }
        else if (dice2 == 6)
        {
          img2 = imgDie6;
        }
        
        return img2.getImage();
      }
     
      
    }
    and the DiceGame class...

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class DiceGame extends JFrame
    {
      public static void main (String[]args)
      {
        new DiceGame();
        Dice d = new Dice();
        
        System.out.println ("You rolled a " + d.roll() + "!");
        System.out.println ("You rolled a " + d.roll2() + "!");
        
      }
      
      public DiceGame()
      {
        setTitle ("Dice Program");
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
      }
      
      public void paint (Graphics g)
      {
        Dice d = new Dice();
        
        Graphics g2 = (Graphics2D) g;
      
        g2.drawImage(d.getDice(), getWidth()/2 - d.getWidth()/2 - 60, getHeight()/3 - d.getHeight()/2, this);
        g2.drawImage(d.getDice2(), getWidth()/2 - d.getWidth()/2 + 60, getHeight()/3 - d.getHeight()/2, this);
        
        Font f = new Font ("Stencil", Font.PLAIN, 20);
        FontMetrics fm = getFontMetrics(f);
        int strWidth = fm.stringWidth("Click ROLL to begin!");
        g2.setFont(f);
        g2.drawString ("Click ROLL to begin!", getWidth()/2 - strWidth/2, 270);
        
      }
    }
    What the first guy to reply to this thread said was correct. I was calling on the d.roll() method too many times, which resulted in me getting a different number. Now, I only call on the d.roll() method once (located in the main method of the DiceGame class) and everything works out just fine. The program outputs both results of both die ONCE.

    Example;

    You rolled a 5! <--- value of num1
    You rolled a 1! <--- value of num2
    Also, lo and behold, the images correspond to the numbers outputted, meaning everything is working fine. What happened was I gave up on the program (don't scold me!) and decided to work ahead in my learning. The next lesson was talking about static fields and methods and that's when I decided, maybe I should take another look at my code with what I've just learned, and it's exactly what I've wanted to do to begin with. I wanted to get the values of num1 without having to use d.roll() in the getDice() class.

    Thank you everyone for all the help.. Now that I've gone through the major problems, I can finally work and complete this program and buff out all those scratches. Rest assured.. if I run into any more problems with this project, I surely won't hesitate to ask for more guidance
    Last edited by CuppaCoffee; 01-04-2012 at 06:57 AM.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Simple Problem with Object-Oriented Programming...

    Using static - except for the main() method - is almost certainly wrong. Not that I've read your code at all closely, but I don't need to to make that observation. Sorry if that comes across a bit arrogant, but static is wrong. Really.

    No-one seems to have mentioned it, but you have:

    Java Code:
    public void paint (Graphics g)
      {
        Dice d = new Dice();
        // do some stuff with d
        // ...
    When your GUI paints itself it begins by creating a new dice. That dice has nothing to do with the one in main() about which you printed the messages. So it's little wonder that they don't agree. (They wouldn't agree unless there were some cosmic synchronicity between dice, or cheating on the part of the casino. It is precisely such synchronicity/cheating that is performed by "static".)

    Another approach: provide DiceGame with a member variable representing the dice. Initialise this and roll() it in the DiceGame constructor (and print the message there if you really want to check.)

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
     
    public class DiceGame extends JFrame
    {
      private Dice theDice;  // <-- the dice "belong" to the game
    
      public static void main (String[]args)
      {
        new DiceGame();
        //Dice d = new Dice();  // <-- not to the main() method
         
        //System.out.println ("You rolled a " + d.roll() + "!");
        //System.out.println ("You rolled a " + d.roll2() + "!");
         
      }
       
      public DiceGame()
      {
        theDice = new Dice();       // <-- the game's constructor initialises things
        System.out.println ("You rolled a " + theDice.roll() + "!");
        System.out.println ("You rolled a " + theDice.roll2() + "!");
    
        setTitle ("Dice Program");
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
      }
       
      public void paint (Graphics g)
      {
        //Dice d = new Dice(); // <-- paint() is for painting, not creating dice
         
        Graphics g2 = (Graphics2D) g;
       
        g2.drawImage(theDice.getDice(), getWidth()/2 - theDice.getWidth()/2 - 60, getHeight()/3 - theDice.getHeight()/2, this);
        g2.drawImage(theDice.getDice2(), getWidth()/2 - theDice.getWidth()/2 + 60, getHeight()/3 - theDice.getHeight()/2, this);
         
        Font f = new Font ("Stencil", Font.PLAIN, 20);
        FontMetrics fm = getFontMetrics(f);
        int strWidth = fm.stringWidth("Click ROLL to begin!");
        g2.setFont(f);
        g2.drawString ("Click ROLL to begin!", getWidth()/2 - strWidth/2, 270);
         
      }
    }
    Last edited by pbrockway2; 01-04-2012 at 10:13 AM. Reason: couple of typos

  12. #12
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Simple Problem with Object-Oriented Programming...

    Hm, you're quite right. I remember reading in another thread to avoid using static variables for such things, and I was growing rather tired of this program and just wanted to get it finished.. but I've updated my code to what you have suggested and it works just as fine. The updated code looks like so:

    Java Code:
    import javax.swing.*;
    import java.util.*;
    import java.awt.*;
    
    public class Dice
    {
      
      private ImageIcon imgDice;
      private ImageIcon imgDie1;
      private ImageIcon imgDie2;
      private ImageIcon imgDie3;
      private ImageIcon imgDie4;
      private ImageIcon imgDie5;
      private ImageIcon imgDie6;
      
      private ImageIcon img1;
      private ImageIcon img2;
      
      public  int num1;
      public  int num2;
      
      private Random rnd;
      
      public Dice()
      {
        imgDice = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        imgDie1 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die1.png");
        imgDie2 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die2.png");
        imgDie3 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die3.png");
        imgDie4 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die4.png");
        imgDie5 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die5.png");
        imgDie6 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die6.png");
        img1 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        img2 = new ImageIcon ("C:\\Users\\user\\Desktop\\Dr Java~\\Images\\die0.png");
        rnd = new Random();
        
      }
      
      public int roll()
      {
        num1 = rnd.nextInt(6) + 1;
       
        return num1; 
      }
      
      public int roll2()
      {
        num2 = rnd.nextInt(6) + 1;
        
        return num2;
      }
      
      public int getWidth()
      {
        int width = 105;
        return width;
      }
      
      public int getHeight()
      {
        int height = 107;
        return height;
      }
       
      public Image getDice()
      {
        int dice = num1;
        
        if (dice == 1)
        {
          img1 = imgDie1;
        }
        else if (dice == 2)
        {
          img1 = imgDie2;
        }
        else if (dice == 3)
        {
          img1 = imgDie3;
        }
        else if (dice == 4)
        {
          img1 = imgDie4;
        }
        else if (dice == 5)
        {
          img1 = imgDie5;
        }
        else if (dice == 6)
        {
          img1 = imgDie6;
        }
        
        return img1.getImage();
      }
      
      public Image getDice2()
      {
        int dice2 = num2;
        
        if (dice2 == 1)
        {
          img2 = imgDie1;
        }
        else if (dice2 == 2)
        {
          img2 = imgDie2;
        }
        else if (dice2 == 3)
        {
          img2 = imgDie3;
        }
        else if (dice2 == 4)
        {
          img2 = imgDie4;
        }
        else if (dice2 == 5)
        {
          img2 = imgDie5;
        }
        else if (dice2 == 6)
        {
          img2 = imgDie6;
        }
        
        return img2.getImage();
      }
      
      public int getValue()
      {
        int value = num1 + num2;
        
        return value;
      }
      
    }
    DiceGame class

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    
    public class DiceGame extends JFrame
    {
      
      private Dice d;
      
      public static void main (String[]args)
      {
        new DiceGame();  
      }
      
      public DiceGame()
      {
        
        d = new Dice();
        
        System.out.println ("You rolled a " + d.roll() + "!");
        System.out.println ("You rolled a " + d.roll2() + "!");
        System.out.println ("That's a total of " + d.getValue() + ".");
        
        setTitle ("Dice Program");
        setSize(400, 300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
      }
      
      public void paint (Graphics g)
      {
    
        Graphics g2 = (Graphics2D) g;
      
        g2.drawImage(d.getDice(), getWidth()/2 - d.getWidth()/2 - 60, getHeight()/3 - d.getHeight()/2, this);
        g2.drawImage(d.getDice2(), getWidth()/2 - d.getWidth()/2 + 60, getHeight()/3 - d.getHeight()/2, this);
        
        Font f = new Font ("Stencil", Font.PLAIN, 20);
        FontMetrics fm = getFontMetrics(f);
        int strWidth = fm.stringWidth("Click ROLL to begin!");
        int strWidth2 = fm.stringWidth("You rolled a " + d.getValue() + "!");
        g2.setFont(f);
        g2.drawString ("Click ROLL to begin!", getWidth()/2 - strWidth/2, 270);
        
        g2.drawString ("You rolled a " + d.getValue() + "!", getWidth()/2 - strWidth2/2, 250);
        
      }
    }
    So the real problem was where I was initializing my declarations.. Precisely num1, num2, and Dice d. I put them inside their methods instead of adding them to the field.. When I did so it couldn't be read in other methods, so it resulted in me having to call the class to get the variable (what I did with the getDice()).. All that trouble just because of incorrect initializing. Silly mistake

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: Simple Problem with Object-Oriented Programming...

    I'm glad you've got it sorted out. Yes, I thought it would be better if you got to the bottom of the problem even if you had working code.

Similar Threads

  1. Replies: 1
    Last Post: 08-04-2011, 10:22 AM
  2. New to object-oriented
    By fluteattack in forum New To Java
    Replies: 6
    Last Post: 03-28-2011, 02:43 PM
  3. Help in object oriented programming .
    By renu in forum New To Java
    Replies: 18
    Last Post: 10-26-2010, 08:57 PM
  4. Object Oriented
    By GraemeH in forum New To Java
    Replies: 3
    Last Post: 03-29-2009, 08:50 PM
  5. Object- Oriented guide?
    By sciguy77 in forum New To Java
    Replies: 4
    Last Post: 01-18-2009, 04:33 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
  •