简体   繁体   English

Java对齐网格

[英]Java snap to grid

Okay so I'm trying to make things snap to a grid I have. 好的,所以我试图使内容与我拥有的网格对齐。

This is how I snap to the screen itself: 这就是我捕捉屏幕本身的方式:

int finalCalcX = (mouseX / Handler.gridSpace32) * Handler.gridSpace32;
int finalCalcY = (mouseY / Handler.gridSpace32) * Handler.gridSpace32;

The variable names speak for themselves I think. 我认为变量名说明了一切。 Mouse coordinates divided by my tiles being 32x32 pixels, times that again to get the snap-to-grid functionality. 鼠标坐标除以我的图块除以32x32像素,再乘以该倍数即可获得对齐网格功能。

Now this works fine for the screen, but when I want to add it to the "map" itself, I can't just add my map x and y offsets to that, it gets messed up. 现在,这在屏幕上可以正常使用,但是当我想将其添加到“地图”本身时,我不能仅将地图的x和y偏移量添加到该位置,就搞砸了。

I've played around with it for about two days, and I also got it to snap to the map itself, but when I'm say, halfway in the map on both axis, the mouseX and mouseY messes the grid thing up. 我已经玩了大约两天,而且我还可以将它捕捉到地图本身,但是当我说在地图的两个轴上的一半位置时,mouseX和mouseY将网格内容弄乱了。 It's kind of hard for me to explain, but the offset from the 0, 0 (every origins position, even the screen) PLUS the maps offset when you move away from the origin, gets added to the distance between the cursor itself and the transparent snap-to-grid tile that I'm using to test. 我很难解释,但是从0、0(每个原点位置,甚至是屏幕)的偏移加上从原点移开时的地图偏移,都会加到光标本身和透明对象之间的距离我正在测试的对齐网格图块。 Basically the offset between the maps origin, and the camera, is for some reason the same offset between the cursor and the transparent tile. 基本上,由于某些原因,地图原点和相机之间的偏移量与光标和透明图块之间的偏移量相同。 So the further into the map i move, the further away the tile gets from the cursor, and eventually moves outside the screen width and height... When I move further into the map, I want the snap-to-grid functionality to stay corret, no matter where on the map I am. 因此,我向地图移动的越远,图块与光标的距离就越远,最终移动到屏幕的宽度和高度之外...当我向地图进一步移动时,我希望保持网格对齐功能corret,无论我在地图上的什么位置。

Render method: 渲染方法:

for (int y = startY; y < endY; y++) {
    for (int x = startX; x < endX; x++) {
        gridSpace(graphics, (int) (x * Handler.gridSpace32 - handler.getCamera().getOffsetX()),
                    (int) (y * Handler.gridSpace32 - handler.getCamera().getOffsetY()));

        checkHighlight(graphics);
    }
}

The gridSpace is the grid itself. gridSpace是网格本身。 Here is what's in the highlight at the moment: 这是目前的亮点:

int finalCalcX = (mouseX / Handler.gridSpace32) * Handler.gridSpace32;
int finalCalcY = (mouseY / Handler.gridSpace32) * Handler.gridSpace32;

graphics.setColor(new Color(100, 200, 100, 3));
graphics.fillRect(finalCalcX, finalCalcY, Handler.gridSpace32, Handler.gridSpace32);

Sorry for my terrible explanation skills, but that's the best I can do. 对不起,我的解释能力很差,但这是我所能做到的。 What am I doing wrong? 我究竟做错了什么?

I think I recreated what you are getting, and the problem lies with usage of incorrect sign in calculations of camera position and translation. 我想我重新创建了所得到的东西,问题出在相机位置和平移计算中使用不正确的符号。 Executing code below you should get similar behavior to what you described, while uncommenting commented lines (while commenting out the ones that follow them) should give you correct behavior. 执行下面的代码,您应该得到与所描述的行为相似的行为,而对注释行取消注释(同时注释掉其后的注释行)则将为您提供正确的行为。

package test;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

public class SnapToGrid extends JPanel implements MouseMotionListener{

    private int[] camera;
    private int[] mouse;
    private final int gridSize = 16;

    SnapToGrid() {
        camera = new int[2];
        mouse = new int[2];
        setFocusable(true);
        addMouseMotionListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        //g2.translate(-camera[0], -camera[1]);
        g2.translate(camera[0], camera[1]);

        //draw background
        for (int i = 0; i < 9; i++)
            for (int j = 0; j < 9; j++)
            {
                Color c = ((j*9) + i) % 2 == 0 ? Color.black : Color.white;
                g2.setColor(c);
                g2.fillRect(i*gridSize, j*gridSize, gridSize, gridSize);
            }
        g2.setColor(Color.blue);
        int[] snappedPos = getSnappedMousePosition();
        g2.fillRect(snappedPos[0], snappedPos[1], gridSize, gridSize);
    }

    private int[] getSnappedMousePosition() {
        return new int[] {
            camera[0] + mouse[0] - ((camera[0] + mouse[0]) % gridSize),
            camera[1] + mouse[1] - ((camera[1] + mouse[1]) % gridSize)  
        };
    }

    public static void main (String[] args) {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new SnapToGrid());
        frame.pack();
        frame.setVisible(true);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        //camera[0] -= e.getX() - mouse[0]; 
        //camera[1] -= e.getY() - mouse[1];
        camera[0] += e.getX() - mouse[0]; 
        camera[1] += e.getY() - mouse[1];

        mouse[0] = e.getX();
        mouse[1] = e.getY();
        repaint();

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mouse[0] = e.getX();
        mouse[1] = e.getY();
        repaint();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(gridSize * 18, gridSize * 18);
    }

}

One thing that I really don't like with your code, is that you are not using transformations. 我真的不喜欢您的代码的一件事是您没有使用转换。 Using transformations allows you to separate world from viewport, submits to far easier debugging of issues like this here, and most importantly, if you want to add things like scaling or rotating later on, you only need to add few lines, as opposed to rewriting half of your render method. 使用转换使您可以将世界与视口分开,在此处轻松调试此类问题,最重要的是,如果您想添加诸如缩放或稍后旋转之类的内容,则只需添加几行,而无需重写渲染方法的一半。

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

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