public void mouseEntered(MouseEvent e) { if(state==GameState.pause){ state=GameState.runing; } } public void mouseExited(MouseEvent e) { if(state==GameState.runing){ state=GameState.pause; } } public void mouseMoved(MouseEvent e){ int x=e.getX(); int y=e.getY(); if(state==GameState.runing){ hero.moveTo(x, y); } }
时间: 2024-04-21 10:26:19 浏览: 13
这是一个 MouseAdapter 类,其中包含了三个方法:mouseEntered、mouseExited 和 mouseMoved。
- mouseEntered 方法:当鼠标进入窗口时,如果游戏状态为 pause,将其改为 runing。
- mouseExited 方法:当鼠标离开窗口时,如果游戏状态为 runing,将其改为 pause。
- mouseMoved 方法:当鼠标在窗口中移动时,如果游戏状态为 runing,将英雄对象的位置移动到鼠标所在的位置。其中,getX() 和 getY() 方法分别获取了鼠标相对于窗口左上角的横纵坐标。
相关问题
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class DrawingBoard extends JPanel implements MouseMotionListener, MouseListener { private int lastX, lastY; public DrawingBoard() { setPreferredSize(new Dimension(400, 400)); setBackground(Color.WHITE); addMouseMotionListener(this); addMouseListener(this); } public void paintComponent(Graphics g) { super.paintComponent(g); Font font = new Font("Arial", Font.BOLD, 20); g.setFont(font); g.setColor(Color.BLUE); g.drawString("Hello World!", 50, 50); } public void mouseDragged(MouseEvent e) { Graphics g = getGraphics(); g.drawLine(lastX, lastY, e.getX(), e.getY()); lastX = e.getX(); lastY = e.getY(); } public void mousePressed(MouseEvent e) { lastX = e.getX(); lastY = e.getY(); } public void mouseMoved(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public static void main(String[] args) { JFrame frame = new JFrame("Drawing Board"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new DrawingBoard()); frame.pack(); frame.setVisible(true); } }加注释
下面是对这段 Java 代码的注释:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// DrawingBoard 类继承了 JPanel 类并实现了 MouseMotionListener 和 MouseListener 接口
public class DrawingBoard extends JPanel implements MouseMotionListener, MouseListener {
private int lastX, lastY; // 上一次鼠标移动的位置
// 构造函数
public DrawingBoard() {
setPreferredSize(new Dimension(400, 400)); // 设置 DrawingBoard 的首选大小
setBackground(Color.WHITE); // 设置 DrawingBoard 的背景颜色为白色
addMouseMotionListener(this); // 添加鼠标移动事件监听器
addMouseListener(this); // 添加鼠标事件监听器
}
// 重写 JPanel 的 paintComponent 方法,在 DrawingBoard 上绘制字符串
public void paintComponent(Graphics g) {
super.paintComponent(g); // 先调用父类的 paintComponent 方法
Font font = new Font("Arial", Font.BOLD, 20); // 创建字体对象
g.setFont(font); // 设置字体
g.setColor(Color.BLUE); // 设置画笔颜色为蓝色
g.drawString("Hello World!", 50, 50); // 在 DrawingBoard 上绘制字符串
}
// 鼠标拖动事件监听器
public void mouseDragged(MouseEvent e) {
Graphics g = getGraphics(); // 获取 DrawingBoard 上的画笔对象
g.drawLine(lastX, lastY, e.getX(), e.getY()); // 绘制直线,连接上一次鼠标移动的位置和当前位置
lastX = e.getX(); // 更新上一次鼠标移动的位置
lastY = e.getY();
}
// 鼠标按下事件监听器
public void mousePressed(MouseEvent e) {
lastX = e.getX(); // 记录上一次鼠标移动的位置
lastY = e.getY();
}
// 鼠标移动事件监听器(不需要实现)
public void mouseMoved(MouseEvent e) {}
// 鼠标单击事件监听器(不需要实现)
public void mouseClicked(MouseEvent e) {}
// 鼠标进入 DrawingBoard 事件监听器(不需要实现)
public void mouseEntered(MouseEvent e) {}
// 鼠标离开 DrawingBoard 事件监听器(不需要实现)
public void mouseExited(MouseEvent e) {}
// 鼠标释放事件监听器(不需要实现)
public void mouseReleased(MouseEvent e) {}
// 主函数
public static void main(String[] args) {
JFrame frame = new JFrame("Drawing Board"); // 创建 JFrame 对象
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置关闭操作
frame.getContentPane().add(new DrawingBoard()); // 在 JFrame 上添加 DrawingBoard
frame.pack(); // 调整 JFrame 的大小以适应 DrawingBoard
frame.setVisible(true); // 显示 JFrame
}
}
```
public class JavaMain { public static void main(String[] args) { new MyWindow(); } } class MyWindow extends JFrame implements MouseListener, MouseMotionListener, ActionListener { //定义两个点 private Point oldPoint = null,newPoint = null; public MyWindow () { this.setSize(600,600); addMouseListener(this); addMouseMotionListener(this); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } //画图 public void paint(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.setStroke(new BasicStroke(10.0f)); g2.setColor(Color.red); if(oldPoint!=null&&newPoint!=null) { g2.drawLine(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y); oldPoint=newPoint; newPoint=null;//置为空,为了迎接下一个点的到来 } } //鼠标拖动事件 @Override public void mouseDragged(MouseEvent e) { if(oldPoint==null) { oldPoint=e.getPoint(); }else { newPoint=e.getPoint(); repaint();//生成新的点,通知系统去更新绘图 } } @Override public void mouseMoved(MouseEvent e) {} //鼠标单击事件 @Override public void mouseClicked(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} @Override public void mousePressed(MouseEvent e) {} //鼠标释放事件,清空工作 @Override public void mouseReleased(MouseEvent e) { newPoint=null; oldPoint=null; } }设置清除画布按钮
您可以在 `MyWindow` 类中添加一个按钮,并为其添加一个 `ActionListener`,在按钮被点击时清除画布。代码示例如下:
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MyWindow extends JFrame implements MouseListener, MouseMotionListener, ActionListener {
private Point oldPoint = null, newPoint = null;
private JButton clearButton; // 添加一个清除按钮
public MyWindow() {
this.setSize(600, 600);
addMouseListener(this);
addMouseMotionListener(this);
// 初始化清除按钮
clearButton = new JButton("清除画布");
clearButton.addActionListener(this);
JPanel buttonPanel = new JPanel();
buttonPanel.add(clearButton);
add(buttonPanel, BorderLayout.SOUTH);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10.0f));
g2.setColor(Color.red);
if (oldPoint != null && newPoint != null) {
g2.drawLine(oldPoint.x, oldPoint.y, newPoint.x, newPoint.y);
oldPoint = newPoint;
newPoint = null;
}
}
@Override
public void mouseDragged(MouseEvent e) {
if (oldPoint == null) {
oldPoint = e.getPoint();
} else {
newPoint = e.getPoint();
repaint();
}
}
@Override
public void mouseMoved(MouseEvent e) {}
@Override
public void mouseClicked(MouseEvent e) {}
@Override
public void mouseEntered(MouseEvent e) {}
@Override
public void mouseExited(MouseEvent e) {}
@Override
public void mousePressed(MouseEvent e) {}
@Override
public void mouseReleased(MouseEvent e) {
newPoint = null;
oldPoint = null;
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == clearButton) {
Graphics g = getGraphics();
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
}
}
}
```
在上面的代码中,我们添加了一个 `clearButton` 按钮,并在其上添加了一个 `ActionListener`。当按钮被点击时,会调用 `actionPerformed` 方法清除画布。在 `actionPerformed` 方法中,我们获取了 `MyWindow` 的 `Graphics` 对象,并使用 `fillRect` 方法覆盖整个画布。这样就达到了清除画布的效果。
相关推荐
![](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)