Java 重新喷漆/重新验证JPanel不起作用
我不清楚如何在现有代码中重新绘制JPanel。我也不明白paintComponent是如何被调用的。首先,我只是想清除JPanel,但我甚至不能这样做 我想能够按下“下一步”按钮,并重新油漆的JPanel给定的变化,在网格我读颜色。我在通过调用、revalidate()、removeAll()和repaint()来更改JPanel时遇到了问题 我称之为这些,但俄罗斯方块网格(tiles)(如图所示)下面的现有JPanel没有发生任何变化 在下面的代码中,我有一个嵌套类,它扩展了JPanel,setup函数创建了一个窗口,其中包含一个2d arraylist网格并构造原始网格 如何至少清除JPanel,或者使用新网格更好地重新绘制。。。我在想一个方法:Java 重新喷漆/重新验证JPanel不起作用,java,swing,awt,java-2d,mixing,Java,Swing,Awt,Java 2d,Mixing,我不清楚如何在现有代码中重新绘制JPanel。我也不明白paintComponent是如何被调用的。首先,我只是想清除JPanel,但我甚至不能这样做 我想能够按下“下一步”按钮,并重新油漆的JPanel给定的变化,在网格我读颜色。我在通过调用、revalidate()、removeAll()和repaint()来更改JPanel时遇到了问题 我称之为这些,但俄罗斯方块网格(tiles)(如图所示)下面的现有JPanel没有发生任何变化 在下面的代码中,我有一个嵌套类,它扩展了JPanel,se
public void redraw(Grid g) {
removeAll();
getColor(); //gets new grid of tiles
repaint();
revalidate();
}
在嵌套的JPanel类中,但这似乎根本不会更改JPanel,甚至不会清除当前图形
public class BoardGraphics {
public ArrayList<ArrayList<Color>> colours;
private final int width;
private int height;
private JFrame display;
private TetrisSquares tiles;
private Container c;
public BoardGraphics(int width, int height,
ArrayList<ArrayList<Integer>> grid) {
this.width = width;
this.height = height;
colours = new ArrayList<ArrayList<Color>>(width);
setColor(grid);
setup();
}
public void setup() {
System.out.println("Let the Tetris Begin");
display = new JFrame("Tetris Agent");
final TextArea welcome = new TextArea("Welcome to Tetris", 2, 10,
TextArea.SCROLLBARS_NONE);
welcome.setBackground(Color.black);
welcome.setForeground(Color.red);
welcome.setFont(new Font("monospaced", 0, 11));
welcome.setEditable(false);
Button btnStart = new Button("Next Move");
btnStart.setFocusable(false);
tiles = new TetrisSquares(TetrisBoard.BOARD_WIDTH, height);
c = new Container();
c.setLayout(new BorderLayout());
c.add(welcome, BorderLayout.NORTH);
c.add(tiles);
c.add(btnStart,BorderLayout.SOUTH);
btnStart.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
tiles.removeAll();
tiles.revalidate();
}
});
final Container main = new Container();
main.setLayout(new GridLayout(1, 2));
display.add(c);
display.pack();
display.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
display.setVisible(true);
}
public ArrayList<ArrayList<Color>> getBoard() {
return colours;
}
public void setColor(ArrayList<ArrayList<Integer>> grid) {
ListIterator<Integer> li;
for (int i = 0; i < width; i++) {
colours.add(new ArrayList<Color>());
li = grid.get(i).listIterator();
for (int j = 0; j <= height; j++) {
int n = 0;
if(li.hasNext()) {
n = li.next();
}
switch (n) {
case 1:
colours.get(i).add(Color.red);
break;
case 2:
colours.get(i).add(Color.pink);
break;
case 3:
colours.get(i).add(Color.orange);
break;
case 4:
colours.get(i).add(Color.yellow);
break;
case 5:
colours.get(i).add(Color.green);
break;
case 6:
colours.get(i).add(Color.cyan);
break;
case 7:
colours.get(i).add(Color.blue);
break;
default:
colours.get(i).add(Color.gray);
break;
}
}
}
}
public class TetrisSquares extends JPanel {
public int width;
public int height;
public TetrisSquares(int width, int height) {
this.width = width;
this.height = width;
this.setPreferredSize(new Dimension(width * 20, height * 20));
}
public void redraw() {
removeAll();
//add your elements
//revalidate();
//repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics = (Graphics2D) g;
graphics.setColor(Color.BLACK);
/*
* tiles.width = getSize().width / width; tiles.height =
* getSize().height / 800;
*
* insets.left = (getSize().width - width * tiles.width) / 2;
* insets.right = insets.left; insets.top = 0; insets.bottom =
* getSize().height - height * tiles.height;
*/
Dimension size = getSize();
Insets insets = getInsets();
int w = size.width - insets.left - insets.right;
int h = size.height - insets.top - insets.bottom;
int tileWidth = w / width;
int tileHeight = w / width;
ListIterator<Color> li;
for (int i = 0; i < width; i++) {
li = colours.get(i).listIterator();
int n = 20;
while(li.hasNext()) {
--n;
int x = (int) (i * tileWidth);
int y = (int) (n * tileHeight);
graphics.setColor(li.next());
graphics.fillRect(x, y, tileWidth, tileHeight);
graphics.setColor(Color.black);
graphics.drawRect(x, y, tileWidth, tileHeight);
}
}
}
}
}
公共类BoardGraphics{
公共阵列颜色;
私有最终整数宽度;
私人内部高度;
专用帧显示;
私人提琴砖;
私人货柜c;
公共电路板图形(内部宽度、内部高度、,
阵列列表(网格){
这个。宽度=宽度;
高度=高度;
颜色=新阵列列表(宽度);
设置颜色(网格);
设置();
}
公共作废设置(){
System.out.println(“让俄罗斯方块开始”);
显示=新JFrame(“俄罗斯方块代理”);
final TextArea welcome=新的TextArea(“欢迎来到俄罗斯方块”),2,10,
文本区域。滚动条(无);
欢迎光临。挫折背景(颜色。黑色);
欢迎。设置前景(颜色。红色);
欢迎使用.setFont(新字体(“等距”,0,11));
welcome.setEditable(false);
按钮btnStart=新按钮(“下一步”);
btnStart.setFocusable(假);
瓷砖=新的三角板(三角板、板的宽度、高度);
c=新容器();
c、 setLayout(新的BorderLayout());
c、 添加(欢迎,BorderLayout.NORTH);
c、 添加(瓷砖);
c、 添加(btnStart,BorderLayout.SOUTH);
btnStart.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件e)
{
tiles.removeAll();
tiles.revalidate();
}
});
最终主容器=新容器();
main.setLayout(新网格布局(1,2));
显示。添加(c);
display.pack();
display.addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
});
display.setVisible(true);
}
公共阵列列表getBoard(){
返回颜色;
}
公共void setColor(ArrayList网格){
列表迭代器李;
对于(int i=0;i 对于(int j=0;j而言,从通读开始,以更好地理解绘画系统
removeAll
从容器中删除所有子组件,这在这种情况下对您没有帮助
revalidate
处理布局子系统和更新容器层次结构以响应容器内容的更改,同样,这并没有真正的帮助
在paintComponent
方法中,您引用了一个名为colors
的ArrayList
,用于绘制正方形。您需要重置此列表以停止paintComponent
方法填充颜色
您的redaw
方法应该更像
public void redraw() {
colours.clear();
repaint();
}
从通读开始,更好地理解绘画系统
removeAll
从容器中删除所有子组件,这在这种情况下对您没有帮助
revalidate
处理布局子系统和更新容器层次结构以响应容器内容的更改,同样,这并没有真正的帮助
在paintComponent
方法中,您引用了一个名为colors
的ArrayList
,用于绘制正方形。您需要重置此列表以停止paintComponent
方法填充颜色
您的redaw
方法应该更像
public void redraw() {
colours.clear();
repaint();
}
为了更快地获得更好的帮助,请发布一条消息。为了更快地获得更好的帮助,请发布一条消息。