- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是一个让球以对角线方式下降的用户界面,但每次我按下“动画它!”按钮时它不会创建一个新球。
另一个问题是“卡住”按钮无法按预期工作,因为它应该停止球,然后;如果你再按一次,它应该会让球再次移动。
最后,限制有问题,当球几乎触及底部时,它没有到达底部并弹到右侧。另外,当球“触及”正确的界限时,它就会上升。
此外,当调整框架大小时,限制不会更新。
请下载一个球并更改目录,以便程序可以找到您的球的分配位置。没有必要下载足球场,但如果您愿意,也可以。最后,我要感谢您花时间寻找这个故障。
Ball link: https://lapequeteria.cl/wp-content/uploads/2018/11/balon-adidas-2.jpeg (Make it png)
Pitch link: https://www.freejpg.com.ar/asset/900/9c/9ca2/F100004898.jpg
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import java.io.File;
class Animation extends JFrame { //Frame
static boolean running = true;
static Layout panel = new Layout();
JButton animate, stop;
Runnable runnable;
Thread move;
public Animation() {
setLayout(new BorderLayout()); //BorderLayout disposition
setTitle("Pelota en acción");
animate = new JButton("Animate it!"); //Button to create balls
animate.setSize(120,30);
animate.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
panel.createEllipse();
runnable = panel;
move = new Thread(runnable);
panel.X = 0;
panel.Y = 0;
move.start();
}
});
stop = new JButton("Freeze"); //Botón para interrumpir hilo (aun no implementado)
stop.setBounds(0,0,120,30);
stop.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
if(move.isAlive()){
stop.setText("Go"); //Pause
synchronized(move) {
try {
// running = false;
move.wait();
} catch (InterruptedException ex) {
System.out.println("Interrupted");
}
}
}
else {
stop.setText("Freeze"); //Play
synchronized(move) {
// running = true;
move.notifyAll();
}
}
}
});
JPanel subPanel = new JPanel(); //Layout with its buttons situated to the south
subPanel.add(animate);
subPanel.add(stop);
add(subPanel,BorderLayout.SOUTH);
add(panel);
}
public static void main(String[] args) { //Frame construction
Animation ventana = new Animation();
ventana.setSize(850,625);
ventana.setLocationRelativeTo(null);
ventana.setVisible(true);
ventana.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
class Layout extends JPanel implements Runnable { //Layout and thread
volatile double X,Y; //Coordinates
volatile double dX=1,dY=1; //Direction
static ArrayList<Image> balls = new ArrayList<>(); //Balls collection
Image picture, ball;
int width = 120,height = 120;
@Override
public void paintComponent(Graphics g) { //Shows the ball and the pitch
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
soccerPitch();
g2.drawImage(picture, 0, 0, null);
for (Image ball : balls) { //Balls added to the layout
g2.drawImage(ball,(int)X,(int)Y,100,100,null);
}
}
public void soccerPitch () { //Builds the soccerPitch image
try {
picture = ImageIO.read(new File("C:\\Users\\Home\\Desktop\\NO BORRAR porfavor\\Cancha.jpg"));
} catch (IOException ex) {
System.out.println("Pitch image was not found");
}
}
public void createEllipse () { //Builds the ball image
try {
ball = ImageIO.read(new File("C:\\Users\\Home\\Desktop\\NO BORRAR porfavor\\Pelota.png"));
System.out.println(ball.getWidth(null)+" "+ball.getHeight(null));
} catch(IOException ex) {
System.out.println("Any balls were found");
}
balls.add(ball); //Balls are added to the collection
}
@Override
public void run () { //Moves the ball(coordinate system doesn't work)
moveBall(Animation.panel.getBounds());
}
public void moveBall(Rectangle2D limits) {
while(true) {
// while(Animation.running) {
System.out.println("X: "+X+"\tY: "+Y);
System.out.println(limits.getBounds());
X+=dX;
Y+=dY;
if(X<limits.getMinX()){
X=limits.getMinX();
dX=-dX;
}
if(X + width>=limits.getMaxX()){
X=limits.getMaxX()-width;
dX=-dY;
}
if(Y<limits.getMinY()){
Y=limits.getMinY();
dY=-dY;
}
if(Y + height>=limits.getMaxY()){
Y=limits.getMaxY()-height;
dY=-dY;
}
try {
Thread.sleep(4);
} catch (InterruptedException e) {
System.out.println("Interrupted");
}
repaint();
}
}
}
以防万一你想帮助我;我不是要求完成工作,只是向我解释为什么它不起作用。
最佳答案
您似乎违反了 Concurrency in Swing 的基本规则。加号而不是 using LayoutManagers您手动设置组件的边界(这就是为什么当调整框架大小时,组件不在正确的位置)。
现在,您在线程中启动应用程序,对吧?在此线程中,您准备 GUI 并执行操作,然后在某个时刻 while(true)
。当您 while(true)
时,线程将“卡在”while 循环内,因此无法发生 swing 事件。这就是为什么您必须 while(true)
并在另一个线程中创建动画。然而,在 Swing 中创建动画的最佳方法是 use a Swing Timer.
我创建了一个示例,其中动画每 100 毫秒在面板中创建一个新的随机矩形 (10x10)。使用它进行一些过期操作,并尝试将相同的逻辑调整到您的应用程序(毕竟,您不会发布代码并期望我们为您修复它:))。
这是示例:
public class Animation extends JFrame implements ActionListener {
private Timer timer;
private RectanglePanel panel;
public Animation() {
super("test");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
add(panel = new RectanglePanel());
timer = new Timer(100, this); //every 100 ms add new Shape
JButton button = new JButton("Start Animation");
button.addActionListener(e -> timer.start()); //start animation timer
add(button, BorderLayout.PAGE_END);
pack();
setSize(500, 500);
setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
panel.createNewShape(); //add new shape
panel.repaint();
}
private class RectanglePanel extends JPanel {
private List<Rectangle> shapes;
public RectanglePanel() {
super();
this.shapes = new ArrayList<>();
}
public void createNewShape() {
int x = (int) (Math.random() * getWidth());
int y = (int) (Math.random() * getHeight());
x = Math.min(x, getWidth() - 10);
y = Math.min(y, getHeight() - 10);
Rectangle r = new Rectangle(x, y, 10, 10);
System.out.println("New rectangle:" + r);
shapes.add(r);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (Rectangle r : shapes) {
g.setColor(randomColor());
g.drawRect(r.x, r.y, r.width, r.height);
}
}
private Color randomColor() {
return new Color((int) (Math.random() * 255), (int) (Math.random() * 255), (int) (Math.random() * 255));
}
}
public static void main(String[] args) {
//All swing applications must run on their own thread
SwingUtilities.invokeLater(() -> new Animation().setVisible(true));
}
}
预览:
关于java - 限制和卡住按钮不起作用,也不会生成新球(动画),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58240541/
我有一个 ServiceBusQueue(SBQ),它获取大量消息负载。我有一个具有 accessRights(manage) 的 ServiceBusTrigger(SBT),它不断轮询来自 SBQ
在下面给出的结果集中,有 2 个唯一用户 (id),并且查询中可能会出现更多此类用户: 这是多连接查询: select id, name, col1Code, col2Code, col2Va
我正在用 Python 2.7.3 编写一个带有 GRequests 的小脚本和 lxml 可以让我从各种网站收集一些收藏卡价格并进行比较。问题是其中一个网站限制了请求的数量,如果我超过它,就会发回
我想知道何时实际使用删除级联或删除限制以及更新级联或更新限制。我对使用它们或在我的数据库中应用感到很困惑。 最佳答案 在外键约束上使用级联运算符是一个热门话题。 理论上,如果您知道删除父对象也将自动删
下面是我的输出,我只想显示那些重复的名字。每个名字都是飞行员,数字是飞行员驾驶的飞机类型。我想显示驾驶不止一架飞机的飞行员的姓名。我正在使用 sql*plus PIL_PILOTNAME
我正在评估不同的移动框架,我认为 nativescript 是一个不错的选择。但我不知道开发过程是否存在限制。例如,我对样式有限制(这并不重要),但我想知道将来我是否可以有限制并且不能使用某些 nat
我正在尝试使用 grails 数据绑定(bind)将一些表单参数映射到我的模型中,但我认为在映射嵌入式集合方面可能存在一些限制。 例如,如果我提交一些这样的参数,那么映射工作正常: //this wo
是否可以将 django 自过滤器起的时间限制为 7 天。如果日期超过 7 天,则不应用过滤器 最佳答案 timesince 的源代码位于 django/django/utils/timesince.
我想在我的网站上嵌入一个 PayPal 捐赠按钮。但问题是我住在伊朗——这个国家受到制裁,人们不使用国际银行账户或主要信用卡。 有什么想法吗?请帮忙! 问候 沮丧 最佳答案 您可以在伊朗境内使用为伊朗
这是我的查询 select PhoneNumber as _data,PhoneType as _type from contact_phonenumbers where ContactID = 3
这个问题在这里已经有了答案: What is the maximum number of parameters passed to $in query in MongoDB? (4 个答案) 关闭
我的一个项目的 AndroidManifest.xml 变得越来越大(> 1000 行),因为我必须对某些文件类型使用react并且涵盖所有情况变得越来越复杂。我想知道 list 大小是否有任何限制。
在使用 Sybase、Infomix、DB2 等其他数据库产品多年后使用 MySQL 5.1 Enterprise 时;我遇到了 MySQL 不会做的事情。例如,它只能为 SELECT 查询生成 EX
这个问题在这里已经有了答案: What is the maximum number of parameters passed to $in query in MongoDB? (4 个回答) 关闭5年
通常我们是在{$apache}/conf/httpd.conf中设置Apache的参数,然而我们并没有发现可以设置日志文件大小的配置指令,通过参考http://httpd.apache.org/do
我正在搜索最大的 Android SharedPreferences 键值对,但找不到任何好的答案。其次,我想问一下,如果我有一个键,它的字符串值限制是多少。多少字符可以放入其中。如果我需要频繁更改值
我目前正在试验 SoundCloud API,并注意到我对/tracks 资源的 GET 请求一次从不返回超过 200 个结果。关于这个的几个问题: 这个限制是故意的吗? 有没有办法增加这个限制? 如
我正在与一家名为 Dwolla 的金融技术公司合作,该公司提供了一个 API,用于将银行信息附加到用户并收取/发送 ACH 付款。 他们需要我将我的 TLS 最低版本升级到 1.2(禁用 TLS 1.
我在 PHP 中有一个多维数组,如下所示: $array = Array ( [0] => Array ( [bill] => 1 ) [1] => Array ( [
我在获取下一个查询的第一行时遇到了问题: Select mar.Title MarketTitle, ololo.NUMBER, ololo.Title from Markets mar JOIN(
我是一名优秀的程序员,十分优秀!