如何在java中输入键盘后更改图像?

时间:2015-05-22 01:50:49

标签: java eclipse swing animation

我有以下代码向您展示:

ALTER TABLE `example` ADD IGNORE...

我的问题是我只要用户在键盘上按住右箭头就会更改图像,当用户放开它时,它会返回默认图像。请告诉我该怎么做。我认为它是Graphics类中的一系列if语句然后将它们调用到键输入但我不太确定。我也在使用Eclipse。谢谢。

1 个答案:

答案 0 :(得分:2)

  1. 覆盖paintComponent而不是paint。有关详细信息,请参阅Performing Custom PaintingPainting in AWT and Swing
  2. 使用密钥绑定API代替KeyListener,它会减少问题。有关详细信息,请参阅How to Use Key Bindings
  3. 基本上,您可以只使用Image作为类实例字段,该字段由paintComponent方法绘制。按下该键后,您可以将图像更改为"移动图像"当它被释放时,将其更改回"默认图像"

    更新了示例

    Walk the pony

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.ActionMap;
    import javax.swing.InputMap;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class Test {
    
        public static void main(String[] args) {
            new Test();
        }
    
        public Test() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                        ex.printStackTrace();
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public interface Mover {
    
            public enum Direction {
    
                LEFT, RIGHT, NONE;
            }
    
            public void setDirection(Direction direction);
    
            public Direction getDirection();
    
        }
    
        public class TestPane extends JPanel implements Mover {
    
            private BufferedImage left;
            private BufferedImage right;
            private BufferedImage stand;
    
            private BufferedImage current;
            private Direction direction = Direction.NONE;
            private int xPos;
            private int yPos;
    
            public TestPane() {
                try {
                    left = ImageIO.read(getClass().getResource("/Left.png"));
                    right = ImageIO.read(getClass().getResource("/Right.png"));
                    stand = ImageIO.read(getClass().getResource("/Stand.png"));
                    current = stand;
                    xPos = 100 - (current.getWidth() / 2);
                    yPos = 100 - (current.getHeight() / 2);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
    
                bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "move.left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,   0, false), new MoveAction(this, Direction.LEFT));
                bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "stop.left", KeyStroke.getKeyStroke(KeyEvent.VK_LEFT,   0, true), new MoveAction(this, Direction.NONE));
    
                bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "move.right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, false), new MoveAction(this, Direction.RIGHT));
                bindKeyStrokeTo(WHEN_IN_FOCUSED_WINDOW, "stop.right", KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0, true), new MoveAction(this, Direction.NONE));
    
                Timer timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        updatePosition();
                        repaint();
                    }
                });
                timer.start();
            }
    
            protected void bindKeyStrokeTo(int condition, String name, KeyStroke keyStroke, Action action) {
                InputMap im = getInputMap(condition);
                ActionMap am = getActionMap();
    
                im.put(keyStroke, name);
                am.put(name, action);
            }
    
            @Override
            public Direction getDirection() {
                return direction;
            }
    
            @Override
            public void setDirection(Direction direction) {
                this.direction = direction;
            }
    
            protected void updatePosition() {
    
                switch (getDirection()) {
                    case LEFT:
                        current = left;
                        xPos -= 1;
                        break;
                    case RIGHT:
                        current = right;
                        xPos += 1;
                        break;
                    case NONE:
                        current = stand;
                        break;
                }
    
                if (xPos < 0) {
                    xPos = 0;
                    current = stand;
                } else if (xPos + current.getWidth() > getWidth()) {
                    current = stand;
                    xPos = getWidth() - current.getWidth();
                }
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(current, xPos, yPos, this);
                g2d.dispose();
            }
    
        }
    
        public class MoveAction extends AbstractAction {
    
            private Mover mover;
            private Mover.Direction direction;
    
            public MoveAction(Mover mover, Mover.Direction direction) {
                this.mover = mover;
                this.direction = direction;
            }
    
            @Override
            public void actionPerformed(ActionEvent e) {
                mover.setDirection(direction);
            }
    
        }
    
    }