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
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# Paint Method Puhhlease Help

Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
Hi all,

I am in a dilemma in synchronizing my answers. As some of u might know my program is to convert binary/octal numbers to decimal. I have already done the conversion. However, as part of my program i would also need to display the positional weightage of each digit of the number.

I used the paint method to display the positional weightage for example(Binary number: 1001) :
......................................................................1...... .......... 0..............0................1

..16............................5................4.................3........... .....2..............1...............0
2...............................2...............2................2............. ....2..............2...............2

This is what my program is getting regardles of how many numbers i key in 1,10,100,10000001 etc. I don't want it to print up to 2^16 unless of course my number has 17 digits.Another problem is i am using
another method to set the Text of the binary/octal number as i am unable to use paint method (it keeps on giving me answer 0 for my conversion). So the positioning of
the numbers is wayyy off.

Basically i want it to be like this :
1................0...............0................1

.3..................2...............1................0
2..................2..............2................2
or another example:

1....................0.............1..............1................1........... .......1.................0..................1

..7...................6............5................4...............3.......... ........2................1...................0
2...................2.............2...............2................2........... ......2.................2..................2

Appreciate it if you do not repost the entire code. Just parts of it that needs correction.

p/s : ignore the dots.

[ February 15, 2005: Message edited by: Alisha Burke ]

[ February 15, 2005: Message edited by: Alisha Burke ]

[ February 15, 2005: Message edited by: Alisha Burke ]

[ February 15, 2005: Message edited by: Alisha Burke ]
[ February 15, 2005: Message edited by: Alisha Burke ]

Ranch Hand
Posts: 51
• Number of slices to send:
Optional 'thank-you' note:
Cool... Now to start looking at it...

Alisha Burke
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
Hi Liam,

Appreciate it if you do not post the entire code or repost my code for that matter just parts that needs correction Thankx.

Liam Tiarnach
Ranch Hand
Posts: 51
• Number of slices to send:
Optional 'thank-you' note:
Not a problem...

Okay here we go, now that I can see what is going on...

first we will introduce a new method to build our weighted answer display...

I believe I had put enough comments for you to understand so I will just explain the one thing that might just jump out at you...
this piece...

I think it's called a "shortcut if-else statement" which has this syntax...

it would be the same if I had done this...

the next change is in this part, the paint method...

we call the answerDisplayBuilder() method and pass it our Graphics g so that the answerDisplayBuilder method can build our weighted answer display...
and the last modification is in the actionPerformed() method...

after we do the numberkeyinLabel.setText() method, we call repaint to, well, repaint our graphics...

So with these modification done, you binary number display should work properly...

later you might want to modify the answerDisplayBuilder() method to have your weighted display self center...
and it to have it display either your binary or octal weighted number...
i.e. instead of using the string literal "2" in...

you can use a String variable that can be set to the base number that you want to display...
i.e.

Have fun...

Ranch Hand
Posts: 624
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Liam Tiarnach:
I think it's called a "shortcut if-else statement" which has this syntax...

In the The Java Language Specification (JLS), it is simply referred to as the "Conditional Operator ?:", or "?:" for short. Details about it and its usage are available in section 15.25.

It is also often referred to as "The Ternary Operator", although it is technically only a type of a ternary operator.

It is a powerful operator, but can be confusing for beginners. Even some advanced developers avoid it since it can make code a bit more muddled in its reading. Some discussion on the topic is available in this thread.

Alisha Burke
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
Thanks Liam!

Its working perfectly except for the fact that the positioning of the powers is going from 0,1,2,3 to 16. And i need it backwards 16,15,14.......3,2,1,0.
I know i need to edit some parts of the code in the for loop to achieve this so I am working on it and i'll post some more if i need help.

Thankx again.

Liam Tiarnach
Ranch Hand
Posts: 51
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Alisha Burke:
Thanks Liam!

Its working perfectly except for the fact that the positioning of the powers is going from 0,1,2,3 to 16. And i need it backwards 16,15,14.......3,2,1,0.
I know i need to edit some parts of the code in the for loop to achieve this so I am working on it and i'll post some more if i need help.

Thankx again.

hehe... and sure enough it is backwards... I was working with two different versions of the same program, Testing and Testing1...

in the buildAnswerDisplay() method, where that loop is...
check this line...

as can be seen, int i counts up... and thats were I goofed
it should be...

since j counts down...

I must admit, that was a good goof up on my part...

Have fun...

Alisha Burke
Greenhorn
Posts: 27
• Number of slices to send:
Optional 'thank-you' note:
Goof or no goof I appreciate you helping me

Liam Tiarnach
Ranch Hand
Posts: 51
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Alisha Burke:
Goof or no goof I appreciate you helping me

Thank you...

 The glass is neither half full or half empty. It is too big. But this tiny ad is just right: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising