簡體   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