간단한 게임 엔진에 대한 자습서를 따르고 있습니다. 어떤 이유로 사각형을 채울 때 setColor가 작동하지 않습니다. 빈 화면이 나타납니다. 다른 비슷한 게시물을 살펴 봤지만 그 중 누구도 나를 도우 려하지 않는 것 같았습니다. 여기에 코드입니다 : ELDO에 의해 제안setColor Java가 작동하지 않습니다.
package com.binaryscythe.SA.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
/**
* @author 4nd3r
*
*/
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = -3472639816592189040L;
public static final int WIDTH = 1920, HEIGHT = WIDTH/16 * 9;
private Thread thread;
private boolean running = false;
private Handler handler;
public Game() {
new Window(WIDTH, HEIGHT, "Senum's Adventure", this);
handler = new Handler();
}
public synchronized void start() {
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
try {
} catch(Exception e) {
e.printStackTrace();
}
}
public void run() {
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 100000000/amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime)/ns;
lastTime = now;
while(delta >= 1) {
tick();
delta--;
}
if (running)
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
private void tick() {
handler.tick();
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0, 0, WIDTH, HEIGHT);
handler.render(g);
g.dispose();
bs.show();
}
public static void main(String args[]) {
new Game();
}
}
에
Graphics
캐스트 :그렇지 않으면, 당신도 다음과 같은 코드를 시도 할 수 있습니다! 귀하의 프로그램은 어떻게 운영됩니까? –
@ Jossie Calderon 자신의 창 클래스에 필요한 매개 변수의 개수는 어떻게 알 수 있습니까? – eldo
윈도우와 핸들러 클래스는 코모도 자신이 만든 클래스입니다. 자신의 코드를 테스트하려면'awt.Frame'을 사용하고 GUI를 준비한 후 Game (Canvas를 확장) 클래스를'awt.Frame'에 추가 한 다음'paint' 메소드를 그립니다. – rupinderjeet