java中怎么实现生成游戏中的格子随鼠标移动高亮
时间: 2024-01-23 22:01:58 浏览: 34
在Java中,可以使用Swing编写GUI应用程序,实现生成游戏中的格子,并且添加鼠标监听器,实现鼠标移动时高亮显示所在的格子。
以下是示例代码:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class GridPanel extends JPanel implements MouseMotionListener {
private int rows; // 行数
private int cols; // 列数
private int cellWidth; // 格子宽度
private int cellHeight; // 格子高度
private int mouseX = -1; // 鼠标x坐标
private int mouseY = -1; // 鼠标y坐标
public GridPanel(int rows, int cols, int cellWidth, int cellHeight) {
this.rows = rows;
this.cols = cols;
this.cellWidth = cellWidth;
this.cellHeight = cellHeight;
int panelWidth = cols * cellWidth;
int panelHeight = rows * cellHeight;
setPreferredSize(new Dimension(panelWidth, panelHeight));
addMouseMotionListener(this);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int x = j * cellWidth;
int y = i * cellHeight;
Color color = Color.WHITE;
if (i == mouseY / cellHeight && j == mouseX / cellWidth) {
color = Color.YELLOW; // 高亮显示所在格子
}
g.setColor(color);
g.fillRect(x, y, cellWidth, cellHeight);
g.setColor(Color.BLACK);
g.drawRect(x, y, cellWidth, cellHeight);
}
}
}
@Override
public void mouseMoved(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
repaint();
}
@Override
public void mouseDragged(MouseEvent e) {
}
public static void main(String[] args) {
int rows = 10;
int cols = 10;
int cellWidth = 30;
int cellHeight = 30;
JFrame frame = new JFrame("GridPanel");
GridPanel panel = new GridPanel(rows, cols, cellWidth, cellHeight);
frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
```
在这个示例中,我们创建了一个继承自JPanel的GridPanel类,该类包含了rows行cols列的格子,每个格子的宽度为cellWidth,高度为cellHeight。我们添加了鼠标移动监听器,实现鼠标移动时高亮显示所在的格子。在paintComponent()方法中,我们使用Graphics对象绘制了所有的格子,并根据鼠标位置高亮显示所在的格子。最后,在main()方法中创建了一个JFrame窗口,并将GridPanel添加到其中进行展示。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)