• Post Reply Bookmark Topic Watch Topic
  • New Topic

Set Pixel(x, y) to color c  RSS feed

 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I best do that? I am currently trying to paint at the JFrame itself, but it's not working at all... I haven't really gotten that far in how Swing works, so I could be that, but is it a good way to do it? Also, is there a better way to color an individual pixel than line(x, y, x, y)?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this code does what you asked

Here is a tutorial for painting
[ November 16, 2003: Message edited by: Jose Botella ]
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, thanks. I have one problem with it though, it works when I call it on an hard-coded integer, but when I try to use it in a for/while-loop it doesn't work. (I am making a fractal-painter, so there are quite a few times when it will try to paint a pixel)
Right now it looks like this

I put the whole loop-sequence in the paintComponent method, should I do that?
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the way you should be doing it... how long does that code take to generate a fractal? paintComponent() gets called every time a component repaints... and until the method exits the entire repaint operation is hung and the program can't respond to any events. In this case what you probably want to do is paint to a BufferedImage and then just draw that image on the component in paintComponent(). You are probably going to want to paint to the image in another thread because generating a fractal could take a while.

Another thing to point out - if you are using integers (which I am assuming you are because I don't see any casts) that fractal drawing code will probably never exit (at least with all the values I'm trying it with). Integer division will produce results that ignore remainders and you will never increment or decrement values. For this to work you're going to have to use floats or doubles to get real values, and then round x and y when you use them to paint.

I'll try to post some code later that shows how to draw to a BufferedImage instead of the component itself, and updates it using another thread.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is it quick enough? then it is ok. Otherwise a separate thread might calculate and store the values to be print.
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't got as far in my learning of Java to do separate threads yet, maybe I'd better look into it, could be useful
My variables are declared like this:

It takes on average 1 - 3 min. depending upon how complicated and coherent the fractal is.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put some System.out.println() statements in, and with those values the first point drawn is at (-278.0, -198.0) and after a little while starts repeatedly producing (-Infinity, -Infinity). Nothing will ever be drawn because all the points are located off the drawable area and because the code loops forever, so the paintComponent() method never exits.
 
Carl Pettersson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... Is there some way to define the coordinate system in the window, or is that fixed?
/edit:
Turns out I had accidentally reversed the order of the steps in the for's, swid/(xmax-xmin) is widely different from (xmax-xmin)/swid
[ November 19, 2003: Message edited by: Carl Pettersson ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!