- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,我需要通过用户输入使火柴人移动。当用户点击某个部位(头、手、脚和臀部)时,他应该移动,但不知道如何去做。如果可能的话,角色周围还需要有一个限制,可能是矩形,这样每个部分可以拉动的距离就有限制。请参阅下面的我的代码;
// Created by Charlie Carr - (28/11/17 - /11/17)
import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
import java.awt.Graphics;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
//Imports complete
//Suppress warning about undeclared static final serialVersionUID field in VS Code
@SuppressWarnings("serial")
public class Animator extends JPanel {
public static class AnimatorWindow extends JPanel {
public void paint(Graphics page) {
setBackground(Color.gray);
setForeground(Color.white);
super.paintComponent(page);
page.drawString("Stickmen Animation Station", 150, 15);
//draw the head
//x1, y1, x2, y2
page.drawOval(90, 60, 20, 20);
// draw the body
page.drawLine(100, 80, 100, 110);
// draw the hands
page.drawLine(100, 90, 80, 105);
page.drawLine(100, 90, 120, 105);
//draw the legs, he hasn't a leg to stand on..
page.drawLine(100, 110, 85, 135);
page.drawLine(100, 110, 115, 135);
}
}
public static void main(String[] args) {
AnimatorWindow displayPanel = new AnimatorWindow();
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(displayPanel, BorderLayout.CENTER);
//declare window size
int x = 480;
int y = 240;
JFrame window = new JFrame("GUI");
window.setContentPane(content);
window.setSize(x, y);
window.setLocation(101, 101);
window.setVisible(true);
}
}
最佳答案
使用MouseListener
处理鼠标事件。
此外,您应该重写 paintComponent()
方法而不是 paint()
,因为 paint()
还会绘制边框和其他内容东西。
public static class AnimatorWindow extends JPanel implements MouseListener{
public AnimatorWindow(){
setBackground(Color.gray);
setForeground(Color.white);
//add the listener
addMouseListener(this);
}
public void paintComponent(Graphics page) {
super.paintComponent(page);
//You should not alter the Graphics object passed in
Graphics2D g = (Graphics2D) page.create();
//draw your stuff with g
g.drawString("Stickmen Animation Station", 150, 15);
.......
//finish
g.dispose();
}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){
//implement your clicking here
//Use e.getX() and e.getY() to get the click position
}
}
有关挥杆 Activity 的更多信息,请查看this site
编辑:您的问题还包括动画,您可以使用 javax.swing.Timer
来执行此操作。
关于java - 如何让火柴人具有互动性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47579626/
我一直在浏览关于 BIRT 的许多网站,特别是与交互或脚本相关的主题。 但是(生活在一个完美无瑕的世界中真是太好了)尝试执行一些示例(例如这个http://kickjava.com/src/org/e
我对我的 Haskell 有点生疏了,我想重新开始。我喜欢 F# 的一件事是与 Visual Studio 集成的 F# Interactive shell:我几乎可以评估任何东西(包括函数和类定义)
有什么方法可以将我自己的补全添加到(interactive) elisp函数中?理想情况下,我希望将其制表符完整的字符串列表传递给它。我知道使用(interactive "D"),(interacti
所以,我有一个问题! 我有一个名为 X 的 Activity 。当用户单击按钮时,将显示 Activity Y。我希望在收到 Activity X 发送的事件后可以关闭此 Activity 。 你知道
有几篇关于相同内容的帖子,但我仍然无法使我的 expect 脚本正常工作。我的意图是将所有内容自动化,但为用户保留输入密码。所以脚本有 3 个部分: 自动登录 让用户输入密码 将控制权交还给 Expe
我在使用带有自行车路线的示例 map 时遇到了两个小问题。 1.目前我的 map 是这样工作的: 当您将鼠标悬停在路线上时,它会突出显示(这很好)。 当您将鼠标移出路线时,它会取消突出显示(这也很好)
有一些 iTunes 链接可以通过网络浏览器中的链接打开 iTunes 中的应用程序或专辑。 这是如何工作的,我可以在 C# .NET 应用程序中完成吗? 最佳答案 iTunes 在注册表中注册 it
我刚刚将 firebase 集成到我的 android 和 ios 应用程序中。 仪表板有一个参与卡,我可以看到用户参与的前 3 个事件或 View Controller 。百分比加起来只有 74%,
我想接收事件并从浏览到a video page on youtube.com时加载的YouTube HTML5播放器获取播放器选项。 我知道可以通过HTML5 media events控制播放器。例如
我需要直接与使用 subprocess 生成的进程的 stdin 和 stdout 进行交互。我可以这样做: proc = subprocess.Popen("/bin/bash", stdin=su
我也尝试简单地使用 interact 并且还通过删除最后一个 expect staement 来实现这一点 expect -re "$prompt" send -- "exit\r"
尝试使用 chrome puppeteer 来渲染一个 React 组件 在我的 Node.js 环境中运行时遇到以下问题: 记录 element在 headless chrome 控制台中给我:co
我一直在努力寻找一种方法让 Seaborn 和 Vincent 互动,例如,我可以实时放大/缩小绘图的特定区域。这可能吗?或者,是否有其他推荐的库(不是基于云的服务)可以很好地可视化时间序列数据? 最
我很难理解两者如何相互作用以及它们之间的界限在哪里。它们重叠吗?它们之间有冗余吗? 我知道两者都有相关的注释,但我无法找到包含简短描述的完整列表。不确定这是否有助于弄清楚它们的不同之处或重叠的地方。
我想通过我自己的线程与 Akka Actor 互动。目前,我喜欢这样: val res = Await.result(aref ? GroupReceive(fromRank), timeout.du
我是编程新手。基本上刚刚完成了几个教程和准系统说明。我想编写 pong 代码让自己开始尝试自己做一些事情,但我遇到了一些障碍。出于某种原因,我生成的球根本不会与我的玩家 1 Racket 互动,但它会
我是一名优秀的程序员,十分优秀!