1

So in my class that extends JPanel I have:

`class Pane extends JPanel {
public static final int MAX_X = 10; //Width
public static final int MAX_Y = MAX_X; //Height
private static final int CELL_WIDTH = 50; 
private static final int PREF_W = CELL_WIDTH * MAX_X;
private static final int PREF_H = CELL_WIDTH * MAX_Y;
private static final Color FILL_COLOR = Color.red;
static final Color GEM_COLOR = Color.blue;

}`

@Override
protected void paintComponent(Graphics g) {
   g.setColor(FILL_COLOR);
   g.fillRect(x * CELL_WIDTH, y * CELL_WIDTH, CELL_WIDTH, CELL_WIDTH);
 }

What I want to do is outside of this class at location x and y, retrieve the color while traversing through the panel. Help would be much appreciated, Thank you!

Bk Razor
  • 1,492
  • 1
  • 14
  • 24
  • Make it not private, then use `Pane.FILL_COLOR`? – user253751 Jul 26 '16 at 00:00
  • Well I know how to retrieve the color from FiILL_COLOR. What I want to do is retrieve the color that has been filled on the grid. For instance at location (2,1) – Bk Razor Jul 26 '16 at 00:05
  • Oh and im actually looking for GEM_COLOR which is why that is not private – Bk Razor Jul 26 '16 at 00:06
  • If you're looking for GEM_COLOR then use `Pane.GEM_COLOR`? – user253751 Jul 26 '16 at 00:15
  • Sorry I didnt meant to confuse anyone, I know how to find Gem_COLOR, I want to compare that GEM_COLOR with ever place in the grid until it matches the color – Bk Razor Jul 26 '16 at 00:17
  • What grid? I see no grid. Is there one Pane per grid cell? – user253751 Jul 26 '16 at 00:18
  • Yes i didnt put the grid code on here because its long and not needed for the question. is there a built in function similar to Pane.getComponentAt(x, y).getColorModel() – Bk Razor Jul 26 '16 at 00:25
  • So you're asking a question about code you haven't posted. How do you expect anyone to answer it without seeing the code it's about? – user253751 Jul 26 '16 at 00:26
  • For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jul 26 '16 at 04:21

1 Answers1

0

Options:

  1. How to get the color of a point in a JPanel?

  2. createScreenCapture() with Robot and search for the pixel

  3. Use a 2d array to keep track of all the drawn colors on JPanel

Community
  • 1
  • 1
JYun
  • 311
  • 2
  • 12