1

I have been researching some about algorithms for geometric primitives where the two most vital for me are lines with stroke and circles. Oracle does not seem to provide the exact information for generating these shapes for painting. The reason why I want this is because I need to store the affected pixels from painting into a texturePaint[] for game collision detection. I mainly wanted expert input on if its worth even doing because just using bufferedImages is effecting how big I can make my game maps because of RAM issues. Will it slow down my painting process by a significant amount from iteration through the array or does java iterate through each pixel for painting to draw its shapes?

StoneAgeCoder
  • 923
  • 1
  • 7
  • 13

1 Answers1

1

In terms of speed, writeableraster allows you to make pretty fast alterations to an image. You can also use tools like fork/join to speed things up faster if needed.

In terms of collision detection, I am afraid you are going to have to rely on your own hand-rolled mathematics functions. Here is an example.

EDIT:

When I was doing that kind of coding, I found that using the setPixels method in writeableraster was a LOT faster than relying on the paintComponent approach, such as drawing shapes. If you want to feel more certain, you could write some functions to test each approach by repeating the operation 10000 times and see which approach finishes faster.

Also, in addition to multithreading, you can use java's double buffering and BufferStrategy tools to prepare the subsequent rasters before they are needed. In this way, each successive raster is simply "turned on" when its turn comes because it has already been pre-rendered in the buffer.

I hope these additional links help.

Community
  • 1
  • 1
CodeMed
  • 9,527
  • 70
  • 212
  • 364
  • Alright so if I were to use fork and join with the geometric algorithms used to create shapes during painting I could possible iterate fast enough to both add the effected pixels to an array while at the same time painting to the screen? Also is using the setPixels method in writable raster faster than just storing the effected pixels as an area and drawing it as a shape? – StoneAgeCoder Feb 11 '14 at 07:00
  • 1
    @StoneAgeCoder +1 for continuing to try to learn. I just edited my original answer above to address your questions. Does this answer your question? – CodeMed Feb 12 '14 at 00:00
  • Thanks for the advise. I read both articles and tested the setPixels method in the BufferedImage. Unfortunately, when I use the getData() method for the writable raster it is not drawing correctly, but when I used getAlphaRaster() it worked just fine but returned a grayscale version of what I acctually wanted. After this I just tried the setRGB() method and it seems to work and paints to the screen about 100 times faster than using the paintComponent() method filling a rectangle pixel by pixel. Also what would I actually need to in order to prepare multiple buffer in code form? – StoneAgeCoder Feb 13 '14 at 00:18
  • I have never actually used a bufferStrategy before. – StoneAgeCoder Feb 13 '14 at 00:19
  • 1
    @StoneAgeCoder You use the `createBufferStrategy()` method of your `JPanel/JFrame/Canvas`. Then at some point you use `BufferStrategy.show()` to put the back buffer into the user's screen. I suggest you work up some brief sample code from the web and then if it does not work, put it in another question. Here is an example from which you might only use a few lines of code to structure your first attempt: http://www.javafaq.nu/java-example-code-782.html – CodeMed Feb 13 '14 at 16:47
  • It seems JPanel does not provide that method. JFrame does provide that method, but all other swing children do not have it. I would also much rather use swing components because I have a rather large personal library consisting of mostly swing compatibility. Your setPixel() method seems to be working smoothly as long as I do not overload the for loop with operations. I have it to where it is looping the current JFrame width and height at around 1000 by 600 so about 600,000 iterations. I found if I were to have a window size of 2000 by 2000 which as you can guess slowed repainting. – StoneAgeCoder Feb 13 '14 at 19:54
  • 1
    @StoneAgeCoder It has been a while since I have done that kind of programming. If you think of creative key words for google searches, you will find a lot of code examples. One thing is that people do is look for symmetries in an image and leverage those symmetries by storing results of functions for sections of an image or also just simply making a copy of the raster and only repainting the portions of the raster that have changed in a given new frame. – CodeMed Feb 14 '14 at 02:58