这段代码应该创建一个JFrame并在其上绘画......但它并没有

时间:2014-05-14 04:38:21

标签: java swing graphics jframe jpanel

我已经盯着这几天......

我认为Imageicon部分是正确的,并且正在引用正确的对象。 它与创建电路板,地图和东西有关,我对此并不是很好。

public class Maze {
    public static final int SCREEN_SIZE = 1024;
    public static void main(String[] args){
        new Maze();
    }

    // help from: http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
    public Maze(){
        JFrame f = new JFrame();
        f.setTitle("Maze");
        f.add(new Board());
        f.setSize(SCREEN_SIZE,SCREEN_SIZE);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

因此,您可以创建屏幕,制作电路板,获取图像,然后根据阵列的内容进行绘制。

public class Board extends JPanel implements ActionListener {
    public static final int TILE_SIZE = 32;
    private Timer timer;
    public Map m;

    public Board(){
    m = new Map();
    timer = new Timer(25, this);
    timer.start();
}

    @Override
    public void actionPerformed(ActionEvent e) {
        repaint(); // TODO Auto-generated method stub
    }

    public void paint(Graphics g){
        super.paint(g);
        for (int y = 0; y < TILE_SIZE; y++) {
            for (int x = 0; x < TILE_SIZE; x++) {
                if(m.Map[x][y] == 0) {
                      g.drawImage(m.getFloor(), x * 32, y * 32, null);
                }
                if (m.Map[x][y] == 1) {
                    g.drawImage(m.getWall(), x * 32, y * 32, null);
                }
            }
        }
    }
}

public class Map {
    public static Image floor, wall;
    public int Map[][] = new int[Maze.SCREEN_SIZE / Board.TILE_SIZE][Maze.SCREEN_SIZE / Board.TILE_SIZE];

    public Map() {
        ImageIcon img = new ImageIcon("C://Users/Brian2/Pictures/backgrounds/FLOOR.png");
        floor = img.getImage();
        img = new ImageIcon("C://Users/Brian2/Pictures/backgrounds/WALL");
        wall = img.getImage();
        GenMap();
    }

    public void GenMap() {
        for(int i = 0; i < Maze.SCREEN_SIZE / Board.TILE_SIZE; i++){
            for(int j = 0; j < Maze.SCREEN_SIZE / Board.TILE_SIZE; j++){
                Map[i][j] = 1;
            }
        }
    }

    public Image getWall() {
        return wall;
    }

    public Image getFloor() {
        return floor;
    }

    public void printMap() {}
}

在我运行它时打开的JPanel上不会显示任何内容。我不知道出了什么问题。没有错误,我已经被困了大约4天。

1 个答案:

答案 0 :(得分:1)

我试图重现你的问题,但它对我有用。 取决于你使用的JDK,我使用了1.6.0_45。

但是我假设您想要绘制一个&#34;世界&#34;。

的地图

我重新整理了你的代码,因为我觉得你试图做一些不必要的事情,比如保持一个整数数组来模拟世界,但事实上你将它翻译成图像。

董事会成员:

package org.myself.mazetest;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class Board extends JPanel implements ActionListener
{
   private static final int BOARD_SIZE = 1024;

   private static final int FRAME_VERTICAL_BORDERS = 40;

   private static final int FRAME_HORIZONTAL_BORDERS = 20;

   private static final int TILE_SIZE = 32;

   private static final int TIMER_DELAY = 25;

   private final Timer timer;

   private final int tileCount;

   public Map map;


   public Board( )
   {
      tileCount = Board.BOARD_SIZE / Board.TILE_SIZE;
      map = new Map( tileCount );
      timer = new Timer( Board.TIMER_DELAY, this );
      timer.start();
      setSize( Board.BOARD_SIZE, Board.BOARD_SIZE );
   }


   @Override
   public void actionPerformed( final ActionEvent e )
   {
      repaint(); // TODO Auto-generated method stub
   }


   @Override
   public void paintComponent( final Graphics g )
   {
      super.paintComponent( g );
      Image img;
      for ( int y = 0; y < tileCount; y++ )
      {
         for ( int x = 0; x < tileCount; x++ )
         {
            img = map.getImageFor( x, y );
            if ( img != null )
            {
               g.drawImage( img, x * Board.TILE_SIZE, y * Board.TILE_SIZE, null );
            }
         }
      }

   }


   public static void main( final String[] args )
   {
      final JFrame f = new JFrame();
      f.setTitle( "Maze" );
      f.setLayout( null );
      f.add( new Board() );
      f.setSize( Board.BOARD_SIZE + Board.FRAME_HORIZONTAL_BORDERS,
            Board.BOARD_SIZE + Board.FRAME_VERTICAL_BORDERS );
      f.setLocationRelativeTo( null );
      f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
      f.setVisible( true );
   }
}

Map类:

package org.myself.mazetest;

import java.awt.Image;

import javax.swing.ImageIcon;

public class Map
{
   private final Image floor, wall;

   private final Image mapArray[][];

   private final int tileCount;


   public Map( final int tileCount )
   {
      mapArray = new Image[tileCount][tileCount];
      this.tileCount = tileCount;
      ImageIcon img = new ImageIcon( "C:\\Users\\Brian2\\Pictures\\backgrounds\\FLOOR.png" );
      floor = img.getImage();
      //TODO: sure without extension?
      img = new ImageIcon( "C:\\Users\\Brian2\\Pictures\\backgrounds\\WALL" ); 
      wall = img.getImage();
      //now build up the world's map :)
      genMap();
   }


   private void genMap()
   {
      //we build the inner area
      //that's why we start from 1, and go until tileCount - 1
      for ( int row = 1; row < (tileCount - 1); row++ )
      {
         for ( int col = 1; col < (tileCount - 1); col++ )
         {
            mapArray[row][col] = floor;
         }
      }

      //we build the wall itself
      final int firstRow = 0;
      final int lastRow = tileCount - 1;
      final int firstCol = 0;
      final int lastCol = tileCount - 1;
      for ( int col = 0; col < tileCount; col++ )
      {
         mapArray[firstRow][col] = wall;
         mapArray[lastRow][col] = wall;
      }
      for ( int row = 0; row < tileCount; row++ )
      {
         mapArray[row][firstCol] = wall;
         mapArray[row][lastCol] = wall;
      }

      //this also can be done as follows, but I don't want do make confusion
      /*
      for ( int colOrRow = 0; colOrRow < tileCount; colOrRow++ )
      {
         mapArray[firstRow][colOrRow] = wall;
         mapArray[lastRow][colOrRow] = wall;

         mapArray[colOrRow][firstCol] = wall;
         mapArray[colOrRow][lastCol] = wall;
      }
      */
   }


   public Image getImageFor( final int col, final int row )
   {
      return mapArray[row][col];
   }

}

附上你找到输出(从谷歌获得墙和地板图像)。 The output of the main method

请检查您的墙上图片,在您的示例中它没有扩展名......这可能会导致某些图像读取API出现问题。

来自Maze class I的代码移动到了board,因为事实上它只是一个框架创建,我认为不需要为它使用单独的类。

我试图给出详细的名字和评论,但如果不清楚的话,请随时回复我。