Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drawing over an image 1

Status
Not open for further replies.

pipk

Programmer
Feb 20, 2001
455
GB
Quick question on Swing.

Is it possible to import an image file, and then create an application which allows you to draw over the image.

Reason is this: I need to create an image consisting of grid lines on an X, Y basis. X being Time and Y being Depth. I want to be able to plot a line from any point on this grid to any other point which will be the function of X and Y.

I have already created something which draws the grid lines onto a JPanel, but am not sure how I will be able to draw over this component - and am wondering if there is an easier option?

I need some fresh thoughts, help is appreciated. cheers Static Apnea - snorkelling in a swimming pool without the snorkel?
 
Why don't you draw the grids and the line in the same JPanel (if it doesn't affect your application design). It would be a lot more simple this case, just draw the grid first followed by the line.

Hope this helps,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
could do, but I am attempting to allow the user to draw the line in by themselves connecting point to point like a join-the-dots picture - I am not sure how to separate the grid drawing from the (later) point to point drawing. Can i use the same paintComponent method of the JPanel? Static Apnea - snorkelling in a swimming pool without the snorkel?
 
Hm... I think you can store the drawing by the user in the form of 'coordinates' in an Array, Vector or whatever. For the grid drawing wise, you can either hardcode, or store in another Array. You can easily store them using the 2 end points as a String.

If you are drawing from point 10,10 to 50,10 and storing as a String, you can set the String with a value of "10 10 50 10". You can easily loop through this String or use a StringTokenizer to get the various coordinates.

So in your paintComponent method, just draw your grid first, followed by looping through the Array for the drawing coordinates. You can use different colours to differentiate between the grid and drawing if you want to make it more obvious to the user, what he has drawn.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
I had similar thoughts Leon, although hadn't thought about storing cocrdintes as a string. I shall have a go.

Once again Thanks.

pipk Static Apnea - snorkelling in a swimming pool without the snorkel?
 
Leon

Just to let you know I have managed to get it working a treat using a Vector storing a String representation of the coordinates, and a tokenizer to split it up again. Looks good.

Thanks again.

pipk::)

Static Apnea - snorkelling in a swimming pool without the snorkel?
 
No problem at all :) If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Another quick question though.

My app works fine, I create a line from point to point on my grid, but i would like to put in a "clear" facility. How do I completely refresh the JPanel so that all the Graphics are removed (so I can redraw the grid from scratch)?

I have tried clearing my vector apart from the initial element (which contains the start point of the grid) and then repainting the JPanel, but for some reason, even though the JPanel is prepared to redraw the grid, it is unwilling to remove the plotted lines I want to clear.

All the graphics stuff is done using Graphics2D draw (and drawing a Line2D) method.
I have even tried setting foreground and background colours in the hope that it will erase the plotted lines, but I am stuck. Does JPanel (or its superclasses) have a method that will remove all Graphics objects? Or is there something within Graphics/Graphics2D that will remove all drawn elements?

Cheers

pipk Static Apnea - snorkelling in a swimming pool without the snorkel?
 
in fact it looks as though what I have done does actually work, but the screen isn't getting refreshed properly. However, when i resize the JFrame (using the mouse pointer) that the JPanel is in, the lines disappear (as I need).
Which method does a JFrame resize event call, and what subsequent refresh methods does this resizing cause to be invoked? any clues?

I think this is another thread in its own right..darn Static Apnea - snorkelling in a swimming pool without the snorkel?
 

Its OK chaps, forget my previous ramblings, I have sorted it out.

I was doing the repaint from the JPanel and I should have been doing it from the JFrame that it was contained in.

But Thanks for reading..

Static Apnea - snorkelling in a swimming pool without the snorkel?
 
Hey, you can call updateUI() in your JPanel. And also, there is the removeAll() method in JPanel too to remove all the components in the JPanel. I think both should work :) If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
cheers Leon,

I didn't try those. They may come in handy later on.

pipk Static Apnea - snorkelling in a swimming pool without the snorkel?
 
Create your own custom line object then store all of those line objects in a vector. You can even use the ObjectOutputStream to save the Vector of Lines to a file and have them restored from the file as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top