When I trigger my fireball creating, it resets my Character JLabel

此生再无相见时 提交于 2019-12-02 10:58:21

Well, there's your problem...

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);

Your container is under the control of a layout manager. Any attempt to change the position or size of any component (via setBounds, setLocation and/or setSize) will fail because the layout manager will discard those changes in favor of its own...

The moment you add or remove any component, the layout manager will update all the components to where it thinks they should be.

This won't work and as a bad idea.

while (true) {
    x = fireballButton[c].getX();
    y = fireballButton[c].getY();
    fireballButton[c].setBounds(x + 5, y, 64, 32);
    c++;
    if (c == fireballTotal) {
        break;
    }
}

Any time consuming or blocking actions (like a loop) will stop the EDT from updating the graphics, basically meaning that you won't actually see the fire ball...And I can't believe you used Timer with this (which would be in the right direction) and still used a loop...

My advice...

Use a JLayerPane instead. This allows you to use absolute positing instead (although you can apply a layout manager to it if you really want...but then it just be a JPanel)

Also, have a read through Concurrency in Swing

UPDATE with Example

public class TestFireBall {

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

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

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

    public class FireBallPane extends JLayeredPane {

        private JLabel character;
        private List<JLabel> fireBalls;
        private ImageIcon icoFireBall;
        private Timer timer;
        private Timer coolOffTimer;
        private int vX = 12;
        private boolean coolOff = false;

        public FireBallPane() {
            fireBalls = new ArrayList<JLabel>(25);

            ImageIcon icoCharacter = null;
            ImageIcon fireBall = null;

            try {
                icoCharacter = new ImageIcon(ImageIO.read(getClass().getResource("/BlackMage.png")));
                icoFireBall = new ImageIcon(ImageIO.read(getClass().getResource("/FireBall.png")));
            } catch (Exception e) {
                e.printStackTrace();
            }

            character = new JLabel(icoCharacter);

            add(character);

            setFocusable(true);
            requestFocusInWindow();

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0), "fire");
            ActionMap am = getActionMap();
            am.put("fire", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!coolOff) {
                        coolOff = true;
                        JLabel fireBall = createFireBall();
                        fireBalls.add(fireBall);
                        if (!timer.isRunning()) {
                            timer.start();
                        }
                        coolOffTimer.restart();
                    }
                }
            });

            timer = new Timer(125, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (fireBalls.size() > 0) {
                        JLabel[] balls = fireBalls.toArray(new JLabel[fireBalls.size()]);
                        for (JLabel fireBall : balls) {
                            if (fireBall.getParent() == null) {
                                add(fireBall);
                            }
                            Point p = fireBall.getLocation();
                            p.x += vX;
                            if (p.x + fireBall.getWidth() >= getWidth()) {
                                remove(fireBall);
                                fireBalls.remove(fireBall);
                            } else {
                                fireBall.setLocation(p);
                            }
                            repaint();
                        }
                    } else {
                        timer.stop();
                    }
                }
            });
            timer.setRepeats(true);
            timer.setCoalesce(true);

            coolOffTimer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    coolOff = false;
                }
            });
            coolOffTimer.setRepeats(false);
            coolOffTimer.setCoalesce(true);
        }

        protected JLabel createFireBall() {
            JLabel fireBall = new JLabel(icoFireBall);

            fireBall.setSize(fireBall.getPreferredSize());
            int x = character.getX() + character.getWidth();
            int y = character.getY() + ((character.getHeight() - fireBall.getHeight()) / 2);
            fireBall.setLocation(x, y);

            return fireBall;
        }

        @Override
        public void invalidate() {
            super.invalidate();

            character.setSize(character.getPreferredSize());
            int height = getHeight();
            int y = (height - character.getHeight()) / 2;
            character.setLocation(0, y);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 400);
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!