简体   繁体   中英

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. 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. 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:

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. However, when I deleted the line creating the paint method, the problem disappeared and the proper display appeared.

PROBLEM 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. 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.

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. The paint() method is responsible for painting the child components on the panel. Since you don't invoke super.paint() the children never get painted.

If you do need custom painting for some reason then you should override the paintComponent() method and don't forget to invoke super.paintComponent() . Don't override paint().

Read the Swing tutorial on 'Custom Painting`, especially the section on A Closer Look at the Paint Mechanism for more information and examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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