作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个面板,它扩展了 JPanel
并覆盖了绘制一些正方形的方法 paintComponent(Graphics g)
。
public class Painter extends JPanel{
private static final Color orange = new Color(225, 95, 0);
private Block[][] blocks;
private int sizeBlock;
public Painter() {
this.setBackground(Color.WHITE);
}
public void setBlocks(Block[][] blocks) {
this.blocks = blocks;
}
public void setSizeBlock(int size) {
this.sizeBlock = size;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(Block[] cols : blocks) {
for(Block block : cols) {
switch(block.getType()) {
case Block.wall: {
paintBlockLineBorder(g, block.getX()*(sizeBlock-1),
block.getY()*(sizeBlock-1), orange);
break;
}
default: {break;}
}
}
}
}
private void paintBlockLineBorder(Graphics g, int x, int y, Color color) {
//background
g.setColor(color);
g.fillRect(x, y, sizeBlock, sizeBlock);
//border
g.setColor(Color.BLACK);
g.drawRect(x, y, sizeBlock-1, sizeBlock-1);
}
}
我将此 JPanel
(Painter) 添加到其他 JPanel
(painterPanel) 中,后者被添加到布局为 GridBagLayout< 的
.JFrame
/
结果不是我想要的:
这就是我想要的:
这是一个重现问题的小用例:
public static void main(String[] args) {
JFrame mainFrame = new JFrame();
mainFrame.setLayout(new GridBagLayout());
mainFrame.setBounds(100, 100, 500, 400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridx = 0; gbc.gridy = 0;
gbc.weighty = 1; gbc.weightx = 1;
JPanel painterPanel = new JPanel();
painterPanel.setBorder(BorderFactory.createLineBorder(Color.black));
mainFrame.add(painterPanel, gbc);
Painter apainter = new Painter();
painterPanel.add(apainter);
Block[][] ablock = new Block[2][2];
ablock[0][0] = new Block(0, 0, Block.wall);
ablock[0][1] = new Block(0, 1, Block.wall);
ablock[1][0] = new Block(1, 0, Block.wall);
ablock[1][1] = new Block(1, 1, Block.wall);
apainter.setBlocks(ablock);
apainter.setSizeBlock(25);
apainter.repaint();
mainFrame.setVisible(true);
}
这里是 Block
类:
public class Block {
public static final String wall = "wall";
private int x;
private int y;
private String type;
public Block(int x, int y, String type) {
this.x = x;
this.y = y;
this.type = type;
}
//getters & setters
}
为什么我的 Painter
不完全可见?
最佳答案
尝试以下操作:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main{
public static void main(String[] args) throws InterruptedException{
JFrame mainFrame = new JFrame();
Block[][] ablock = new Block[2][2];
ablock[0][0] = new Block(0, 0, Block.wall);
ablock[0][1] = new Block(0, 1, Block.wall);
ablock[1][0] = new Block(1, 0, Block.wall);
ablock[1][1] = new Block(1, 1, Block.wall);
Painter apainter = new Painter();
mainFrame.add(apainter);
apainter.setBlocks(ablock);
apainter.setSizeBlock(25);
mainFrame.pack();
mainFrame.setVisible(true);
}
}
class Painter extends JPanel{
private static final Color orange = new Color(225, 95, 0);
private Block[][] blocks;
private int sizeBlock;
public Painter() {
setBackground(Color.WHITE);
setBorder(BorderFactory.createLineBorder(Color.black));
setPreferredSize(new Dimension(400, 300));
}
public void setBlocks(Block[][] blocks) {
this.blocks = blocks;
}
public void setSizeBlock(int size) {
sizeBlock = size;
//you may want to update Preferred Size
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(Block[] cols : blocks) {
for(Block block : cols) {
switch(block.getType()) {
case Block.wall: {
paintBlockLineBorder(g, block.getX()*(sizeBlock-1),
block.getY()*(sizeBlock-1), orange);
break;
}
default: {break;}
}
}
}
}
private void paintBlockLineBorder(Graphics g, int x, int y, Color color) {
//background
g.setColor(color);
g.fillRect(x, y, sizeBlock, sizeBlock);
//border
g.setColor(Color.BLACK);
g.drawRect(x, y, sizeBlock-1, sizeBlock-1);
}
}
class Block {
public static final String wall = "wall";
private final int x;
private final int y;
private final String type;
public Block(int x, int y, String type) {
this.x = x;
this.y = y;
this.type = type;
}
int getX() {return x; }
int getY() {return y; }
String getType() { return type;}
}
关于java - 在 A JPanel 上绘制的网格不完全可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53995194/
我是一名优秀的程序员,十分优秀!