Hello
I'm trying to write a minor "snake" clone; but I have some problems with the code
I want to retrieve a pixel at (x,y) and check its color; and according to my googling I should use g.getRGB(x,y), but I can't get it to work.
import java.applet.Applet; import java.awt.*; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.event.MouseEvent; import java.awt.event.MouseMotionAdapter; import java.awt.image.BufferedImage; import javax.swing.JFrame; public class SnakeTest2 extends Applet { int gap = 3; int mx, my; Image buffer = null; int w, h; public static void main(String[] a) { JFrame f = new JFrame(); f.setSize(258, 277); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new SnakeTest2()); f.setVisible(true); } public SnakeTest2() { setSize(258, 277); Dimension d = getSize(); w = d.width; h = d.height; buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); } public void paint(Graphics g) { Graphics screengc = null; screengc = g; g = buffer.getGraphics(); g.setColor(Color.BLUE); g.fillRect(0,0,250,250); g.setColor(Color.BLACK); g.fillRect(5,5,240,240); // I want to check if the color of pixel (x,y) = RED, how do I do it? screengc.drawImage(buffer, 0, 0, null); } }