Martin
Martin

Reputation: 202

How to find out all pixels in lines drawn by user in Java Graphics

I've been trying to get all the pixels of a line which is being drawn by a user into an array.

I will later try to make an analysis of this line so it is very important that I don't miss any pixels. With the code I use currently it misses quite a few pixels. The problems should be in the way that graphics creates lines and not individual pixels. The pixels that are set, are saved into the image1Pixel array. Here is my myMouseDragPaint function in which I save the lines into the array. Does anyone know how I can change this so I won't miss any pixels? If you need any additional information regarding this question, please don't hesitate to ask.

Thank you,

Miza

Upvotes: 0

Views: 781

Answers (1)

Sergey Grinev
Sergey Grinev

Reputation: 34518

You are saving only line vertexes in your code, missing all pixels between them:

image1Pixel[currX][currY] = 1; // line vertex
G.drawLine(prevX, prevY, currX, currY); // this draws a whole line, no just two vertex points

You need to either:

  • interpolate all pixels on that line. E.g., popular algorithm is Bresenham's
  • or get an image and read pixel color directly from it

Also, note that lines don't cover single pixels (except exactly vertical or horizontal ones). So second option will work better for you, because you will not repeat drawing math after graphical library.

Upvotes: 1

Related Questions