简体   繁体   English

Java Tile滚动问题

[英]Java Tile Scrolling Issues

I'm fairly new to programming with graphics and I'm attempting to code a side scrolling 2D game. 我对使用图形进行编程非常陌生,并且尝试编写侧面滚动2D游戏的代码。 At the moment, I'm trying to figure out how to approach redrawing a scrolling image as it appears in the JFrame. 目前,我正在尝试找出如何重新绘制JFrame中出现的滚动图像。 I'm using 8x8 pixel blocks as images. 我正在使用8x8像素块作为图像。 One possible issue I thought about concerns moving a sprite just 1 or 2 pixels and still rendering each image as it appears pixel by pixel on/off of the screen. 我想到的一个可能的问题是,仅将精灵移动1或2个像素,并在屏幕上逐个像素显示每个图像时仍渲染每个图像。 How do I go about rendering the image/blocks pixel by pixel instead of whole images should the sprite barely move? 我应该如何逐个像素渲染图像/块而不是整个图像,否则精灵应该几乎不移动? Any feedback is much appreciated! 任何反馈都非常感谢!

This is a proof of concept only! 这仅是概念证明 I randomly generate the tiles that get painted, I hope you have some kind of virtual map setup so you know which tiles to paint at any given virtual point! 我随机生成要绘制的图块,希望您有某种虚拟地图设置,以便您知道在任何给定的虚拟点上要绘制的图块!

Basically, what this does, is when the screen is moved left or right, it shifts the "master" image left or right and stitches new tiles onto new edge 基本上,这样做是在向左或向右移动屏幕时,将“主”图像向左或向右移动,并将新图块拼接到新边缘上

My test was using a style sheet of 31x31 cells (don't ask, I just grab it off the net) 我的测试使用的是31x31单元格样式表(不要问,我只是将其从网上获取)

This is VERY scaled down example of the output, it was running at 1100x700+ 这是按比例缩小的输出示例,其运行速度为1100x700 +

例

import java.awt.BorderLayout;
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.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
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 Scroll {

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

    public Scroll() {
        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 class TestPane extends JPanel {

        private BufferedImage screen;
        private BufferedImage styleSheet;

        public TestPane() {
            try {
                styleSheet = ImageIO.read(getClass().getResource("/StyleSheet.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "left");
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "right");

            ActionMap am = getActionMap();
            am.put("left", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    stitch(-31);
                }
            });
            am.put("right", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    stitch(31);
                }
            });
        }

        @Override
        public void invalidate() {
            screen = null;
            super.invalidate();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        protected void stitch(int direction) {
            if (screen == null) {
                prepareScreen();
            }

            Random r = new Random();
            BufferedImage update = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = update.createGraphics();
            g2d.drawImage(screen, direction, 0, this);
            int gap = direction < 0 ? (direction * -1) : direction;

            int xOffset = 0;
            if (direction < 0) {
                xOffset = getWidth() - gap;
            }

            for (int x = 0; x < gap; x += 31) {
                for (int y = 0; y < getHeight(); y += 31) {
                    xOffset += x;
                    int cellx = 2;
                    int celly = 2;
                    if (r.nextBoolean()) {
                        cellx = 7;
                        celly = 5;
                    }

                    BufferedImage tile = styleSheet.getSubimage((cellx * 33) + 1, (celly * 33) + 1, 31, 31);
                    g2d.drawImage(tile, xOffset, y, this);
                }
            }
            g2d.dispose();

            screen = update;

            repaint();

        }

        protected void prepareScreen() {

            if (screen == null) {
                screen = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
            }

            Random r = new Random();
            Graphics2D g2d = screen.createGraphics();
            for (int x = 0; x < getWidth(); x += 31) {
                for (int y = 0; y < getHeight(); y += 31) {

                    int cellx = 2;
                    int celly = 2;
                    if (r.nextBoolean()) {
                        cellx = 7;
                        celly = 5;
                    }

                    BufferedImage tile = styleSheet.getSubimage((cellx * 33) + 1, (celly * 33) + 1, 31, 31);
                    g2d.drawImage(tile, x, y, this);

                }
            }
            g2d.dispose();

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            if (screen == null) {
                prepareScreen();
            }
            g2d.drawImage(screen, 0, 0, this);
            g2d.dispose();
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM