绘制到特定的JPanel

时间:2013-05-30 19:05:47

标签: java swing jpanel paintcomponent

我正在尝试重拍Minesweeper,但是当我将其他组件添加到屏幕上时,我在显示按钮后面的数字时遇到了问题,例如我的计数器。

在这种特殊情况下,我有两个JPanel。 我的第一个JPanel将包含多个组件,例如我的计数器和另一个包含该板的JPanel(按自定义JButtons列的行数组)。

在我添加矿井计数器之前,一切似乎工作正常,数字很好,逻辑有效,等等。但这不是我的问题所在。

在我的自定义JPanel(包含标签,电路板等的面板)中,我正在使用paintComponent()方法将数字打印到屏幕上。但是,一旦我添加了另一个JPanel,我需要将数字添加到屏幕上,我遇到了一些问题。

首先,我不知道如何区分我正在绘制的面板,但我认为我找到了一行如下:“g = boardPanel.getGraphics()”。如果这样可行,我在其他地方遇到了问题。可能我的paintComponent()方法结构如何,我不确定订单的外观。

以下是我所拥有的一些代码:

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g = boardPanel.getGraphics();

    //Draw the lines and numbers behind and separating the tiles. These, in my
    //mind, are drawn to the graphics object regarding the playable board (the
    //board, not the panel including the mine counter, etc.) But, when I click
    //on a tile, there are no mine indicators behind it.

}

我想,从本质上讲,我只是不确定如何构造paintComponent()方法,因为我相信我必须弄乱两个不同的图形对象,一个用于电路板面板,另一个用于控制我的数量,董事会小组。

任何帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:2)

您必须为每个JPanel实现两个不同的类。然后他们将拥有自己的paintComponent()方法,你不会想到"猜测"至于你在哪个JPanel上画画。

我不会将每个地砖都作为JButton实施,但这是另一个问题。