Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java中的重新绘制_Java_Swing_User Interface_Dictionary_Paint - Fatal编程技术网

java中的重新绘制

java中的重新绘制,java,swing,user-interface,dictionary,paint,Java,Swing,User Interface,Dictionary,Paint,我创造了一个类似于吃豆人的游戏。我已经使用此代码显示了地图 int leveldata1[] = { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 0, 0, 0, 40, 40, 0

我创造了一个类似于吃豆人的游戏。我已经使用此代码显示了地图

int leveldata1[] =
    { 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
      40, -1,  1,  1,  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 40,
      40, 40,  40,  40,  40, 40, 40, 40, 40, 40, 40, 0, 0, 0, 40, 
      40, 0,  1,  1,  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 40, 
      40, 0, 40, 40, 40, 40, 40, 40,  40, 40, 40, 40, 40, 40, 40,
      40, 1, 1, 1, 1, 1, 1, 1,  1, 1, 1, 1, 1, 1, 40, 
      40, 0, 0, 0, 0, 0, 0, 0,  0, 0, 0, 0, 0, 0,  40, 
      40, 40, 40, 40, 40, 40, 40, 0, 0, 40, 40, 40, 40, 40, 40,
      40, 40, 40, 40, 40, 40, 40, 0, 0, 40, 40, 40, 40, 40, 40,
      40, 1, 1, 1, 1, 1, 1, 0, 0, 40, 40, 40, 40, 40, 40, 
      40, 1, 1, 1, 1, 1, 1, 0, 0, 40, 40, 40, 40, 40, 40,
      40, 1, 1, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
      40, 0, 0, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1,  40,
      40, 0, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 5, 40,
      40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40 };

40 = wallblock
1 = dots
0 = grass
5 = finish
2 = enemy

public void paint(Graphics g)
{  
  super.paint(g);
if (ingame == false)
  {
      MainMenu mm = new MainMenu();
      Graphics2D g2d = (Graphics2D) g;
      mm.displayMainMenu(g2d);
  }
  else if(ingame== true)
  {
      if(level == 1)
      {
          Graphics2D g2d = (Graphics2D) g;
          Maze mz1 = new Maze();
          mz1.drawMaze(g2d, level, lives);
          pl1.drawCharacter(g2d);
          DrawScore(g2d);
      }
  }
在我的迷宫课上:

    life = new ImageIcon(MainMenu.class.getResource("../images/life.png")).getImage();
    wallBlock = new     ImageIcon(MainMenu.class.getResource("../images/Wall.png")).getImage();
    grass = new ImageIcon(MainMenu.class.getResource("../images/Grass.png")).getImage();
    dots = new ImageIcon(MainMenu.class.getResource("../images/Dots.png")).getImage();
    enemy = new ImageIcon(MainMenu.class.getResource("../images/Enemy.png")).getImage();
    finish = new ImageIcon(MainMenu.class.getResource("../images/Finish.png")).getImage();

    if (level == 1) 
    {
        for(int i = 0;i<leveldata1.length;i++)
        {   
            if(super.leveldata1[i] == 40)
                g2d.drawImage(wallBlock, mapx,mapy, null);
            if(super.leveldata1[i] <= 5)
                g2d.drawImage(grass, mapx,mapy, null);
            if(super.leveldata1[i] == 1)
                g2d.drawImage(dots, mapx,mapy, null);
            if(super.leveldata1[i] == 5)
            {
                g2d.drawImage(finish, mapx,mapy, null);
            }
            if(super.leveldata1[i] == 2)
            {
                g2d.drawImage(enemy, mapx,mapy, null);
                enemyx[0] = mapx;
                enemyy[0] = mapy;
            }

            if(mapx < 460)
            {
                mapx += 33;
            }
            else
            {
                mapy += 33;
                mapx = 0;
            }

        }
        while(j < lives)
        {
            g2d.drawImage(life, x,0, null);
            x += 40;
            j++;
        }
    }
问题是,我无法在绘制方法中更新贴图,以将点图像更改为指示角色与点图像相交的草图像。就像吃豆人一样

我正在使用actionPerformed

public void actionPerformed(ActionEvent e) {
    repaint(); 
}

希望在我将1=点更改为0=草后,地图会自动更新。使用此行
leveldata1[playerpos-1]=0
。有什么想法吗?

我想你应该在重新绘制()之前调用revalidate()


不知道您的级别参数到底是什么。但是,可能您对数组有一个参考问题:

1)要更快获得更好的帮助,请发布一个。2) “有什么想法吗?”问一个(更具体的)问题。在这个问题的答案中可以找到一些相关的答案。卢克五世(@ljpv14),为我们发布你的跑步记录,以便更好地帮助你解决这一问题。关于Java游戏开发,您可能希望搜索一些参考资料,比如帮助您编写代码等。希望这有帮助。还有一件事,最好在绘制相应图像时使用switch case,而不是if else。除非从容器中删除/添加组件,否则我看不出
revlaidate()
有什么帮助。
public void actionPerformed(ActionEvent e) {
    repaint(); 
}
public void actionPerformed(ActionEvent e) {
    revalidate();
    repaint(); 
}