This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Find index by xy ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Find index by xy ??" Watch "Find index by xy ??" New topic
Author

Find index by xy ??

Jimmy Nilsson
Greenhorn

Joined: Sep 25, 2004
Posts: 26
Hey

I got an Array that contains info about words, and the position thay shold be printed on the Drawing.

I use a defultStyledDocument where i use colors.

Say that the first word is placed some rows down, and i only got the x y value of this, not how many /n i realy need. Is there a way to give a x y(point) value and get the postione in the styled document?

Or do i have to calculate the rows my self(with scalings) and the have a loop that adds "\n" as many times as needed to the string?

This is not easy, i dont know the row size and it can be in diffrent scales to.

Do you have any ide how i could solve this in a good way?

//Jimmy
Jimmy Nilsson
Greenhorn

Joined: Sep 25, 2004
Posts: 26
I shold also mension that i am loading this Styled document in to a JTextPane.

//Jimmy
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
In the JTextPane api scroll down to the section Methods inherited from class javax.swing.text.JTextComponent to find the modelToView and viewToModel methods.
Jimmy Nilsson
Greenhorn

Joined: Sep 25, 2004
Posts: 26
Thanks!

I have now tryed to go from positioning(point(XY)) to rows, but i dont get the right XY value.

Here is a code, first i count all the "\n" in the StyledDocument, and it is 221, then i whant to know the XY value for the last row, and by this i tke the JTextPane and use modelToView with the 221 as argument, and then get the Y out of the rectangle.

int endRow = this.BP.getLastRowNr()+5; // 221
int endY = (int)TextP.modelToView(endRow).getY();

this shold work, but the value is not right
Row : 221 (sounds right)
Y : 302 (do NOT sound right) is shold be over 5000 i think.

I am drawing a frame round the text and this frame is not bigger then 18 rows if i calc them by hand in the view, if i could get a proper valu out of modelToView then it would fit the text perfect(around 5000 rows or somthing)

Regards
Jimmy
Jimmy Nilsson
Greenhorn

Joined: Sep 25, 2004
Posts: 26
I have notice that i have to add around 3350 to the current row nr to get it it right, this mean that 3571 rows is getting me the right Y value to my 221 rows, this dosent simes right, what am i doing wrong here?

//Jimmy
Jimmy Nilsson
Greenhorn

Joined: Sep 25, 2004
Posts: 26
Anyone?
Jimmy Nilsson
Greenhorn

Joined: Sep 25, 2004
Posts: 26
It simes like it is counting the positions in the string to the Styled dokument and sees were on the JTexPanel it is. that means that if there is alot of text on one row say

1. "Here is som text\n"
2. "and here is the new row"

An if i then send 2 as an argument( i whant to know rows two Position(XY) but in will get the position of r instead.

Pleas is there any way to get the positon by giving a row nr, say 2 in this case?

O do i have to come up with somthing on my own here? like counting all the "\n"? and then take the position right after the "\n" and get the XY Position?

//Jimmy
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find index by xy ??
 
Similar Threads
Iterative Fibonacci Method Problem
Echo value capture in .sh script
Apply function names as parameters in other functions
StringTokenzier
I am new to Java programming