• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing and Saving an Image  RSS feed

 
Vincent Cas
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Wasn't sure whether to put this is beginners or something becuase I don't think its very hard, I just can't figure out how to do it.

Here goes,

I need to set up an Image that I can paint simple object on (rectangles, lines ect.), I need to save it as a file and I need to set the coordinates system so that its 0-255 from bottom left to top right.

Tanks for your time,
Thomas May
 
Vincent Cas
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should probably add, I can draw this objects fine on an applet, its the drawing on image, saving and coordinates system I'm having trouble with.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

If you have a Swing component that you can draw on, then it should be easy to display the image on it so that you can draw on top of it. What does "I'm having trouble with [it]" mean - what do you have, and how is or isn't it working?

Saving an image is generally done using the javax.imageio.ImageIO.write method, which takes a BufferedImage. Here's an example of how you can get a BufferedImage from a Graphics2D and save it using ImageIO.

I'm not sure what exactly you mean by the remark about coordinate systems. Stored images do not have an associated coordinate system that specifies that the lower left is point (0,0) and the upper right is point (255,255). That is a feature of display systems (like a Swing component). Can you clarify what you mean by that?
 
Vincent Cas
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for your rapid response and apologies that it has taken me so long to reply. I must confess I'd not read the 'How to ask good questions' guide and looking back at the original question it is very vauge.

The link you provided me with resolved most of my problems, so thanks very much. As to the coordiates, I will attempt to clarify.

When drawing an object on the image I'm using randomly generated values to determine the location and size. These values range from 0-255. A rectangle for example, x1 = 20
y1 = 50
x2 = 156
y2 = 221

I need to use these values to place the object in the correct location with x from going from left to right and y from bottom to top. Does this make sense?
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can never remember whether Java painting uses y coordinates top-to-bottom or bottom-to-top. If it's the other way around from what you're looking for, just use "getHeight() - y" instead of "y" for drawing.

You'd also need to do some scaling so that getWidth() corresponds to 255 in x direction, and getHeight() corresponds to 255 in y direction.

Does that help?
 
Vincent Cas
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, i will investigate the scaling.
Thanks for your help.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!