【问题标题】:Trying to retrieve the color from a grid at location x,y JPANEL试图从位置 x,y JPANEL 的网格中检索颜色
【发布时间】:2016-07-25 23:58:59
【问题描述】:

所以在我扩展 JPanel 的课程中​​,我有:

`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);
 }

我想要做的是在位置 x 和 y 的此类之外,在遍历面板时检索颜色。 非常感谢您的帮助,谢谢!

【问题讨论】:

  • 设为非私有,然后使用Pane.FILL_COLOR?
  • 我知道如何从 FiILL_COLOR 中检索颜色。我想要做的是检索已在网格上填充的颜色。例如在位置 (2,1)
  • 哦,我实际上正在寻找 GEM_COLOR,这就是为什么它不是私有的
  • 如果您正在寻找 GEM_COLOR,请使用 Pane.GEM_COLOR?
  • 对不起,我不是故意要混淆任何人,我知道如何找到 Gem_COLOR,我想将该 GEM_COLOR 与网格中的任何位置进行比较,直到它与颜色匹配

标签: java swing


【解决方案1】:

选项:

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

  2. 使用机器人创建ScreenCapture() 并搜索像素

  3. 使用二维数组跟踪 JPanel 上所有绘制的颜色

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 1970-01-01
    相关资源
    最近更新 更多