繁体   English   中英

图形代码的奇怪输出

[英]Weird output for Graphics code

我正在尝试制作一个Connect Four游戏,以提高我使用Java Graphics和作为学校项目的能力。 游戏的背景将是一个蓝色的JPanel ,游戏板将是一个单独的JPanel,将放置在背景顶部。 请参阅下面的课程:

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);
    }

}

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类中包含public void paint(Graphics g)行时,即使没有调用repaint()并且paint()方法为空,运行驱动程序时出现的显示也只是一个灰色的JFrame 。 。 但是,当我删除创建paint方法的行时,问题消失了,并且出现了正确的显示。

问题2:

即使当我在gameBoard类的paint方法中放置代码以绘制蓝色矩形并调用repaint()JFrame也是蓝色的,这在一定程度上是正确的。 我知道Java从上到下执行命令,因此我确保将实际游戏板添加到gameBoard JPanel的代码是在绘制一个蓝色矩形后出现的,但是没有用。

题:

我做错了什么以及如何解决?

要更改面板的背景色,请使用:

setBackground( Color.BLUE );

在面板上。 然后,无需自定义绘画。

当您重写paint()并忘记了super.paint() ,您真的会弄乱绘画过程。 paint()方法负责在面板上绘制子组件。 由于您不调用super.paint()因此子代永远不会被绘制。

如果由于某种原因确实需要自定义绘画,则应该重写paintComponent()方法,不要忘记调用super.paintComponent() 不要覆盖paint()。

阅读有关“自定义绘画”的Swing教程,尤其是有关“仔细研究绘画机制”的部分, 获取更多信息和示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM