Как создать мозаичную карту (у меня просто белый экран)

Мне нужна помощь в создании тайловой карты, я просто получаю белый экран вместо карты с изображениями (фрагментами) на нем. Может ли кто-нибудь помочь с этим?

World.java:

   package game.test.src;

   import java.awt.Graphics;
   import java.awt.Image;
   import java.awt.Rectangle;

   import javax.swing.ImageIcon;

    public class World {

private Rectangle[] blocks;
private Image[] blockImg;
private final int arrayNum = 500;

//Block Images
private Image BLOCK_GRASS, BLOCK_DIRT, BLOCK_STONE, BLOCK_SKY;

private int x, y;

public World(){
    BLOCK_GRASS = new ImageIcon("C:/Users/Pim/Desktop/2D game test/Game test 2/src/game/test/src/images/tile_grass").getImage();
    BLOCK_DIRT = new ImageIcon("C:/Users/Pim/Desktop/2D game test/Game test 2/src/game/test/src/images/tile_dirt").getImage();
    BLOCK_STONE = new ImageIcon("C:/Users/Pim/Desktop/2D game test/Game test 2/src/game/test/src/images/tile_stonek").getImage();
    BLOCK_SKY = new ImageIcon("C:/Users/Pim/Desktop/2D game test/Game test 2/src/game/test/src/images/tile_sky").getImage();
    blocks = new Rectangle[500];
    blockImg = new Image[500];
    
    loadArrays();
}

private void loadArrays()
{
    for(int i = 0; i < arrayNum; i++)
    {
        if(x >= 500){
            x = 0;
            y += 20;
        }
        if(i >= 0 && i < 100)
        {
            blockImg[i] = BLOCK_SKY;
            blocks[i] = new Rectangle(x, y, 20, 20);
        }
        if(i >= 100 && i < 125)
        {
            blockImg[i] = BLOCK_GRASS;
            blocks[i] = new Rectangle(x, y, 20, 20);
        }
        if(i >= 125 && i < 225)
        {
            blockImg[i] = BLOCK_DIRT;
            blocks[i] = new Rectangle(x, y, 20, 20);
        }
        if(i >= 225 && i < 500)
        {
            blockImg[i] = BLOCK_STONE;
            blocks[i] = new Rectangle(x, y, 20, 20);
        }
        x += 20;
        
    }
    
}

public void draw(Graphics g)
{
    for(int i = 0; i< arrayNum; i++){
        g.drawImage(blockImg[i], blocks[i].x, blocks[i].y, null);
    }
}   
}

А вот и GamePanel.java:

    package game.test.src;

import java.awt.*;
import java.awt.event.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements Runnable {
    //Double buffering
    private Image dbImage;
    private Graphics dbg;
    //JPanel variables
    static final int GWIDTH = 500, GHEIGHT = 400;
    static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);
    //Game variables
    private Thread game;
    private volatile boolean running = false;
    //Game Objects
    World world;
    
    public GamePanel(){
        world = new World();
        
        setPreferredSize(gameDim);
        setBackground(Color.WHITE);
        setFocusable(true);
        requestFocus();
        //Handle all key inputs from user
        addKeyListener(new KeyAdapter(){
            @Override
            public void keyPressed(KeyEvent e){
                
            }
            @Override
            public void keyReleased(KeyEvent e){
                
            }
            @Override
            public void keyTyped(KeyEvent e){
                
            }
        });
        
    }
    
    public void run(){
        while(running){
            gameUpdate();
            gameRender();
            paintScreen();
            
        }
    }
    
    private void gameUpdate(){
        if(running && game != null){
            
        }
    }
    
    private void gameRender(){
        if(dbImage == null){ // Create the buffer
            dbImage = createImage(GWIDTH, GHEIGHT);
            if(dbImage == null){
                System.err.println("dbImage is still null!");
                return;
            }else{
                dbg = dbImage.getGraphics();
            }
        }
        //Clear the screen
        dbg.setColor(Color.WHITE);
        dbg.fillRect(0, 0, GWIDTH, GHEIGHT);
        //Draw Game elements
        draw(dbg);
    }
    
    /* Draw all game content in this method */
    public void draw(Graphics g){
        world.draw(g);
    }
    
    private void paintScreen(){
        Graphics g;
        try{
            g = this.getGraphics();
            if(dbImage != null && g != null){
                g.drawImage(dbImage, 0, 0, null);
            }
            Toolkit.getDefaultToolkit().sync(); //For some operating systems
            g.dispose();
        }catch(Exception e){
            System.err.println(e);
        }
    }
    
    public void addNotify(){
        super.addNotify();
        startGame();
    }
    
    private void startGame(){
        if(game == null || !running){
            game = new Thread(this);
            game.start();
            running = true;
        }
    }
    
    public void stopGame(){
        if(running){
            running = false;
        }
    }
    }

и Main.java:

package game.test.src;

import javax.swing.JFrame;

public class Main extends JFrame
{
    GamePanel gp;
    
    public Main()
    {
        gp = new GamePanel();
        setSize(500, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setResizable(false);
        add(gp);
    }
    

    
    public static void main(String[] args)
    {
        Main m = new Main();
    }

}

person Pim Schwippert    schedule 02.09.2012    source источник
comment
Вы используете такие странные пути?   -  person Roman C    schedule 02.09.2012
comment
Можете ли вы привести пример лучшего пути? (Я не знал, как это работает...)   -  person Pim Schwippert    schedule 02.09.2012
comment
Кажется, вы вызываете getGraphics() для компонентов, чтобы получить графический контекст, и не должны этого делать. Ваш учебник говорит вам сделать это? Если да, пожалуйста, покажите нам ссылку на это, потому что это очень неортодоксально. Я должен задаться вопросом, какой учебник или книгу вы используете.   -  person Hovercraft Full Of Eels    schedule 02.09.2012
comment
Это ссылка на серию руководств: youtube.com/playlist?list=PL54DB126285ED0420.   -  person Pim Schwippert    schedule 02.09.2012
comment
Не могли бы вы изменить его на World.class.getResource("images/xxx.gif"))?   -  person Roman C    schedule 02.09.2012
comment
Куда я мог его положить тогда? Я сделал это так: BLOCK_GRASS = World.class.getResource(images/tile_grass); и это дало мне ошибки   -  person Pim Schwippert    schedule 02.09.2012
comment
Их нет в папке src проекта?   -  person Roman C    schedule 02.09.2012
comment
он сказал, что мне нужно изменить тип BLOCK_GRASS на и URL: S   -  person Pim Schwippert    schedule 02.09.2012
comment
Поставьте add(gp) перед setVisible.   -  person Roman C    schedule 02.09.2012
comment
Я только что сделал это, и это не сработало :(   -  person Pim Schwippert    schedule 02.09.2012
comment
Конечно, это не единственное изменение, которое вы должны внедрить paintComponent для панели.   -  person Roman C    schedule 02.09.2012
comment
это дает и ошибку, когда я внедряю paintComponent в класс. он сказал, что я должен был изменить его на paintcontext   -  person Pim Schwippert    schedule 02.09.2012
comment
Хорошо, я опубликую решение?   -  person Roman C    schedule 02.09.2012
comment
Я нашел это! Мне просто нужно было поставить .png в конце пути к изображению...   -  person Pim Schwippert    schedule 02.09.2012
comment
Это то, что вы искали?   -  person Roman C    schedule 02.09.2012
comment
Спасибо за EXIT_ON_CLOSE :)   -  person Roman C    schedule 02.09.2012


Ответы (1)


Я немного изменил ваш код, попробуйте

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class World {

  private Rectangle[] blocks;
  private Image[] blockImg;
  private final int arrayNum = 500;

  //Block Images
  private Image BLOCK_GRASS, BLOCK_DIRT, BLOCK_STONE, BLOCK_SKY;

  private int x, y;

  public World(){
    BLOCK_GRASS = new ImageIcon(Frame1.class.getResource("images/openFile.gif")).getImage();
    BLOCK_DIRT = new ImageIcon(Frame1.class.getResource("images/openFile.gif")).getImage();
    BLOCK_STONE = new ImageIcon(Frame1.class.getResource("images/openFile.gif")).getImage();
    BLOCK_SKY = new ImageIcon(Frame1.class.getResource("images/openFile.gif")).getImage();
    blocks = new Rectangle[500];
    blockImg = new Image[500];

    loadArrays();
  }

  private void loadArrays()
  {
    for(int i = 0; i < arrayNum; i++)
    {
      if(x >= 500){
        x = 0;
        y += 20;
      }
      if(i >= 0 && i < 100)
      {
        blockImg[i] = BLOCK_SKY;
        blocks[i] = new Rectangle(x, y, 20, 20);
      }
      if(i >= 100 && i < 125)
      {
        blockImg[i] = BLOCK_GRASS;
        blocks[i] = new Rectangle(x, y, 20, 20);
      }
      if(i >= 125 && i < 225)
      {
        blockImg[i] = BLOCK_DIRT;
        blocks[i] = new Rectangle(x, y, 20, 20);
      }
      if(i >= 225 && i < 500)
      {
        blockImg[i] = BLOCK_STONE;
        blocks[i] = new Rectangle(x, y, 20, 20);
      }
      x += 20;

    }

  }

  public void draw(Graphics g)
  {
    for(int i = 0; i< arrayNum; i++){
      g.drawImage(blockImg[i], blocks[i].x, blocks[i].y, null);
    }
  }
}

class GamePanel extends JPanel implements Runnable {
  //Double buffering
  private Image dbImage;
  private Graphics dbg;
  //JPanel variables
  static final int GWIDTH = 500, GHEIGHT = 400;
  static final Dimension gameDim = new Dimension(GWIDTH, GHEIGHT);
  //Game variables
  private Thread game;
  private volatile boolean running = false;
  //Game Objects
  World world;

  public GamePanel(){
    world = new World();

    setPreferredSize(gameDim);
    setBackground(Color.WHITE);
    setFocusable(true);
    requestFocus();
    //Handle all key inputs from user
    addKeyListener(new KeyAdapter(){
      @Override
      public void keyPressed(KeyEvent e){

      }
      @Override
      public void keyReleased(KeyEvent e){

      }
      @Override
      public void keyTyped(KeyEvent e){

      }
    });

  }

  public void run(){
    while(running){
      gameUpdate();
//      gameRender();
//      paintScreen();
      repaint();

    }
  }

  private void gameUpdate(){
    if(running && game != null){

    }
  }

  private void gameRender(){
    if(dbImage == null){ // Create the buffer
      dbImage = createImage(GWIDTH, GHEIGHT);
      if(dbImage == null){
        System.err.println("dbImage is still null!");
        return;
      }else{
        dbg = dbImage.getGraphics();
      }
    }
    //Clear the screen
    dbg.setColor(Color.WHITE);
    dbg.fillRect(0, 0, GWIDTH, GHEIGHT);
    //Draw Game elements
    draw(dbg);
  }

  /* Draw all game content in this method */
  public void draw(Graphics g){
    world.draw(g);
  }
  public void paintComponent(Graphics g){
    super.paintComponent(g);
    gameRender();
    paintScreen(g);
  }
  private void paintScreen(Graphics g){
    try{
//      g = this.getGraphics();
      if(dbImage != null && g != null){
        g.drawImage(dbImage, 0, 0, null);
      }
      Toolkit.getDefaultToolkit().sync(); //For some operating systems
//      g.dispose();
    }catch(Exception e){
      System.err.println(e);
    }
  }

  public void addNotify(){
    super.addNotify();
    startGame();
  }

  private void startGame(){
    if(game == null || !running){
      game = new Thread(this);
      game.start();
      running = true;
    }
  }

  public void stopGame(){
    if(running){
      running = false;
    }
  }
}
class Main extends JFrame
{
  GamePanel gp;

  public Main()
  {
    gp = new GamePanel();
    setSize(500, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    add(gp);
    setResizable(false);
    setVisible(true);
  }



  public static void main(String[] args)
  {
    Main m = new Main();
  }

}
person Roman C    schedule 02.09.2012