如何在Java中创建数组的简单图形显示?

时间:2013-02-08 04:52:01

标签: java arrays 2d graphic

在过去的几天里,我用Java编写了一个(可怕的拼凑在一起)文本RPG游戏。世界上的定位几乎由一个简单的100x100 2D字符串数组控制。然后,当玩家实际进入网格时,我将字符串转换为实际对象。

我想到的是有一个图形显示屏显示这个100x100网格,网格的每个部分都有一个图像,与图像中的内容相对应。

例如,如果块[10] [15]的字符串是“摇滚”,则第10行第15列的网格图形显示部分将显示岩石的图片。

理想情况下,每次循环执行do-while循环时,此图形都会刷新。奇怪的是,我想到的是与早期的口袋妖怪游戏非常相似的东西。

如果我的描述措辞不当或我的问题太模糊,我道歉。我在计算机科学课程中只学了半个学期的java,所以我的知识仅限于我们在一个学期学到的基础知识。我喜欢在课外进行各种项目,比如我(自豪地)编码的文字国际象棋游戏。我更喜欢从头开始创建所有内容,以便在使用各种库之前学习基础知识。

有人可以指出我正在寻找正确的方向或提供更好的方式来展示这个展示吗?

非常感谢您提前。如果对我的代码的引用更能帮助回答我的问题,请告诉我。

3 个答案:

答案 0 :(得分:0)

在我的个人经历中,最容易用于此类任务的是Processing,这是一个轻量级框架,能够提供一个简单的API来绘制东西。

有一个reference,还有很多tutorials因此,即使您不是专家,也不应该很难开始。

作为一个侧节点:为什么要使用字符串来存储各种块?你可以使用像enum更好的东西吗?

答案 1 :(得分:0)

首先,您可以使用枚举而不是上面Jack提到的字符串。例如

private enum Objects {           摇滚(1),硬币(8),医学(45)......等等  }

在数组中,您可以将这些对象存储为数字而不是字符串。

例如:

     boolean stopFlag=false;    
do{  
  //check each element of your world array with the enum and draw it  
  for(int i=0;i<yourObjectsArray.length;i++)
   {
      for(int j=0;j<yourObjectsArray[i].length;j++){
        switch(yourObjectsArray[i][j])
        {
          case Objects.Rock: drawRock(i,j);
                             break;
          case Objects.Coin: drawCoin(i,j);
                             break;
          //and so on....
        }
     }
   }
  //you can also put this into a separate function as drawWorld() and pass the objects.

//Key press checking logic here. If user presses exit key [esc] then you set the stopFlag  as true
  if(keypress==KEY_ESC){ stopFlag=true;}  
}while(!stopFlag);

Draw Rock的一个例子:

private void drawRock(int i,int j){
      //i and j are the cols and row values so you need to resolve them to coordinates.
      //I am assuming u have a 800*600 screen and you mentioned that your world is 100x100 array. Then each of your object is 8*6 units in size so
     xCoord=i*8;
     yCoord=j*6; 
        //So if you have to draw a rock on [10][15] it will resolve as
        //xCoord=10*8-> 80
        //yCoord=15*6-> 90  so it will draw your rock in (80,90) on the screen

//Now you can either open the rock image from your disk now or u maintain one instance of rock at the beginning of the program so that you can use the same image later rather than opening it everytime you encounter a new Rock object in your array.For now I will open it here.

   String path = "C:\\YourGameDirectory\\rock.jpg";
    URL url = new File(path).toURI().toURL();
    BufferedImage rockImg = ImageIO.read(url);

   //draw it to the screen now if you have the graphics instance.
    yourUIPanel.getGraphics().drawImage(rockImg,xCoord,yCoord,yourUIPanel);

  // You may find many resources that teach you how to draw an image on the screen in         Java. You may repeat the same for all the objects.

 }

我希望上面的代码对你有所帮助。如果不是,那就是我的坏事。

您可以尝试this教程系列开始使用。虽然它在C语言中,但它的概念可以帮助你实现上面提到的内容。

答案 2 :(得分:0)

您可能想查看我的旧roguelike游戏Tyrant的源代码:

主要观点:

  • 有一个Map类负责存储代表地图的数据。在Tyrant中,Map将地形和对象位置的存储封装在地图上。
  • 有一个MapPanel类在GUI中显示Map。这与Map本身是分开的 - 将GUI与核心引擎数据分开通常是一个好主意。
  • 有一个Thing类来表示地图上的对象。这包括从怪物,物品,树木和烟雾到玩家角色本身的所有内容。基本上任何不是地形的东西。

至于刷新,它的工作方式是MapPanel根据需要重新绘制自己,查看地图的内容。查看MapPanel.paint(Graphics g)及其调用的各种方法。