This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

g.drawString is not printing the correct value in JPanel  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
g.drawString is not printing the correct value in Jpanel.
The program is few thousand lines long. So I am posting a snippet where the error occurs



Output of this section of the code is
       0 8 8 8
0 8 8
While the values in print statement are correct. The value printed on the jpanel using g.drawString
is not correct. The g.drawString method prints 0 in the graphics panel instead of 8. The value in indexOfBlock1
is 8 and value in indexOfBlock is 0. Value in val3 is also same as value in indexOfBlock1. But if
I print it through g.drawString then it shows value as 0.
 
Marshal
Posts: 63837
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't use \n. If you are using printf() or format(), use %n instead.
On the second format String, you only have three % tags.
 
Campbell Ritchie
Marshal
Posts: 63837
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are the two positions you are drawing the String at. If yo uadd 10 to the x, you will be 10 px from the left of your Component, but where is your y? If that is the top left of the Component, then the text will be drawn outwith the visible area of the panel.
And sorry that I misunderstood your problem earlier
 
Chandrasegaran Narasimhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not that the number is not appearing in the jpanel. It is appearing. so the addition of 10 is not the error.In fact I added 10 to check whether print location moves when 10 is added. In fact I check the values of indexOfBlock1 by giving a print statement before and after the drawString statement. I don't know why the error occurs. I posted this because someone might have experienced similar problem.
Thanks for the %n tip, I did not know that.
 
Master Rancher
Posts: 3028
21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The program is few thousand lines long.


That is completely irrelevant to your question.

Your stated problem is that you can't print the value of a variable correctly.

So, create a proper SSCCE to demonstrate your problem.

All you need to do is override the paintComponent(...) method of a JPanel and add the above drawstring(...) statement to do the painting.

You just need a variable to contain the data you want to print and then you hardcode the x/y location of the string.

Get this simple program working first so you understand the basics of how the drawstring() method methods to prove to yourself that is does work.

Then do debugging of your code to see what you are doing differently.

We can't offer any help with 3 random lines of code.
 
Bartender
Posts: 690
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the snippet you posted an EXACT copy/paste of the real code for the g,drawString() line?
Is it possible that that line is   instead?

Try replacing that line with:
and see what results.
 
Chandrasegaran Narasimhan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried what Fred K. suggests.I still get the same error. I am now posting the function where the error occurs and the corresponding paint method. Remember I print values used by drawString before and after the drawstring statement.
 
Campbell Ritchie
Marshal
Posts: 63837
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code llooks horrible and is really difficult to read. What does == 1 mean in line 40? That doesn't look right. Since you are not printing anything called indexXXX but val3, I think that code does nothing but muddy the water; it could be hiding any number of errors. Please follow Rob Camick't suggestion of an SSCCE, but make the code legible.
 
Campbell Ritchie
Marshal
Posts: 63837
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, override the paintComponent() method, with protected access rather than paint().
I shall go back to my last post and correct its spelling errors.
 
Rob Camick
Master Rancher
Posts: 3028
21
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am now posting the function where the error occurs and the corresponding paint method.


You missed the point of my last comment.

You have NOT posted a SSCCE. How can we compile/text that code.

Again, we are not interested in your application.

You made a statement that drawstring(...) does not paint the contents of a variable properly.

So prove it. Post your SSCCE with a single drawstring and a single variable that contains the value you want to paint.

Get that simple code working first.

Once you get that working you will then realize the problem is NOT with the drawstring() method, but with your application code.

If is then up to you to debug your code to determine why your dynamic variables don't contain the value you think they contain.

 
Fred Kleinschmidt
Bartender
Posts: 690
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your original post, you call g.drawString() with the argument String.valueOf(indexOfBlock1).
In your revised post, you call g,drawString() with the argument val3. which is set to Integer.toString(val3).
What is the value of each of these, and which one do you really want?
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!