我试图使用绘图面板生成一个50x50的网格,但由于某种原因,它停在了50x10。我正在使用Intellij,如果这是相关的。
protected void paintComponent(Graphics g){
super.paintComponent(g);
int theChangingX = 0;
int theChangingY = 0;
for (int row = 0; row <= 2500 + 1; row++){
g.setColor(Color.red);
g.drawRect(theChangingX,theChangingY, pixelsize, pixelsize );
theChangingX+=10;
for (int col = 0; col<=2500 + 1; col++ ){
if (theChangingX ==2500){
theChangingY+=10;
theChangingX =-10;
}
}
}
}发布于 2020-02-09 03:08:12
我认为你把代码弄得太复杂了,我会这样做的:
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
for (int col = 0; col < 50; col++) {
for (int row = 0; row < 50; row++) {
g.drawRect(row * cellSize, col * cellSize, cellSize, cellSize);
}
}
}发布于 2020-02-09 03:09:48
尝尝这个
protected void paintComponent(Graphics g){
super.paintComponent(g);
int theChangingX = 0;
int theChangingY = 0;
for (int row = 0; row < 50; row++){
g.setColor(Color.red);
theChangingX = row*10;
for (int col = 0; col < 50 ;col++ ){
theChangingY = col*10;
g.drawRect(theChangingX,theChangingY, pixelsize, pixelsize );
}
}
}https://stackoverflow.com/questions/60130160
复制相似问题