isotest.java
来自「java learn PPT java learn PPT java learn」· Java 代码 · 共 124 行
JAVA
124 行
import java.applet.*;
import java.awt.*;
public class IsoTest extends Applet implements Runnable
{
// 动画线程
private Thread animation;
private VolatileGraphics offscreen;
// 一个等大场景
private IsoScene scene;
public void init()
{
// 初始化场景
scene = new IsoScene(getBounds());
// 创建IsoTileGroup
IsoTileGroup tileGroup = new IsoTileGroup();
tileGroup.init(this);
int x = -getSize().width *2 - (IsoTileGroup.TILE_WIDTH /2);
int y = -getSize().height*2 - (IsoTileGroup.TILE_HEIGHT/2);
int width = getSize().width *4;
int height = getSize().height*4;
// 用iso砖块填充场景-注意offset标志是如何让其他的行偏移产生互锁的
IsoTile tile;
boolean offset = false;
while(y < height)
{
while(x < width)
{
tile = new IsoTile(tileGroup);
tile.setPos(x, y);
// 将砖块作为非参照物添加进来
scene.add(tile, false);
x += IsoTileGroup.TILE_WIDTH;
}
offset = !offset;
if(offset) x = -getSize().width*2;
else x = -getSize().width*2 - (IsoTileGroup.TILE_WIDTH /2);
y += IsoTileGroup.TILE_HEIGHT/2;
}
// 创建一个IsoManGroup并创建一个IsoMan角色
IsoManGroup group = new IsoManGroup();
group.init(this);
IsoMan isoMan = new IsoMan(group);
isoMan.setPos(getSize().width/2, getSize().height/2);
// 将isoman作为参照物添加到场景中
scene.add(isoMan, true);
// 注册场景让它接收键盘事件
addKeyListener(scene);
offscreen = new VolatileGraphics(this);
animation = new Thread(this);
AnimationStrip.observer = this;
} // init
public void start()
{
// 启动动画线程
animation.start();
}
public void stop()
{
animation = null;
}
public void run()
{
Thread t = Thread.currentThread();
while (t == animation)
{
repaint();
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
break;
}
}
} // run
public void update(Graphics g)
{
// 更新场景
scene.update();
paint(g);
}
public void paint(Graphics g)
{
Graphics2D bg = (Graphics2D)offscreen.getValidGraphics();
bg.setPaint(Color.WHITE);
bg.clearRect(0, 0, getSize().width, getSize().height);
// 绘制场景
scene.paint(bg);
g.drawImage(offscreen.getBuffer(), 0, 0, this);
} // paint
} // IsoTest
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?