在 Swing JPanel 的边框后面绘制图像
Drawing image behind border in Swing JPanel
我创建了一个 JFrame
,它在 MigLayout
中包含一个 JPanel
。
在那个布局中,我有 5 列和 4 行,每行都有一个 'Big Cell'。
'Big Cell' 扩展了 JPanel
并具有边框颜色,在 'Big cell' 中是一个 'small cell',它也扩展了 JPanel
并具有边框颜色。 16x16 grid in big cell of the small cells.
我正在尝试绘制图像,但它只会在绘制其他所有内容(边框、背景等)后才绘制图像
但是我想先画图,再画边框。
我不能对单个单元格使用 setIcon,因为它不绘制图像(不确定为什么)并且它使我的网格大 10 倍并且程序溢出太大以至于它甚至不适合4k 分辨率。
这是它的外观图片
我希望每个黑色 'box' 或 'Big Cell' 都有自己的背景图像,在带有橙色边框的小单元格后面绘制或渲染。
下面是绘制图像后的样子
您可能看不出来,但这就是每个 'big cell' 中呈现的图像,除了它在所有内容的前面。看起来它只是一张图片,但不要被愚弄,因为它是一张无缝图片。
这是我认为的 'BigCell' class 问题的根源。
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);
}
}
还有 'Cell' class,这是一个带有橙色边框的小盒子
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);
}
}
改变...
@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);
}
到
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, (int) getSize().getWidth() - 0, (int) getSize().getHeight() - 0, this);
}
}
paintComponent
在 paintBorder
和 paintChildren
之前调用,因此它是绘制背景图像的完美位置
然后将 setOpaque(false);
添加到 Cell
的构造函数中
查看 Performing Custom Painting and Painting in AWT and Swing 了解有关 Swing 绘画工作原理的更多详细信息
我创建了一个 JFrame
,它在 MigLayout
中包含一个 JPanel
。
在那个布局中,我有 5 列和 4 行,每行都有一个 'Big Cell'。
'Big Cell' 扩展了 JPanel
并具有边框颜色,在 'Big cell' 中是一个 'small cell',它也扩展了 JPanel
并具有边框颜色。 16x16 grid in big cell of the small cells.
我正在尝试绘制图像,但它只会在绘制其他所有内容(边框、背景等)后才绘制图像
但是我想先画图,再画边框。
我不能对单个单元格使用 setIcon,因为它不绘制图像(不确定为什么)并且它使我的网格大 10 倍并且程序溢出太大以至于它甚至不适合4k 分辨率。
这是它的外观图片
我希望每个黑色 'box' 或 'Big Cell' 都有自己的背景图像,在带有橙色边框的小单元格后面绘制或渲染。
下面是绘制图像后的样子
您可能看不出来,但这就是每个 'big cell' 中呈现的图像,除了它在所有内容的前面。看起来它只是一张图片,但不要被愚弄,因为它是一张无缝图片。
这是我认为的 'BigCell' class 问题的根源。
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);
}
}
还有 'Cell' class,这是一个带有橙色边框的小盒子
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);
}
}
改变...
@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);
}
到
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, (int) getSize().getWidth() - 0, (int) getSize().getHeight() - 0, this);
}
}
paintComponent
在 paintBorder
和 paintChildren
之前调用,因此它是绘制背景图像的完美位置
然后将 setOpaque(false);
添加到 Cell
的构造函数中
查看 Performing Custom Painting and Painting in AWT and Swing 了解有关 Swing 绘画工作原理的更多详细信息