单击Java-Slick-Fill矩形
我正在尝试用50x50大小填充一个最接近10像素的矩形。每当执行此代码时,它都会向我显示syso中的数字,这样我就知道正确计算了位置。但是,矩形不会出现。我绘制了一个网格,但是没有正确绘制正方形。我做错了什么?我没有从错误或类似的东西中得到任何堆栈跟踪单击Java-Slick-Fill矩形,java,graphics,grid,fill,slick2d,Java,Graphics,Grid,Fill,Slick2d,我正在尝试用50x50大小填充一个最接近10像素的矩形。每当执行此代码时,它都会向我显示syso中的数字,这样我就知道正确计算了位置。但是,矩形不会出现。我绘制了一个网格,但是没有正确绘制正方形。我做错了什么?我没有从错误或类似的东西中得到任何堆栈跟踪 if(gc.getInput().isMousePressed(0)){ float f1 = (float) Math.ceil(gc.getInput().getMouseX() / 16 * 10); float f2 =
if(gc.getInput().isMousePressed(0)){
float f1 = (float) Math.ceil(gc.getInput().getMouseX() / 16 * 10);
float f2 = (float) Math.ceil(gc.getInput().getMouseY() / 12 * 10);
gc.getGraphics().fillRect(f1, f2, 50, 50);
System.out.println("Filled: " + f1 + "x" + f2);
}
这是我的解决方案,基于您的代码:
package game;
import entity.Block;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import world.Camera;
import world.World;
import java.util.ArrayList;
import java.util.List;
public class TestState extends BasicGameState {
/**
* This variable sets
* the gird size, and the block's
* size.
*/
private final int size = 50;
/**
* This list contains/stores the blocks
* who going to be rendered.
*/
private final List<Block> blocks = new ArrayList<>();
@Override
public int getID() {
return 0;
}
@Override
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
}
@Override
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
/**
* DRAW THE GRID
*
* This can be confusing
* for the first look, but its logical.
*/
g.setColor(Color.white);
for (int i = 0; i < 50; i++) {
float y = i * size;
float x = 0;
g.drawLine(x, y, gc.getWidth(), y);
for (int i2 = 0; i2 < 50; i2++) {
float x2 = i2 * size;
g.drawLine(x2, y, x2, gc.getHeight());
}
}
// DRAW EACH BLOCKS (this is a for-each loop)
for (Block block : blocks) {
block.render(gc, g);
}
}
@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
// ADD NEW BLOCK IF MOUSE PRESSED
if (gc.getInput().isMouseButtonDown(0)) {
// get the x and y positions
float x = (float) Math.ceil(gc.getInput().getMouseX() / size * size);
float y = (float) Math.ceil(gc.getInput().getMouseY() / size * size);
/**
* The block class has a
* special constructor because
* I built it for my game.
* You should build your own.
*
* The block is *size by *size sized :D
* I mean the *size is the variable.
* Ohh, and it's uses the x & y positions.
*/
blocks.add(new Block(new World(new Camera()), new Camera(), new Rectangle(x, y, size, size)));
}
}
}
打包游戏;
导入实体.Block;
导入org.newdawn.slick.Color;
导入org.newdawn.slick.GameContainer;
导入org.newdawn.slick.Graphics;
导入org.newdawn.slick.SlickException;
导入org.newdawn.slick.geom.Rectangle;
导入org.newdawn.slick.state.BasicGameState;
导入org.newdawn.slick.state.StateBasedGame;
进口世界相机;
进口世界。世界;
导入java.util.ArrayList;
导入java.util.List;
公共类TestState扩展了BasicGameState{
/**
*此变量设置为
*网格大小和块的
*尺寸。
*/
私有最终整数大小=50;
/**
*此列表包含/存储块
*谁将被呈现。
*/
私有最终列表块=新的ArrayList();
@凌驾
公共int getID(){
返回0;
}
@凌驾
public void init(GameContainer gc,StateBasedGame sbg)引发异常{
}
@凌驾
公共void呈现(GameContainer gc、StateBasedGame sbg、Graphics g)引发异常{
/**
*画网格
*
*这可能令人困惑
*乍一看,这是合乎逻辑的。
*/
g、 setColor(Color.white);
对于(int i=0;i<50;i++){
浮动y=i*尺寸;
浮动x=0;
g、 绘制线(x,y,gc.getWidth(),y);
对于(inti2=0;i2<50;i2++){
浮动x2=i2*尺寸;
g、 抽绳(x2,y,x2,gc.getHeight());
}
}
//绘制每个块(这是一个针对每个循环的图)
用于(块:块){
块渲染(gc,g);
}
}
@凌驾
公共无效更新(GameContainer gc、StateBasedGame sbg、int delta)引发异常{
//如果按下鼠标,则添加新块
if(gc.getInput().isMouseButtonDown(0)){
//得到x和y的位置
float x=(float)Math.ceil(gc.getInput().getMouseX()/size*size);
float y=(float)Math.ceil(gc.getInput().getMouseY()/size*size);
/**
*block类有一个
*特殊构造函数,因为
*我是为我的比赛而建的。
*你应该建立你自己的。
*
*该块是*大小*大小:D
*我的意思是*大小是变量。
*哦,它使用x&y位置。
*/
添加(新块(新世界(新摄影机())、新摄影机()、新矩形(x、y、大小、大小));
}
}
}
输出:
事实上我让它工作了。我所做的是为一个名为TileMapper的新类创建一个ArrayList。在那门课上,我保存了颜色、位置和其他数据。如果我想要空白的,颜色是黑色的。这有点奇怪,不过还是要感谢你的帮助。哦,所以你试着在网格中放置矩形。在我的解决方案中,我只是在网格上移动了一个矩形。(跟着鼠标)我更新了答案。它应该对某人有用。