重绘在java中

时间:2012-11-28 03:06:54

标签: java swing user-interface dictionary paint

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

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++;
        }
    }

我可以通过使用键事件和以下代码成功检测字符是否与点图像相交。

if(leveldata1[playerpos-1] != 40)
{
    if(leveldata1[playerpos-1] == 1)
    {
        leveldata1[playerpos-1] = 0;
        score += 5;
        level1totalpoints -=1;
    }
    if(leveldata1[playerpos-1] == 5)
    {
        if(level1totalpoints == 0)
        {
            level = level + 1;
        }
    }
    playerpos = playerpos -1;
    pl1.moveCharacter(-33, 0);
 }

问题是我无法在我的paint方法中更新我的地图,将点图像更改为草图像,表示该字符与点图像相交。就像pacman一样。

我正在使用actionPerformed

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

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

2 个答案:

答案 0 :(得分:2)

我认为你应该在重绘()This might resolve your problem:

之前调用revalidate()
public void actionPerformed(ActionEvent e) {
    revalidate();
    repaint(); 
}

答案 1 :(得分:0)

不知道您的关卡参数到底是什么。但也许您对数组有参考问题:Java: How to pass byte[] by reference?