图形代码的奇怪输出
Weird output for Graphics code
我正在尝试制作四人连线游戏,以提高我在 Java 图形方面的能力,并将其作为学校项目。游戏的背景将是蓝色 JPanel
,游戏板将是一个单独的 JPanel,将放置在背景之上。请参阅下面我的 classes:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class gameBoard extends JPanel {
private Board bored;
public gameBoard(){
setLayout(new BorderLayout());
bored = new Board();//does not appear in Center Region of gameBoard
add(bored, BorderLayout.CENTER);
}
public void paint(Graphics g){//This line is the one that is acting weird.
//blue rectangle board is here, but when repaint called
//JFrame turns blue and does not add new JPanel called above
g.setColor(Color.BLUE);
g.fillRect(0, 0, 1456, 916);
}
}
AND
import java.awt.BasicStroke;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Board extends JPanel {
/*
* 2d array will represent board and take 1's(red) and 2's(black) the nums
* represent pieces, with each redraw of the board, a check will be done to
* compare a sum against blackWin and redWin. Sum will be created by
* summing a continuous line of 1's or 2's going left -> right
*/
public int[][] boardEvalMatrix = new int[6][7];
private final int blackWin = 8, redWin = 4;
public Board() {//1200 x 764
BoardMethods a = new BoardMethods();
a.printBoard(getBoard());
JPanel panelLORDY = new JPanel(new FlowLayout());
repaint();
}
public int[][] getBoard(){
return boardEvalMatrix;
}
public void paint(Graphics g){
g.setColor(Color.BLUE);//Drawing background with actual board as a Test
g.fillRect(0, 0, 1456, 916);//will not remain like this
Graphics2D newG = (Graphics2D) g;
newG.setStroke(new BasicStroke(15));
g.setColor(Color.YELLOW);
for(int a = 0; a < 6; a++)//rows for board --- rowHeight is 127
g.drawRect(128, 68+ (a*127), 1200, 127);
//g.setColor(Color.BLACK);
//newG.setStroke(new BasicStroke(8));
//for(int a = 0; a < 7; a++)//columns for board --- columnWidth is 171
// g.drawRect(208, 152, 70, 10);
//g.drawLine(50,0, 1456, 916); //width 1456 length 916 - school computer monitors
}
}
所以发生的事情是这样的:
问题 1:
当我在 gameBoard
class 中包含 public void paint(Graphics g)
行时,运行 驱动程序出现的显示只是灰色 JFrame
,即使没有调用 repaint()
并且 paint()
方法是空的。但是,当我删除创建 paint
方法的行时,问题就消失了,并且出现了正确的显示。
问题 2:
即使我在 gameBoard
class 的 paint
方法中放置绘制蓝色矩形的代码并调用 repaint()
JFrame
蓝色,部分正确。我知道 Java 从上到下执行命令,所以我确保将实际游戏板添加到 gameBoard
JPanel
的代码是在绘制蓝色矩形之后出现的,但它没有用。
问题:
我做错了什么,我该如何解决?
要更改您刚刚使用的面板的背景颜色:
setBackground( Color.BLUE );
在面板上。然后就不需要定制绘画了。
当您覆盖 paint()
而忘记了 super.paint()
时,您真的会搞砸绘画过程。 paint()
方法负责在面板上绘制 child 组件。由于您不调用 super.paint()
,因此 children 永远不会被绘制。
如果您出于某种原因确实需要自定义绘画,那么您应该覆盖 paintComponent()
方法并且不要忘记调用 super.paintComponent()
。不要覆盖 paint()。
阅读关于“自定义绘画”的 Swing 教程,尤其是关于 A Closer Look at the Paint Mechanism 的部分,了解更多信息和示例。
我正在尝试制作四人连线游戏,以提高我在 Java 图形方面的能力,并将其作为学校项目。游戏的背景将是蓝色 JPanel
,游戏板将是一个单独的 JPanel,将放置在背景之上。请参阅下面我的 classes:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class gameBoard extends JPanel {
private Board bored;
public gameBoard(){
setLayout(new BorderLayout());
bored = new Board();//does not appear in Center Region of gameBoard
add(bored, BorderLayout.CENTER);
}
public void paint(Graphics g){//This line is the one that is acting weird.
//blue rectangle board is here, but when repaint called
//JFrame turns blue and does not add new JPanel called above
g.setColor(Color.BLUE);
g.fillRect(0, 0, 1456, 916);
}
}
AND
import java.awt.BasicStroke;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class Board extends JPanel {
/*
* 2d array will represent board and take 1's(red) and 2's(black) the nums
* represent pieces, with each redraw of the board, a check will be done to
* compare a sum against blackWin and redWin. Sum will be created by
* summing a continuous line of 1's or 2's going left -> right
*/
public int[][] boardEvalMatrix = new int[6][7];
private final int blackWin = 8, redWin = 4;
public Board() {//1200 x 764
BoardMethods a = new BoardMethods();
a.printBoard(getBoard());
JPanel panelLORDY = new JPanel(new FlowLayout());
repaint();
}
public int[][] getBoard(){
return boardEvalMatrix;
}
public void paint(Graphics g){
g.setColor(Color.BLUE);//Drawing background with actual board as a Test
g.fillRect(0, 0, 1456, 916);//will not remain like this
Graphics2D newG = (Graphics2D) g;
newG.setStroke(new BasicStroke(15));
g.setColor(Color.YELLOW);
for(int a = 0; a < 6; a++)//rows for board --- rowHeight is 127
g.drawRect(128, 68+ (a*127), 1200, 127);
//g.setColor(Color.BLACK);
//newG.setStroke(new BasicStroke(8));
//for(int a = 0; a < 7; a++)//columns for board --- columnWidth is 171
// g.drawRect(208, 152, 70, 10);
//g.drawLine(50,0, 1456, 916); //width 1456 length 916 - school computer monitors
}
}
所以发生的事情是这样的:
问题 1:
当我在 gameBoard
class 中包含 public void paint(Graphics g)
行时,运行 驱动程序出现的显示只是灰色 JFrame
,即使没有调用 repaint()
并且 paint()
方法是空的。但是,当我删除创建 paint
方法的行时,问题就消失了,并且出现了正确的显示。
问题 2:
即使我在 gameBoard
class 的 paint
方法中放置绘制蓝色矩形的代码并调用 repaint()
JFrame
蓝色,部分正确。我知道 Java 从上到下执行命令,所以我确保将实际游戏板添加到 gameBoard
JPanel
的代码是在绘制蓝色矩形之后出现的,但它没有用。
问题:
我做错了什么,我该如何解决?
要更改您刚刚使用的面板的背景颜色:
setBackground( Color.BLUE );
在面板上。然后就不需要定制绘画了。
当您覆盖 paint()
而忘记了 super.paint()
时,您真的会搞砸绘画过程。 paint()
方法负责在面板上绘制 child 组件。由于您不调用 super.paint()
,因此 children 永远不会被绘制。
如果您出于某种原因确实需要自定义绘画,那么您应该覆盖 paintComponent()
方法并且不要忘记调用 super.paintComponent()
。不要覆盖 paint()。
阅读关于“自定义绘画”的 Swing 教程,尤其是关于 A Closer Look at the Paint Mechanism 的部分,了解更多信息和示例。