【发布时间】:2021-12-31 01:58:19
【问题描述】:
我有一个矩形,并通过缩放和旋转将其平移到屏幕上的新位置。我想要的是能够检测鼠标光标何时悬停在屏幕上的该对象上。而不是其原始的非翻译形式。我提供了可运行的代码来在下面显示我的问题
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Path2D;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;
public class Main extends JPanel {
static int WIDTH;
static int HEIGHT;
public Main(){
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
g2d.fillRect(WIDTH/2, HEIGHT/2, 100, 100);
g2d.rotate(Math.toRadians(45),WIDTH/2,HEIGHT/2);
g2d.scale(0.5, 0.5);
g2d.setColor(Color.BLUE);
g2d.fillRect(WIDTH/2, HEIGHT/2, 100, 100);
}
public static void main(String[] args){
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
WIDTH = (int) screenSize.getWidth();
HEIGHT = (int) screenSize.getHeight();
Main main = new Main();
JFrame frame = new JFrame();
frame.setTitle("360 ATTACK");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(main);
frame.setVisible(true);
}
}
所以要重新迭代,我想知道鼠标光标何时悬停在蓝色形状上。不是红色的形状。
谢谢
【问题讨论】:
标签: java graphics mouse-cursor