数组中的矩形未绘制

时间:2018-01-17 13:59:21

标签: arrays netbeans-6.9

我正在使用Java NetBeans 6.9.1制作破砖游戏。我的数组映射/矩形不是应该绘制的。我不知道是什么问题。

主类:

  public static void main(String[] args) {
   JFrame obj = new JFrame();
   GamePlay gamePlay = new GamePlay();
   obj.setBounds (10, 10, 700, 600);
   obj.setTitle("Brick Breaker");
   obj.setResizable(false);
   obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   obj.setVisible(true);
   obj.add(gamePlay);

MapGenerator:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
public class MapGenerator {
public int map[][];
public int brickWidth;
public int brickHeight;
public MapGenerator(int row, int col){
    map = new int [row][col];
    for (int i = 0; i< map.length;i++) {
        for (int j=0; j< map[0].length; j++) {
            map[i][j] = 1;
        }
    }
    brickWidth = 540/col;
    brickHeight = 150/row;
}
public void draw(Graphics2D g){
    for(int i = 0; i < map.length; i++) {
        for(int j=0; j< map[0].length; j++){
            if (map[i][j]>0){
               g.setColor(Color.red);
               g.fillRect(j * brickWidth + 80, i *brickHeight + 50,       brickWidth, brickHeight);

在GamePlay类中绘制地图:

private MapGenerator map;
 public GamePlay() {
    map = new MapGenerator(3,7);
    //drawing map
    map.draw((Graphics2D)g);

0 个答案:

没有答案