• Post Reply Bookmark Topic Watch Topic
  • New Topic

Keeping track of strings drawn on JPanel using Paint in Swing  RSS feed

 
Harry Weps
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I have this simple program that paints a string on JPanel using g2.drawString("Hello world", 40, 120). Basically, I want to be able to keep track of many strings on the JPanel at once. I'm not sure how to do this. For example, I would want to have an ArrayList of objects that keep track of these strings.

I want to be able to click-and-drag these strings so I will need to know there locations, etc.

Right now, using g2.drawString, it only draws the string. I want something like gw.draw(myStringObject). Not sure if this is a dumb request but this is what I am looking for. Thanks!

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have a good start, in that you have an object which represents the panel. (In fact it IS a panel.) So the next step is to have the object store a list of strings which are supposed to appear, and every time the Swing code calls the paintComponent method (not the paint method in Swing, please) that method goes through the list and draws the strings where they are supposed to appear.
 
Harry Weps
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You have a good start, in that you have an object which represents the panel. (In fact it IS a panel.) So the next step is to have the object store a list of strings which are supposed to appear, and every time the Swing code calls the paintComponent method (not the paint method in Swing, please) that method goes through the list and draws the strings where they are supposed to appear.


Thanks for the post. In respect to your comment, I am trying to keep track of where each string is and it's height, etc. So, instead of having one object that store each string, I am looking for something like an object for each string that keeps track of where the string is supposed to be.

I also need this because I want to detect a hover (and eventually drag) of these strings over the panel.

I am looking for something similar to a Path2D where you can do:



Not sure if you know what I am trying to say...lol
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wes Snips wrote:I am trying to keep track of where each string is and it's height, etc. So, instead of having one object that store each string, I am looking for something like an object for each string that keeps track of where the string is supposed to be.


Yes, you're on the right track there as well. You need another class which contains a String and a position (x and y coordinates?), and its height. That sounds like enough for a start. Then your panel object is going to want a list of those objects.
 
Rob Camick
Ranch Hand
Posts: 2756
12
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!