マウスの位置を取得し、カーソルがボタンの上にあるときにボタンを点灯させようとしています。ただし、マウスの位置が正しく更新されていません。現在、ボタンのはるか上にいると点灯するため、yの位置がめちゃくちゃになっていると思います。
間違っている私のコードは次のとおりです。
public void mouseMoved(MouseEvent e) {
Window.mse = new Point((e.getX()) + ((Frame.size.width - Window.myWidth)/2), (e.getY()) + (Frame.size.height - (Window.myHeight)/2));
}
これはウィンドウ ファイルです。
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
public class Window extends JPanel implements Runnable {
public Thread thread = new Thread(this);
public static Image[] tileset_ground = new Image[100];
public static Image[] tileset_air = new Image[100];
public static Image[] tileset_resources = new Image[100];
public static int myWidth, myHeight;
public static boolean isFirst = true;
public static Point mse = new Point(0, 0);
public static Room room;
public static Levels levels;
public static Shop shop;
public Window(Frame frame) {
frame.addMouseListener(new KeyHandler());
frame.addMouseMotionListener(new KeyHandler());
thread.start();
}
public void define() {
room = new Room();
levels = new Levels();
shop = new Shop();
tileset_resources[0] = new ImageIcon("resources/cell.png").getImage();
levels.loadLevels(new File("levels/level1.level"));
for(int i=0;i<tileset_ground.length; i++) {
tileset_ground[i] = new ImageIcon("resources/tileset_ground.png").getImage();
tileset_ground[i] = createImage(new FilteredImageSource(tileset_ground[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32)));
}
for(int i=0;i<tileset_air.length; i++) {
tileset_air[i] = new ImageIcon("resources/tileset_air.png").getImage();
tileset_air[i] = createImage(new FilteredImageSource(tileset_air[i].getSource(), new CropImageFilter(0, 32 * i, 32, 32)));
}
}
public void paintComponent(Graphics g) {
if(isFirst) {
myWidth = getWidth();
myHeight = getHeight();
define();
isFirst = false;
}
g.setColor(new Color(70, 70, 70));
g.fillRect(0, 0, myWidth, myHeight);
g.setColor(new Color(0, 0, 0));
g.drawLine(room.block[0][0].x, room.block[room.worldHeight - 1][0].y + room.blockSize, room.block[0][room.worldWidth - 1].x + room.blockSize, room.block[room.worldHeight - 1][0].y + room.blockSize);
g.drawLine(room.block[0][0].x, room.block[room.worldHeight - 1][0].y + 1 + room.blockSize, room.block[0][room.worldWidth - 1].x + room.blockSize, room.block[room.worldHeight - 1][0].y + 1 + room.blockSize);
room.draw(g); //Draws room
shop.draw(g); //Draws shop
}
public void run() {
while(true) {
if(!isFirst) {
room.physic();
}
repaint();
try {
Thread.sleep(1);
} catch(Exception e) {
e.printStackTrace();
}
}
}
}