• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Find index by xy ??

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I shold also mension that i am loading this Styled document in to a JTextPane.

//Jimmy
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone?
 
Jimmy Nilsson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic