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]
Re: Why doesnt my constructor work?
Code :
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.
Re: Why doesnt my constructor work?
Quote:
Originally Posted by
pbrockway2
Code :
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
Re: Why doesnt my constructor work?
Quote:
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:
Code java:
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;
Re: Why doesnt my constructor work?
Thanks, it works & I understand it too !
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.
Re: Why doesnt my constructor work?
Quote:
Originally Posted by
veeer
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.
Re: Why doesnt my constructor work?
Special for veeer,
Code java:
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?
Re: Why doesnt my constructor work?
Quote:
Originally Posted by
Voodoo
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
Special for veeer,
Code java:
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...
Quote:
...
- ...
- ...
- 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.
- 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.
- 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)
Code java:
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.
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.
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.
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:
Quote:
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:
Quote:
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.
Code java:
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
Quote:
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!
Re: Why doesnt my constructor work?
I feel a lock coming on...
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.