I'm currently researching how to utilize aspect ratio in a magnified area full of pixels. My main focus of this project is to match the mouse cursor to the dot on the screen, while the mouse cursor is moving around in the area. However, I am unable to completely match the mouse cursor to the dot.
This first image here shows how it is supposed to look when the mouse is moving around in the magnified area. I use the program, PSR, or Problem Steps Recorder for short, in order to capture the mouse cursor and the background screenshot together.
This second image here shows the same scenario, only that the mouse cursor is moved to the far right and far down of the area. This is just one of the situations where the mouse cursor isn't directly on top of the dot, and the dot moves faster than the mouse cursor.
Here's the current code:
public class DeadGame {
public InputHandler inputHandler;
public DeadGame(GameComponent g) {
inputHandler = new InputHandler();
g.addMouseListener(inputHandler);
g.addMouseMotionListener(inputHandler);
}
public void tick() {
}
public void render(int[] data) {
/*
* Codes that are marked with // are debugging codes; tinkering with the aspect ratios and math to try to get the dot to stay with the cursor.
* Apparently, this is incorrect.
*
* */
// double ratio = (double) GameComponent.WIDTH / (double) GameComponent.HEIGHT;
// int x = (int) ((inputHandler.mouseX / GameComponent.SCALE) * ratio);
// int y = (int) ((inputHandler.mouseY / GameComponent.SCALE) * ratio);
//
// System.out.printf("Mouse: %d, %d (Correct: %d, %d) Aspect Ratio: %f\n", x, y, (int) (inputHandler.mouseX * ratio), (int) (ratio * inputHandler.mouseY), (double) GameComponent.WIDTH / (double) GameComponent.HEIGHT);
//
// int length = y * (GameComponent.WIDTH / GameComponent.SCALE) + x;
// if (length < data.length && length > 0)
// data[length] = -1;
int x = inputHandler.mouseX / GameComponent.SCALE;
int y = inputHandler.mouseY / GameComponent.SCALE;
int length = y * (GameComponent.WIDTH / GameComponent.SCALE) + x;
if (length < data.length && length > 0)
data[length] = -1;
}
}
InputHandler code:
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
How do I manage to magnify the area? Below are the codes used to create the effect.
Variables used:
public static final int WIDTH = 640;
public static final int HEIGHT = 480;
public static final int SCALE = 4;
Initialization code used for the background area. I used BufferedImage and java.awt.Canvas:
public GameComponent() {
threadRunning = false;
Dimension d = new Dimension(WIDTH, HEIGHT);
this.setPreferredSize(d);
this.setMaximumSize(d);
this.setMinimumSize(d);
this.setSize(d);
image = new BufferedImage(WIDTH / SCALE, HEIGHT / SCALE, BufferedImage.TYPE_INT_ARGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
game = new DeadGame(this);
}
Code used to render it to the back buffer. I suspect that this is the place where the aspect ratio is off entirely here, but I can't really tell. Main suspects are the java.awt.Canvas.getWidth() and java.awt.Canvas.getHeight() functions.
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
bs = this.getBufferStrategy();
}
int w = this.getWidth();
int h = this.getHeight();
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, w, h);
clear();
game.render(pixels);
g.drawImage(image, 0, 0, w, h, null);
g.dispose();
bs.show();
}
So, what do I have to do to fix this issue? Thanks in advance.