我創建了一個JFrame
,其中包含JPanel
,其中包含MigLayout
。在Swing中的邊框後面繪製圖像JPanel
在該佈局中,我有5列和4行,每個都有一個「大單元」。
'大細胞'延伸JPanel
並且具有邊框顏色,並且在'大細胞'內是'小細胞',其也延伸JPanel
並且具有邊框顏色。 16x16格子在小細胞的大細胞內。
我試圖繪製圖像,但其餘的都繪製(邊框,背景等)
後僅繪製圖像,但我想先繪製圖像,並繪製後的邊界。
我不能使用setIcon爲單個單元格,因爲它不繪製圖像(不知道爲什麼),它使我的網格10倍大,程序溢出太大,它甚至不適合4k解析度。
這裏是什麼樣子
我希望每個黑「盒子」或「大細胞」有它的,是描繪或渲染的小細胞與後面自己的背景圖像的圖片橙色邊框。
這裏就是它看起來像畫
你可能無法告訴的形象,但這是呈現在每個「大細胞」,因爲它應該,除了它是在前面的圖像一切。它看起來像只是一個圖像,但不要被愚弄,因爲它是一個無縫的圖像。
下面是我所假設的'BigCell'類是問題的根源。
package com.michaelgates.dev.OldLeaf.gui.components;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
public class BigCell extends JPanel
{
int column;
int row;
Cell[][] children;
JPanel parent;
Image image;
public BigCell(int column, int row, JPanel parent)
{
this.column = column;
this.row = row;
this.parent = parent;
setLayout(new GridLayout(16, 16, 0, 0));
setBorder(new LineBorder(Color.DARK_GRAY, 2));
children = new Cell[16][16];
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 16; j++)
{
Cell cell = new Cell(i, j, this);
add(cell);
children[i][j] = cell;
}
}
image = Toolkit.getDefaultToolkit().getImage(getClass().getClassLoader().getResource("img/acre/acre_0.png"));
}
@Override
public void paint(Graphics g)
{
if (image != null)
{
g.drawImage(image, 0, 0, (int) getSize().getWidth() - 0, (int) getSize().getHeight() - 0, this);
}
super.paint(g);
}
}
和 '細胞' 類,這是有橙色邊框
package com.michaelgates.dev.OldLeaf.gui.components;
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
public class Cell extends JPanel
{
int column;
int row;
BigCell parent;
public Cell(int column, int row, BigCell parent)
{
this.column = column;
this.row = row;
this.parent = parent;
setBorder(new LineBorder(Color.orange, 1));
//setBackground(Color.LIGHT_GRAY);
}
}
考慮提供一個[runnable示例](https://stackoverflow.com/help/mcve),它演示了您的問題。這不是代碼轉儲,而是您正在做的事情的一個例子,它突出了您遇到的問題。這將導致更少的混淆和更好的響應 – MadProgrammer
@MadProgrammer我認爲這些圖像足夠了,但如果不是,我會添加一個可運行的示例。 –
那麼,我基本上沒有真正的想法,你在做什麼的順序或組件的佈局達到什麼目的。油漆背景可能會很棘手。你可以使所有組件透明,並通過容器'paintComponent'方法繪製圖像,但我不知道這種情況是否可行 – MadProgrammer