日期:2014-05-20  浏览次数:20696 次

请教大家一个简单算法
问题是这样的显示5个图片在屏幕上

                                      3 4

2 5

1

                              当按下右键变成这样:(继续按左键就以此类推逆时针转)

4 5

3 1

2

              按下左键变成这样:(继续按左键就以此类推顺时针转)
2 3

1 4

5


我的方法感觉比较笨重:

switch(listIndex){
case   0:
                                                      g.drawImage(0,   8,   100,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(1,   8,   100,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(2,   16,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(3,   120,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(4,   128,   100,   Graphics.TOP   |   Graphics.LEFT);
break;
case   1:
                                      g.drawImage(1,   8,   100,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(2,   8,   100,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(3,   16,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(4,   120,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(0,   128,   100,   Graphics.TOP   |   Graphics.LEFT);
break;
case   2: g.drawImage(2,   8,   100,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(3,   8,   100,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(4,   16,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(0,   120,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(1,   128,   100,   Graphics.TOP   |   Graphics.LEFT);
break;
case   3: g.drawImage(3,   8,   100,   Graphics.TOP   |   Graphics.LEFT);

g.drawImage(4,   8,   100,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(0,   16,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(1,   120,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(2,   128,   100,   Graphics.TOP   |   Graphics.LEFT);
break;
case   4: g.drawImage(4,   8,   100,   Graphics.TOP   |   Graphics.LEFT);

g.drawImage(0,   8,   100,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(1,   16,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(2,   120,   80,   Graphics.TOP   |   Graphics.LEFT);
g.drawImage(3,   128,   100,   Graphics.TOP   |   Graphics.LEFT);
break;
}

按键改变listIndex的值

有没有简单点的算法呢!

------解决方案--------------------
说一下我个人的看法.

如果图片比较少,这种方法是最简单直观的了.
如果拓展到N多图片,那么最好把图片们都放入一个数组中.好比images[].
设置一个偏移量d = 0;
g.drawImage(image[(0 + d) % n], 8, 100, Graphics.TOP | Graphics.LEFT);