Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Determining Pixel Colour

 
Jonathan Steel
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Im trying to figure out how you can determine the colour of a pixel in the buffer of a JPanel.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't do that easily. Usually, to get a pixel valuem, one is buffering in a java.awt.image.BufferedImage and uses its getRGB method. You can scrape pixels off the screen, but that be nasty.
 
Jonathan Steel
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

I guess ill have to go the back buffer route then. I had hoped i could circumvent that option because Im trying to make a 4K game and this approach is sure to eat up some precious bytes.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another approach could be to "calculate" the pixel from the application state of your program. For example, if I were writing a chess game, given a (x,y) location I would check if the location was on the board or not; if on the chess board, if there was a piece on that square; if there was a piece, take the piece's image...
 
Jonathan Steel
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gotcha. I cant take that route though because Im actually using the buffer to store the information about my game world. Strange, I know. Im doing this because there will be debris all over the screen not very easily represented by any data structures due to the operations ill be performing on them. I want to know the colour of a pixel to use for collision detection.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic