有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java图形未显示在JFrame上

我不熟悉java中的图形,由于某些原因,图形没有显示在jframe上。我对如何设置和实例化图形感到困惑。代码中也可能有一个愚蠢的错误,我只是没有看到。谢谢你的反馈

地图类

public class Map extends JPanel{

private static int WIDTH;
private static int HEIGHT;
private static int ROWS;
private static int COLS;
private static int TILE_SIZE;
private static int CLEAR = 0;
private static int BLOCKED = 1;

private static int[][] GRID;

public Map(int w, int h, int t){

    WIDTH = w;
    HEIGHT = h;
    TILE_SIZE = t;
    ROWS = HEIGHT/TILE_SIZE;
    COLS = WIDTH/TILE_SIZE;

    GRID = new int[ROWS][COLS];

    for (int row = 0; row < ROWS; row++){
        for (int col = 0; col < COLS; col++){
            GRID[row][col] = BLOCKED;
        }
    }

    randomMap();
}

public void randomMap(){
    int row = 0;
    int col = 0;
    int turn;

    Random rand = new Random();

    GRID[row][col] = CLEAR;

    do{
    turn = rand.nextInt(2)+1;
    if (turn == 1)
        row++;
    else
        col++;
    GRID[row][col] = CLEAR;
    }while(row<ROWS-1 && col<COLS-1);

    if (row == ROWS-1){
        for (int i = col; i < COLS; i++){
            GRID[row][i] = CLEAR;
        }
    }
    else{
        for (int i = row; i < ROWS; i++){
            GRID[i][col] = CLEAR;
        }
    }


}


public void paintComponent(Graphics g) {

    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    for (int row = 0; row < WIDTH; row++){
        for (int col = 0; col < HEIGHT; col++){
            if (GRID[row][col] == 1){
                g2d.setColor(Color.BLACK);
                g2d.fillRect(row*TILE_SIZE, col*TILE_SIZE, TILE_SIZE, TILE_SIZE);
            }else{
                g2d.setColor(Color.WHITE);
                g2d.fillRect(row*TILE_SIZE, col*TILE_SIZE, TILE_SIZE, TILE_SIZE);
            }
        }
    }
}

public void displayConsole(){

    for (int row = 0; row < ROWS; row++){
        for (int col = 0; col < COLS; col++){

            System.out.print(GRID[row][col] + "   ");
        }
        System.out.println("");
        System.out.println("");
    }
}

}

游戏课

public class Game extends JFrame{

private Map map;

public Game(){

    setLayout(null);
    setBounds(0,0,500,500);
    setSize(500,500);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Map map = new Map(500,500,50);
    map.displayConsole();

    add(map);
    repaint();
    setVisible(true);
}

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Game game = new Game();

}

}

共 (2) 个答案

  1. # 1 楼答案

    涂漆部件的尺寸可能为0x0。自定义绘制的组件应返回组件的首选尺寸

    将构件添加到框架后,打包框架,以确保框架的大小与显示构件所需的大小完全一致

    当然,在框架中使用或设置适当的布局/约束。在本例中,我将使用默认布局BorderLayout和默认约束CENTER

  2. # 2 楼答案

    安德鲁是对的。我不得不重新布置才能让它工作。我添加了perferredSize()minimumSize()的代码,并添加了对pack()的调用,删除了setLayout(null)。此外,计算高度和宽度时也会遇到问题,它们不会与行和列对齐,并且会将索引抛出边界

    更正了下面的代码

    class Game extends JFrame
    {
    
       private Map map;
    
       public Game()
       {
    
    //      setLayout( null );
          setBounds( 0, 0, 500, 500 );
          setSize( 500, 500 );
          setResizable( false );
          setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    
          Map map = new Map( 500, 500, 50 );
          map.displayConsole();
    
          add( map );
          pack();
          repaint();
          setVisible( true );
       }
    
       public static void main( String[] args )
       {
          // TODO Auto-generated method stub
    
          Game game = new Game();
    
    
       }
    
    }
    
    class Map extends JPanel
    {
    
       private static int WIDTH;
       private static int HEIGHT;
       private static int ROWS;
       private static int COLS;
       private static int TILE_SIZE;
       private static int CLEAR = 0;
       private static int BLOCKED = 1;
    
       private static int[][] GRID;
    
       public Map( int w, int h, int t )
       {
    
          WIDTH = w;
          HEIGHT = h;
          TILE_SIZE = t;
          ROWS = HEIGHT / TILE_SIZE;
          COLS = WIDTH / TILE_SIZE;
    
          GRID = new int[ ROWS ][ COLS ];
    
          for( int row = 0; row < ROWS; row++ )
             for( int col = 0; col < COLS; col++ )
                GRID[row][col] = BLOCKED;
    
          randomMap();
       }
    
       public void randomMap()
       {
          int row = 0;
          int col = 0;
          int turn;
    
          Random rand = new Random();
    
          GRID[row][col] = CLEAR;
    
          do {
             turn = rand.nextInt( 2 ) + 1;
             if( turn == 1 )
                row++;
             else
                col++;
             GRID[row][col] = CLEAR;
          } while( row < ROWS - 1 && col < COLS - 1 );
    
          if( row == ROWS - 1 )
             for( int i = col; i < COLS; i++ )
                GRID[row][i] = CLEAR;
          else
             for( int i = row; i < ROWS; i++ )
                GRID[i][col] = CLEAR;
    
       }
    
       @Override
       public Dimension preferredSize()
       {
    //      return super.preferredSize(); //To change body of generated methods, choose Tools | 
          return new Dimension( WIDTH, HEIGHT );
       }
    
       @Override
       public Dimension minimumSize()
       {
          return preferredSize();
       }
    
    
    
    
       public void paintComponent( Graphics g )
       {
    
          super.paintComponent( g );
          Graphics2D g2d = (Graphics2D) g;
    
          for( int row = 0; row < ROWS; row++ )
             for( int col = 0; col < COLS; col++ )
                if( GRID[row][col] == 1 ) {
                   g2d.setColor( Color.BLACK );
                   g2d.fillRect( row * TILE_SIZE, col * TILE_SIZE,
                           TILE_SIZE, TILE_SIZE );
                } else {
                   g2d.setColor( Color.WHITE );
                   g2d.fillRect( row * TILE_SIZE, col * TILE_SIZE,
                           TILE_SIZE, TILE_SIZE );
                }
       }
    
       public void displayConsole()
       {
    
          for( int row = 0; row < ROWS; row++ ) {
             for( int col = 0; col < COLS; col++ )
                System.out.print( GRID[row][col] + "   " );
             System.out.println( "" );
             System.out.println( "" );
          }
       }
    }