简体   繁体   English

AWT 图形转换不正确

[英]AWT Graphics not Translating Correctly

I'm making a simple 2-D game for which I'd like to move the camera with the mouse.我正在制作一个简单的 2-D 游戏,我想用鼠标移动相机。 There are loads of better ways to do this, I'm sure, but I decided to try out the Graphics2D method setTransform() .有很多更好的方法可以做到这一点,我敢肯定,但我决定尝试使用 Graphics2D 方法setTransform()

AffineTransform at = new AffineTransform();
at.translate(mousex, 0);

Graphics2D g2d = (Graphics2D)g.create();
g2d.setTransform(at);

However, the graphics don't translate linearly with the mouse, as you can see from the images below.但是,图形不会随鼠标线性平移,如下图所示。 For the first few pixels it seems to move correctly, but it kind of slows down later ?对于前几个像素,它似乎可以正确移动,但稍后会变慢

By the way, the mouse is indicated by the blue circle.顺便说一下,鼠标用蓝色圆圈表示。

在此处输入图片说明

在此处输入图片说明

When the mouse is near the edge of the frame, the graphics moves almost linearly with it to the right.当鼠标靠近框架的边缘时,图形会随着它几乎线性地向右移动。
When the mouse is dragged further to the right, the graphics moves with it, but with a kind of lag (it shouldn't)当鼠标进一步向右拖动时,图形会随之移动,但有一种滞后(不应该)

The white border around the blocks represents the outline of the graphics that should be moving.块周围的白色边框表示应该移动的图形的轮廓。

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

import javax.swing.*;

public class Bruh extends JPanel implements MouseMotionListener
{
    int mousex = 0;

    public static void main(String[] args) {
        JFrame f = new JFrame();
        
        f.setSize(500, 500);
        f.setLocationRelativeTo(null);
        
        f.setUndecorated(true);
        
        f.add(new Bruh());
        
        f.setVisible(true);
    }

    Bruh()
    {
        setBackground(Color.ORANGE);

        addMouseMotionListener(this);
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        AffineTransform at = new AffineTransform();
        at.translate(mousex, 0);

        Graphics2D g2d = (Graphics2D)g.create();
        g2d.setTransform(at);

        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 100, 100, 100);

        g2d.dispose();

        g.setColor(Color.BLUE);
        g.fillOval(mousex-5, 200-5, 10, 10);

        repaint();
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        mousex = e.getX();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        mousex = e.getX();
    }
}

TL;DR TL; 博士

The setTransform(AffineTransform at) function of Graphics2D isn't working as intended. Graphics2D 的setTransform(AffineTransform at)函数未按预期工作。 Any help is appreciated :)任何帮助表示赞赏:)

Okay so I got the answer.好的,所以我得到了答案。 The problem isn't in my code, or with my touchpad scrolling.问题不在于我的代码,也不在于我的触摸板滚动。

On my laptop, I had set the scaling of the display to 125%, causing everything to work normally EXCEPT apps that use default scaling - Java being one of them.在我的笔记本电脑上,我将显示的缩放比例设置为 125%,这使得一切正常,除了使用默认缩放比例的应用程序 - Java 就是其中之一。

The problem was that my mouse moved correctly (because that's what mice do) but the in-built graphics of java were responding to the default scaling of the display ie 125%.问题是我的鼠标移动正确(因为这就是鼠标所做的)但 java 的内置图形响应显示的默认缩放比例,即 125%。 So everything was moving 1.25 times slower than expected.所以一切都比预期慢了 1.25 倍。

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

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