简体   繁体   English

图形代码的奇怪输出

[英]Weird output for Graphics code

I am attempting to make a Connect Four game to improve my ability with Java Graphics and as a school project. 我正在尝试制作一个Connect Four游戏,以提高我使用Java Graphics和作为学校项目的能力。 The background for the game will be a blue JPanel and the game board will be a separate JPanel that will be placed on top of the background. 游戏的背景将是一个蓝色的JPanel ,游戏板将是一个单独的JPanel,将放置在背景顶部。 See my classes below: 请参阅下面的课程:

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

So what happened is this: 所以这是怎么回事:

PROBLEM 1: 问题1:

When I include the public void paint(Graphics g) line in the gameBoard class, the display that appears when I run the driver is just a gray JFrame , even though there is no call to repaint() and the paint() method is empty. 当我在gameBoard类中包含public void paint(Graphics g)行时,即使没有调用repaint()并且paint()方法为空,运行驱动程序时出现的显示也只是一个灰色的JFrame 。 。 However, when I deleted the line creating the paint method, the problem disappeared and the proper display appeared. 但是,当我删除创建paint方法的行时,问题消失了,并且出现了正确的显示。

PROBLEM 2: 问题2:

Even when I placed the code to draw a blue rectangle in the paint method in the gameBoard class and called repaint() the JFrame was blue, which is partly right. 即使当我在gameBoard类的paint方法中放置代码以绘制蓝色矩形并调用repaint()JFrame也是蓝色的,这在一定程度上是正确的。 I know that Java executes commands from top to bottom so I made sure that the code adding the actual game board to the gameBoard JPanel came after drawing a blue rectangle, but it didnt work. 我知道Java从上到下执行命令,因此我确保将实际游戏板添加到gameBoard JPanel的代码是在绘制一个蓝色矩形后出现的,但是没有用。

QUESTION: 题:

What did I do wrong and how do I fix it? 我做错了什么以及如何解决?

To change the background color of a panel you just use: 要更改面板的背景色,请使用:

setBackground( Color.BLUE );

on the panel. 在面板上。 Then is no need for custom painting. 然后,无需自定义绘画。

When you override paint() and forget the super.paint() , then you really mess up the painting process. 当您重写paint()并忘记了super.paint() ,您真的会弄乱绘画过程。 The paint() method is responsible for painting the child components on the panel. paint()方法负责在面板上绘制子组件。 Since you don't invoke super.paint() the children never get painted. 由于您不调用super.paint()因此子代永远不会被绘制。

If you do need custom painting for some reason then you should override the paintComponent() method and don't forget to invoke super.paintComponent() . 如果由于某种原因确实需要自定义绘画,则应该重写paintComponent()方法,不要忘记调用super.paintComponent() Don't override paint(). 不要覆盖paint()。

Read the Swing tutorial on 'Custom Painting`, especially the section on A Closer Look at the Paint Mechanism for more information and examples. 阅读有关“自定义绘画”的Swing教程,尤其是有关“仔细研究绘画机制”的部分, 获取更多信息和示例。

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

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