gpt4 book ai didi

Java Mouse "Flashlight"效果如何?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:29:54 25 4
gpt4 key购买 nike

我正在编写一个简单的 2D 迷宫游戏,您可以在其中穿过许多房间。我想通过限制玩家的视野来让它变得有点挑战。起初我想将框架内的默认鼠标图标替换为半透明的 PNG 椭圆,但后来我意识到我需要遮挡它周围的东西。

我能想到的唯一方法是让鼠标指针图标成为一个比框架大的图像(这样当用户移动到一个角落时它仍然是黑色的)填充它然后放置透明- 指针区域的渐变椭圆。

我想知道的是,这是否可能,我将如何着手去做?我正在学习 java,所以示例和 oracle 文档将对我有很大帮助。提前致谢!

从这里和这里看到 link (加载需要一段时间) PS:我用的是eclipse。

enter image description here

最佳答案

您已经确定了基本要求。

  • 您需要一个鼠标监听器来监视鼠标的移动,以便您可以更新聚光灯的位置。通读how to write a mouse listener
  • 您需要在图像上渲染聚光灯效果

以下示例基本上使用 RadialGradientPaint 在图像上绘制“聚光灯”。它使用 MouseMoitionListener 来监视鼠标的位置并在您移动时更新聚光灯。

enter image description here

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Point;
import java.awt.RadialGradientPaint;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MouseCover {

public static void main(String[] args) {
new MouseCover();
}

public MouseCover() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public static class TestPane extends JPanel {

public static final int RADIUS = 200;
private Point mousePoint = null;
private BufferedImage background;

public TestPane() {

MouseAdapter mouseHandler = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
mousePoint = e.getPoint();
repaint();
}

@Override
public void mouseExited(MouseEvent e) {
mousePoint = null;
repaint();
}
};

addMouseMotionListener(mouseHandler);
addMouseListener(mouseHandler);
try {
background = ImageIO.read(...);
} catch (IOException ex) {
ex.printStackTrace();
}
}

@Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(), background.getHeight());
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();

if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g2d.drawImage(background, x, y, this);
}

Paint paint = Color.BLACK;
if (mousePoint != null) {
paint = new RadialGradientPaint(
mousePoint,
RADIUS,
new float[]{0, 1f},
new Color[]{new Color(0, 0, 0, 0), new Color(0, 0, 0, 255)});
}

g2d.setPaint(paint);
g2d.fillRect(0, 0, getWidth(), getHeight());

g2d.dispose();
}
}
}

关于Java Mouse "Flashlight"效果如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15488853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com