Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Image/Graphics color

  Asked By: Pedro    Date: May 16    Category: Java    Views: 1041
  

Does anyone know of a method that will return the color value of
either an Image, or Graphics at a given x,y coordinate? If not a
method, can anyone give me some code or an idea of how I would go
about this?

Share: 

 

6 Answers Found

 
Answer #1    Answered By: Janis Hart     Answered On: May 16

"Does anyone know of a method  that will return  the color  value of
either an Image, or Graphics at a given x,y coordinate?"

...you can use the getColor() method on a Graphics object... if you
are looking to find the individual color of a point, you can check
out the classes/methods of java.awt.color, but i believe you are
going to have to code  it; try copying a small area of the display to
an object then call getColor() on the pixel object you copied, if
that is unsatisfactory run it(the object) through a switch filter
(the better the filter the more precise the color match)...

 
Answer #2    Answered By: Shiv Patel     Answered On: May 16

i have been working on it for a
few hours now, however I still cant get it to work!! What im trying
to make is a snake/tron type game, that will eventually have an AI
computer opponent! (hopefully!) heres the code  I have so far, I'd
appreciate it if anyone has the time to look over it for me to see
where im going wrong. I just cant see why the color  values dont
change as the "snake" moves in and out of the red section?! anyway,
heres the JAVA code:

import java.awt.image.*;
import java.awt.*;
import java.applet.*;

public class snake extends Applet implements Runnable
{
int p1SpeedX = 0;
int p1SpeedY = 0;
int p1X = 10;
int p1Y = 10;
int appletWidth; // size of the applet - x
int appletHeight; // size of the applet - y
Image Buffer;
Graphics gBuffer;

public void init()
{
appletWidth = size().width;
appletHeight = size().height;

Buffer=createImage(appletWidth,appletHeight);
gBuffer=Buffer.getGraphics();

gBuffer.setColor(Color.black);
gBuffer.fillRect(0,0,100,size().height);


gBuffer.setColor(Color.red);
gBuffer.fillRect(100,0,size().width,size().height);

}

public void start ()
{
// creates a new thread
Thread th = new Thread (this);
// Starts the thread
th.start ();
}

public void stop()
{

}

public void destroy()
{

}

public boolean keyDown (Event e, int key)
{

if ((key == Event.LEFT)&&(p1SpeedX != 1))
{
p1SpeedX = -1;
p1SpeedY = 0;
}

else if ((key == Event.RIGHT)&&(p1SpeedX != -1))
{
p1SpeedX = 1;
p1SpeedY = 0;
}

else if ((key == Event.UP)&&(p1SpeedY != 1))
{
p1SpeedY = -1;
p1SpeedX = 0;
}
else if((key == Event.DOWN)&&(p1SpeedY != -1))
{
p1SpeedY = 1;
p1SpeedX = 0;
}
else
{
System.out.println ("Charakter: " + (char)
key + " Integer Value: " + key);
}

return true;
}

public void run ()
{

//Thread.currentThread().setPriority
(Thread.MIN_PRIORITY);


while (true)
{
try {Thread.sleep(5);}
catch (Exception e) { }


if (p1X > appletWidth)
{

p1X = 0;
}

else if (p1X < 0)
{

p1X = appletWidth;
}

if (p1Y > appletHeight)
{

p1Y = 0;
}

else if (p1Y < 0)
{

p1Y = appletHeight;
}

p1X += p1SpeedX;
p1Y += p1SpeedY;

repaint();

// ---- THIS SECTION BELOW IS THE BIT IM
HAVING PROBLEMS WITH
int[] pixel = new int[1];

PixelGrabber pg = new PixelGrabber(Buffer,
p1X, p1Y, 1, 1, pixel, 0, 1);
try
{
pg.grabPixels();
} catch (InterruptedException e)
{
System.err.println("interrupted
waiting for pixels!");
return;
}
ColorModel cm = pg.getColorModel();

gBuffer.setColor(Color.black);
gBuffer.fillRect(5,90,size().width,110);
gBuffer.setColor(Color.yellow);
gBuffer.drawString(" R: "+cm.getRed(0)+"
G: "+cm.getGreen(0)+" B: "+cm.getBlue(0)+"\n"+cm.toString(),10,100);

gBuffer.setColor(Color.yellow);
gBuffer.fillOval(p1X, p1Y, 1, 1); //the line
that the player controls


//Thread.currentThread().setPriority
(Thread.MAX_PRIORITY);
}
}

public void update(Graphics g)
{
paint(g);
}


public void paint(Graphics g)
{
g.drawImage (Buffer,0,0, this);
}

}




and heres the HTML code to run the applet:

<applet code="snake.class" width=500 height=500>
</applet>

 
Answer #3    Answered By: Marnia Mian     Answered On: May 16

Actually it will have to wait, I'm still not exactly sure of what
you are after... as your snake grows the tail color  changes to yellow
as i would expect from the code...
---> did you want the tail yellow in the black and blue in the red
areas? if so, just set up a collision detection method  to determine
if the snake crosses into/out of the red boundaries and implement a
tail color change at the borders

Refinements,
keyDown() was deprecated to processKeyEvent()...so read up on the
class KeyEvent to keep your code  up to speed

 
Answer #4    Answered By: Ayaz Hashmi     Answered On: May 16

Sorry, I should have made it clearer. The red section is only there
to test the code  used to work out the color  of the pixel (i.e so i
can see the change from red to black). In the finished game the
background will be all black. The idea  is that before the head of
the tail is moved on a pixel, the pixel that will be colored yellow
(to form the head) is tested to see what color it is. if its the
background color then all is ok. If its not then there has been a
collision (either with itself, or with another player). my code
however claims the same color of R0 G0 B0 no matter what! something
is amiss!

As for the lack of OO and the deprecated methods:

yeah I realise it aint coded too hotly. I just quickly hacked it
down to get a game going, the main aspect of the project will be the
addition of the AI but cheers for the advice, I will look up
processKeyEvent().

 
Answer #5    Answered By: Zane Thompson     Answered On: May 16

You will be pleased to know that I have solved my issues with
finding a pixels color  @ a specific x,y coordinate. for anyone whos
interested I used "BufferedImage" instead of "Image". BufferedImage
has the most useful getRGB(int x, int y) method!

 
Answer #6    Answered By: Constance Reid     Answered On: May 16

Sorry about the delay, my connection degraded to the point of having
to call SBC and having them replace the line... actually they
switched me to "another?" line, while they repair my old one... end
result 4 days no connection with hyperworld, but now i'm back...

Glad to see you've figured out your problem on your own, actually I
may have never thought of that... I've implemented something similar
to what you are doing by drawing my "snake" to an array and having
my "snake" class draw itself everytime it was called. This makes
collision detection easy. Utilizing the game engine class just make
a call to the "snake" array and test every segment of the "snake" for
collision with walls, jumpgates, snake treats, enemy snakes, ect.
Although it may not work as well with your implementation as your
snake grows constantly... I was planning ahead thinking maybe you
might later plan on having your snake only grow when it eats enemy
snakes or whatever... well that is my 7.5 cents...

 
Didn't find what you were looking for? Find more on Image/Graphics color Or get search suggestion and latest updates.




Tagged: