• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Trouble with a setX method

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I am trying to draw a few card objects in a JFrame, and I set the x position using a setX method. The problem is that, even when I set the x with the method, the x can be often random, and will paint in odd places.
Main class:



Card Class:



I don't see any thing that could possibly make the setX method act like that. Any help would be appreciated. Thanks!
 
Bartender
Posts: 5585
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,

Unfortunately, I don't know what is wrong with your setX method. I find your code very hard to follow.
From a design point of view, I think a lot could be improved, but this is not your question. So let me ask you this:

Your paint(g) routine is:

What do all these "g.drawString(..) tell you what all the X-positions are?
Do you see your cards drawn in these positions?


Greetings,
Piet
 
Jose Estevez
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Piet, those drawStrings are there just to tell me the cards x position, and will be removed after I (hopefully) fix this problem. What's weird, is that the drawStrings show odd x positions, that I didn't set my cards to be drawn at, but their still drawn at that x. Here is an example:

As you can see in my code, I set the first card to be drawn at 15, but the drawstring says the x of card 1 is 315. Also, card 3 is suppost to be 65, but it seems to think it is 240. Have any ideas?

 
Piet Souris
Bartender
Posts: 5585
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jose,

well, I must say the GUI looks a lot nicer than I had anticipated! Very pleasing.

By the look of the positions of the drawStrings the coordinates seem to be oke.

In your code I do not see the drawing of the card images Can you send me the code that draws
these images? See if there's something happening to the coordinates.

 
Jose Estevez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Piet,
Sorry about that, that screenshot is from an updated code. Here is the updated card class. (kinda long).

 
Piet Souris
Bartender
Posts: 5585
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whow... indeed some lengthy code!

First, in your drawType method, I see the variable "cat". See line 164. I can't find where this variable is declared.
Your code is running nevertheless, so I must be overlooking something.

But I still haven't found the problem, unfortunately, so this is going to take some time.

In your addStuff() method of your original sending, you clearly set the xPos of all the ten cards of the hand.
Nowhere can I find some code that changes this xPos. Yet in the screenshot we see that the xPos of card 0 is 315.
The card is then drawn at that position (the spades card on the right).
So the question is: in what piece of the code does this change of xPos take place.

So I would suggest to change the setX() method of your Card class slightly:


That will not tell us from where this method is called (you should see it ten times, from the addStuff method). So if you see this message
printed more than 10 times, you'll know that from somewhere you are adjusting the xPos.

Since your blackJack class has no "main" method, you probably have some other code that creates an instance of your class.
Maybe it's somewhere in that code. Also, the xPos is not a private member, so it is possible to change xPos directly, without
calling the setX method. Can you check this out?
I must add that I wonder how you are creating an instance. You have a constructor (line 25 original code), but I also see a static metod
"create", where you instantiate the blackJack class. So, how do you create an instance?

Maybe you have some method that allows you to play a new game of blackJack, and you may put 10 new cards in your hand[].
Now, the xPos is initialized to 0, in the Card class, so you would expect, if you do indeed change your hand[], that some cards will be
drawn at position 0. Do you see that happening?

So, some homework to do.

Greetings,
Piet

 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's nothing to do with your original question, but here's a piece of advice. Never do this under any circumstances:

Jose Estevez wrote:


If something does go wrong there, and an exception is thrown, you've completely ignored it and any information it can give you. Something will fail later and you'll have no idea why. Never have an empty catch block like this - at the very least print out the stack trace if you don't want to handle it in any other way.
 
Piet Souris
Bartender
Posts: 5585
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to make a small addition. So obvious that I didn't think of it.

I wrote:
Also, the xPos is not a private member, so it is possible to change xPos directly, without
calling the setX method. Can you check this out?


Now, the obvious way is to make (at least) xPos a private member of the Card class,
so the only way to change it is by the setX method. All code trying to change it directly
will then fail to work.
 
Jose Estevez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello again

I've followed your suggestions, and added the system.out.print, as well as making xPos private. Also, I've added the Main method, which is simply this:


After this, I ran it again, and interesting things happened:


As you can see, the out.print says that its changed like its changed in the blackjack class. Yet, X1 was still changed to an odd xPos. Also, as for the cat variable, its just the color variable, I changed it from cat to color to avoid some embarassment. I cant remember why I named it cat. Perhaps my cat was near me?
 
Piet Souris
Bartender
Posts: 5585
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Getting weirder and weirder...

Certainly an interesting thing happened. X1 has the same xPos as X10,
the value of 190 of X8 has gone and has been changed to that of X9.

I looked, but I see no code whatsover that does anything like it, except
from the lines 65-74 (from the method addStuff() in your original listing).

And there you are!

Say, hand[0] contains deck[5], hand[1] contains deck[40] and hand[2] contains deck[5].

Now, when filling hand[] with deck[] cards (lines 44-61), you check whether you have any
double cards, and if you find two cards to be identical, you choose another card for hand[x]. (line 56).

Well, I say that this method is not waterproof, i.e. it does not prevent hand[] from having
double (or triple, or ... ) values. Why not? Well, suppose that we draw a 10th card, hand[9].

You then check for equality of all the 10 cards. In the final check (i = 9 j = 9) you check hand[9] with hand[9],
find equality, and you appoint another card to hand[9].

Now suppose this new card is deck[5], indeed, the same card as hand[0]! Since this is your last check,
you end up with two hand[] cards being equal, namely hand[0] and hand[9]..

We're almost there.

Now, you say: hand[0].setX = 15; That happens, we see it in the System.out screen. So what happens is that card deck[5]
gets an xPos equal to 15.

Then, in the end, we say: hand[9].setX = 240. Effectively, we say deck[5] = 240.

But hand[0] is also deck[5], so, when we print hand[0].xPos, we print deck[5].xPos, and we see 240,
in stead of the expected 15!

Subtle, isn't it?

So, the first thing to do is fix your addStuff() method, to make sure you absolutely do not have doubles in your hand[] array!
There are some handy ways to do this, but try it first.

Keep me posted!

Greetings,
Piet
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic