我的任务是使用128*128 px
对游戏进行编程。所以我开始准备Game Java Framework,一切都很好:我有一个JFrame
,里面是Canvas,我可以画画。但我无法正确设置尺寸。我必须使用128*128 px
,但如果框架大小相同,那么它太小了,所以我需要扩展它。
但是如果我向上缩放框架的大小,则画布太小并且不再填充它。如果我只是给它与Frame相同的大小,那么我将不再使用128*128 px
。那么我怎样才能将正常情况画到我的小画布上并随后进行扩展呢?
package de.gaminggears.communitygame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import de.gaminggears.communitygame.display.Display;
public class Game implements Runnable{
private Display display;
public int width, height;
public String title;
private boolean running = false;
private Thread thread;
private BufferStrategy bs;
private Graphics g;
public Game(String title,int width, int height){
this.height = height;
this.width = width;
this.title = title;
}
private void init(){
display = new Display(title, width, height);
}
private void tick(){
}
private void render(){
bs = display.getCanvas().getBufferStrategy();
if(bs == null){
display.getCanvas().createBufferStrategy(3);
return;
}
g = bs.getDrawGraphics();
Graphics2D g2d = (Graphics2D) g;
//Clear Screen
g2d.clearRect(0, 0, width, height);
//Draw Here!
g2d.setColor(Color.RED);
g2d.fillRect(10, 50, 50, 70);
g2d.fillRect(0, 0, 10, 10);
//End Drawing
g2d.scale(5, 5);
bs.show();
g.dispose();
}
public void run(){
init();
while(running){
tick();
render();
}
stop();
}
public synchronized void start(){
if(running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop(){
if(!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}