日記

たいとるめんどう [プログラム関係]

最近ゲーム作ってて気づいたどうでもいいTipsをいくつか。

Frameを画面中央に寄せる。
知ってれば割と使いそう。

frame.pack();
Rectangle rect = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
frame.setLocation((rect.width-frame.getWidth())/2, (rect.height-frame.getHeight())/2);
frame.setVisible(true);

ゲームのFPSでの実装。
Thread.sleepは不完全なので最後はwhileで待つと安定するらしい。

int frameWaitTime = 1000000000 / 60;//60FPS
int waitTime = (int)(frameWaitTime - (System.nanoTime() - startTime));
if(waitTime > 0)
{
try
{
Thread.sleep(waitTime/1000000, waitTime%1000000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
long waitFor = startTime + frameWaitTime;
while(System.nanoTime() < waitFor) {}
}

Graphicsで透明色で塗る。
ただアルファが0な色で塗っただけだと透明で塗る=何も起こらないので
気づくのに時間がかかりました。
単にsetBackgroundとclearRectを使えばいいだけの話。

Graphics2D g = image.createGraphics();
g.setBackground(new Color(0,0,0,0));
g.clearRect(24, 0, 16, 8);

at 2011-1-12 0:00 | Comment(0)
名前: 削除パス: 非公開
URL:
添付:(jpg,gif,png,200KBまで)

 
required 0.0399 sec