简体   繁体   English

Java-平滑-单击时填充矩形

[英]Java - Slick - Fill Rectangle on Click

I am trying to fill a rectangle on the nearest 10th pixel with a 50x50 size. 我正在尝试使用50x50的尺寸在最近的第10个像素上填充一个矩形。 Whenever this code executes, it shows me the numbers in the syso so I know the locations are being calculated correctly. 每当执行此代码时,它就会向我显示syso中的数字,因此我知道位置计算正确。 However, the rectangle does not appear. 但是,矩形不会出现。 I have a grid drawn but the squares aren't being drawn correctly. 我绘制了一个网格,但是正方形绘制不正确。 What am I doing wrong? 我究竟做错了什么? I'm not getting any Stack Traces from errors or something like that... 我没有从错误或类似的东西得到任何堆栈跟踪...

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);
}

This is my solution, based on your code: 这是我的解决方案,基于您的代码:

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)));
        }
    }
}

The output: 输出: 输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM