Infinite background for game

后端 未结 2 1546
無奈伤痛
無奈伤痛 2020-11-29 10:55

I am working on a Java project to simulate the flight of a helicopter in a frame. The helicopter moves on the screen using the arrow keys. I want the helicopter to be able t

2条回答
  •  失恋的感觉
    2020-11-29 11:13

    This is a really simple example (you can only move in a single direction). The basic idea is that there is a prepareView method that is responsible for generating a view of the world based on the available viewable area. If the view is trying to view an area off the map, the map is titled to make up for it.

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.imageio.ImageIO;
    import javax.swing.AbstractAction;
    import javax.swing.ActionMap;
    import javax.swing.InputMap;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class InfiniteBackground {
    
        public static void main(String[] args) {
            new InfiniteBackground();
        }
    
        public InfiniteBackground() {
            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 {
    
            protected static final int DELTA = 5;
            private BufferedImage terrian;
            private BufferedImage heli;
            private Point pov;
            private Point heliPoint;
            private BufferedImage view;
    
            public TestPane() {
                pov = new Point();
                heliPoint = new Point();
                try {
                    terrian = ImageIO.read(getClass().getResource("/terrain_map.jpg"));
                    heli = ImageIO.read(getClass().getResource("/helicopter2f.png"));
    
                    pov.x = terrian.getWidth() - getPreferredSize().width;
                    pov.y = ((terrian.getHeight() - getPreferredSize().height) / 2);
    
                    heliPoint.x = getPreferredSize().width / 2;
                    heliPoint.y = getPreferredSize().height / 2;
    
                    prepareView();
    
                    InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
                    ActionMap am = getActionMap();
    
                    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "goLeft");
                    am.put("goLeft", new AbstractAction() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            heliPoint.x -= DELTA;
                            if (heliPoint.x - (heli.getWidth() / 2) < 0) {
                                heliPoint.x = (heli.getWidth() / 2);
                                prepareView();
                                pov.x -= DELTA;
                            }
                            repaint();
                        }
                    });
    
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
    
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 200);
            }
    
            protected void prepareView() {
                if (getWidth() > 0 && getHeight() > 0) {
                    view = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
                    Graphics2D g2d = view.createGraphics();
                    if (pov.x < 0) {
                        pov.x = terrian.getWidth();
                    }
                    g2d.drawImage(terrian, -pov.x, -pov.y, this);
                    if (pov.x + getWidth() > terrian.getWidth()) {
                        g2d.drawImage(terrian, -pov.x + terrian.getWidth(), -pov.y, this);
                    }
                    g2d.dispose();
                }
            }
    
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                if (terrian != null) {
                    Graphics2D g2d = (Graphics2D) g.create();
                    if (view == null) {
                        prepareView();
                    }
                    g2d.drawImage(view, 0, 0, this);
                    g2d.drawImage(heli, heliPoint.x - (heli.getWidth() / 2), heliPoint.y - (heli.getHeight() / 2), this);
                    g2d.dispose();
                }
            }
        }
    }
    

提交回复
热议问题