Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Why doesnt my constructor work?

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Why doesnt my constructor work?

    Hi

    I'm new to Java & am working through the Dietels' "Java How to Program" and am stuck on exercise 6.1 (I'm not doing a formal course)
    I managed to get the bulls eye required but I can't seem to get my constructor to 'work' - one of the circles should be red the colours when I create a DrawBullseye object but it defaults to black.

    Any suggestions gratefully appreciated


    [highlight = JAVA]
    import java.util.Random;
    import javax.swing.JFrame;

    public class DrawSmileyTest
    {

    public static void main(String[] args)
    {
    DrawBullseye target = new DrawBullseye(200,0,0);
    JFrame frame = new JFrame();

    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
    frame.add(target);
    frame.setSize( 250,250);
    frame.setVisible(true);
    }

    }
    [/highlight]

    [highlight = Java]
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import java.util.Random;

    public class DrawBullseye extends JPanel
    {
    int counter = 0;

    int red1;
    int green1;
    int blue1;

    int red2 = 99;
    int green2 = 182;
    int blue2 = 222;


    public DrawBullseye(int r1, int g1, int b1)
    {
    red1 = r1;
    green1 = g1;
    blue1 = b1;
    }
    Color firstColour = new Color(red1, green1, blue1);
    Color secondColour = new Color (red2, green2, blue2);

    public void paintComponent (Graphics g)

    {
    super.paintComponent(g);
    for (int l = 1; l<=10; l++)
    {
    int step = 10;
    g.setColor(firstColour);
    g.fillOval( 10 + (counter*step), 10 + (counter*step), 200 - (counter*2*step), 200 - (counter*2*step) );

    g.setColor(secondColour);
    g.fillOval ( 15 + (counter*step), 15 + (counter*step), 200 - (counter*2*step) - 10, 200 - (counter*2*step) - 10 );


    counter++;
    }
    }
    }
    [/highlight]


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Why doesnt my constructor work?

    Color firstColour = new Color(red1, green1, blue1);

    You can't just put this line after the constructor and expect it to happen after the constructor is called.

    In general the order of things doesn't matter much. If you want to set the value of firstColor based on the values of red1, green1 and blue1 then do that as part of the constructor.

    Of course firstColor has to be declared outside the constructor so you can use it elsewhere in the painting method. It is common to group all the declarations together.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    hairLess (July 23rd, 2012)

  4. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Why doesnt my constructor work?

    Quote Originally Posted by pbrockway2 View Post
    Color firstColour = new Color(red1, green1, blue1);

    In general the order of things doesn't matter much. If you want to set the value of firstColor based on the values of red1, green1 and blue1 then do that as part of the constructor.
    Thank you for your reply. I moved the Color declarations to the paint component method and it did work !

    Unfortunately (for me) I don't understand why. I did want set the value of the firstColour in the constructor (having declared firstColour outside the constructo - how do I do that? I feel like I'm missing something really fundamental here...

    thanks in advance

  5. #4
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Why doesnt my constructor work?

    Thank you for your reply. I moved the Color declarations to the paint component method and it did work !

    Unfortunately (for me) I don't understand why. I did want set the value of the firstColour in the constructor (having declared firstColour outside the constructo - how do I do that? I feel like I'm missing something really fundamental here...

    thanks in advance
    Your algorithm is wrong, that's the reason. The Constructor is invoked but the firstColour and secondColour were already instantiated. So, they "ignore" what you set in your constructor. Compare them with the int-variables you'll see the similarity. Your constructor should be as following:
    public DrawBullseye(int r1, int g1, int b1) {
            red1 = r1;
            green1 = g1;
            blue1 = b1;
            firstColour = new Color(red1, green1, blue1);
            secondColour = new Color (red2, green2, blue2);
    }
    Color firstColour, secondColour;

  6. The Following User Says Thank You to Voodoo For This Useful Post:

    hairLess (July 24th, 2012)

  7. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Why doesnt my constructor work?

    Thanks, it works & I understand it too !

  8. #6
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Why doesnt my constructor work?

    I don't quite know what Voodoo is on about, considering your 'algorithm' (what algorithm?) is not wrong -- I'm not sure quite what he means when he speaks of 'ignoring' what is passed in the constructor. The reality is that you're merely recreating the Color objects every paint with the same components each time, so they turn out to be the same. This is pointless and can be replaced by initializing in the constructor, but both will have the same effect -- though not as Voodoo explained.
    Last edited by veeer; July 24th, 2012 at 10:03 PM.

  9. #7
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Why doesnt my constructor work?

    Quote Originally Posted by veeer View Post
    I don't quite know what Voodoo is on about, considering your 'algorithm' (what algorithm?) is not wrong -- I'm not sure quite what he means when he speaks of 'ignoring' what is passed in the constructor. The reality is that you're merely recreating the Color objects every paint with the same components each time, so they turn out to be the same. This is pointless and can be replaced by initializing in the constructor, but both will have the same effect -- though not as Voodoo explained.
    Wenn Du nichts verstehst, kannst Du beim Admin beschweren. If you don't understand you can complain at the admin.

  10. #8
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Why doesnt my constructor work?

    Special for veeer,
    int red1;
    int green1; 
    int blue1;
     
    int red2 = 99;
    int green2 = 182;
    int blue2 = 222;
     
     
    public DrawBullseye(int r1, int g1, int b1)
    {
         red1 = r1;
         green1 = g1;
         blue1 = b1;
    }
    Color firstColour = new Color(red1, green1, blue1);
    ....
    DrawBullseye target = new DrawBullseye(200,0,0);
    Can you tell the forum what firstColour could be after the constructor DrawBullseye(200,0,0) is instanstiated? 0,0,0 or 200,0,0?

  11. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Why doesnt my constructor work?

    Quote Originally Posted by Voodoo View Post
    Wenn Du nichts verstehst, kannst Du beim Admin beschweren. If you don't understand you can complain at the admin.
    I believe you still are unable to understand the situation. You gave an incorrect explanation which I fully understood, thus being able to point out that it was incorrect. I didn't mean any hard feelings, so I apologize if you took what I said as an attack :p

    Quote Originally Posted by Voodoo View Post
    Special for veeer,
    int red1;
    int green1; 
    int blue1;
     
    int red2 = 99;
    int green2 = 182;
    int blue2 = 222;
     
     
    public DrawBullseye(int r1, int g1, int b1)
    {
         red1 = r1;
         green1 = g1;
         blue1 = b1;
    }
    Color firstColour = new Color(red1, green1, blue1);
    ....
    DrawBullseye target = new DrawBullseye(200,0,0);
    Can you tell the forum what firstColour could be after the constructor DrawBullseye(200,0,0) is instanstiated? 0,0,0 or 200,0,0?
    Obviously Color[r=0, g=0, b=0]... that does not change the fact that you are being misleading with talking about 'ignoring' anything. It's simply evaluation order during initialization of an object. See the relevant part of the Java Language Specification...
    ...
    1. ...
    2. ...
    3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
    4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5.
    5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

    ...
    Here's how it compiles to (output from jad -sjava -t2 -nonlb -b -- i'd have shown javap -c, but bytecode is unnecessarily low-level here)
      public DrawBullseye(int i, int j, int k) {
        red2 = 99;
        green2 = 182;
        blue2 = 222;
        firstColour = new Color(red1, green1, blue1);
        red1 = i;
        green1 = j;
        blue1 = k;
      }

    You also neglected to mention why it worked when placing the initializer in paintComponent(), which is why I posted in the first place. Hopefully all of your questions have been answered.
    Last edited by veeer; July 25th, 2012 at 08:07 AM.

  12. #10
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Why doesnt my constructor work?

    Aha,
    firstColour = new Color(red1, green1, blue1); is BEFORE red1 = i, etc. That is, my dear schoolmarm !
    Or should I call you an uber-schoolteacher? I never intend to write a java book. My answer is usually very short, sometime rude. But I never try to "schoolmarm" anyone, and never demand anyone to stop posting. Are you the owner of this forum? By the way, I never post for a help.

  13. #11
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Why doesnt my constructor work?

    You should consider calming down, and re-evaluate your situation. I apologize for suggesting you quit posting, but I was unsure of how to respond to your German equivalent of "fuck off". I also apologize if I came off as a condescending schoolmarm to you, but posts with ample evidence and support from authoritative (and thus very useful) resources shouldn't be considered the equivalent of being a schoolmarm, but rather be considered model ideal ones, what every post should be like... I think... Other than that, I don't personally care whether you post for help or not, as there's no shame in seeking help (who doesn't google things daily? :p). I was merely pointing out that you should prefer to be clear and avoid ambiguous terminology, remaining strictly true as to preserve the integrity of this forum.

    P.S. Yes, of course that happens in that order... hence I linked to the JLS. There one can learn everything (and more) that they seek to know about both the language syntax and semantics. I still don't see the reason for your mentioning of 'algorithm', being a vague word, but I'll accredit that to our language barrier, friend.
    Last edited by veeer; July 25th, 2012 at 08:43 AM.

  14. #12
    Member
    Join Date
    Jul 2012
    Posts
    119
    Thanks
    0
    Thanked 19 Times in 19 Posts

    Default Re: Why doesnt my constructor work?

    Man, still playing a sniffy schoolmarm,
    why should I have to calm down when I'm not "angry"? It's your verbal language that makes you thinking that people are angry. You read something and decide to do something against it without having to understand what was written.
    First:
    Wenn Du nichts verstehst, kannst Du beim Admin beschweren. If you don't understand you can complain at the admin.
    It's so obvious that the English sentence is the translation....only bad people think of evil idea.
    Second:
    Your algorithm is wrong, that's the reason. The Constructor is invoked but the firstColour and secondColour were already instantiated. So, they "ignore" what you set in your constructor.
    public DrawBullseye(int r1, int g1, int b1) {
            red1 = r1;
            green1 = g1;
            blue1 = b1;
            firstColour = new Color(red1, green1, blue1);
            secondColour = new Color (red2, green2, blue2);
    }
    Color firstColour, secondColour;
    What algorithm I talked about, huh? What is an algorithm? Read carefully this passage
    More precisely, an algorithm is an effective method expressed as a finite list[1] of well-defined instructions[2] for calculating a function.[3] Starting from an initial state and initial input (perhaps empty),[4] the instructions describe a computation that, when executed, will proceed through a finite [5] number of well-defined successive states, eventually producing "output"[6] and terminating at a final ending state ...Algorithm - Wikipedia, the free encyclopedia.
    So, why the steps of the above-mentioned code is not an algorithm and where is the problem?
    Sorry, pal. Time-out and game over!

  15. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Why doesnt my constructor work?

    I feel a lock coming on...

  16. #14
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Why doesnt my constructor work?

    This discussion is going absolutely nowhere, and some of the phrases used can be construed as personal insults, which is not tolerated on this forum. I am locking this thread before it truly spirals out of control. Lets keep things civil and not take things so personally.

Similar Threads

  1. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  2. excuse me, can somebody explain how this constructor work? thanks before
    By erdy_rezki in forum Java Theory & Questions
    Replies: 6
    Last Post: April 19th, 2012, 12:27 AM
  3. [SOLVED] (Beginner) Program doesnt work
    By moneyman021 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: January 15th, 2012, 04:28 AM
  4. HI, could someone please tell me why my code doesnt work?
    By joelmeler in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 3rd, 2011, 01:37 AM
  5. my menu doesnt work can u tell me whats wrong
    By claymore in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 8th, 2010, 04:16 AM