如何创建循环世界地图? (Java的秋千)

问题描述 投票:-3回答:1

所以,我已经开始使用Swing在Java中制作一个大型策略游戏,我想创建一个循环的世界地图。 (因此,如果你到达世界地图的西端,系统将开始绘制东边和vica诗歌,就像在HOI4或EU4中一样。)

我不知道该怎么做。我试图创建3个地图,如果你到达最后系统回到中间,但这个方法吃了我的电脑。

或者,如果它更容易理解,我想创建一个圆柱体,并从其墙壁绘制一个部分。

(我打算切换到libgdx,特别是在那里我可以让这更容易。)

这是我的WorldMap类:

import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;

import me.fiveship.waw.objects.Area;
import me.fiveship.waw.objects.Point;

public class WorldMap extends JComponent {

    private static final long serialVersionUID = -4823224592445587979L;

    public static int WIDTH = 1280;
    public static int HEIGHT = 768;

    public WorldMap() {
        setBounds(0, 0, WIDTH, HEIGHT);
    }

    public Point location = new Point(0, 0);
    public double zoomLevel = 3;

    protected java.awt.Point p;

    private static boolean settedUp = false;

    private static BufferedImage areaMap = null;
    private static BufferedImage countryMap = null;
    private static BufferedImage regionMap = null;

    public static void createPreMaps() {
        Point max = Area.max();
        areaMap = new BufferedImage(max.X, max.Y, BufferedImage.TYPE_INT_ARGB);
        countryMap = new BufferedImage(max.X, max.Y, BufferedImage.TYPE_INT_ARGB);
        regionMap = new BufferedImage(max.X, max.Y, BufferedImage.TYPE_INT_ARGB);
        // AREA MAP
        Graphics g = areaMap.createGraphics();
        for (Area area : Area.areas()) {
            g.setColor(area.color());
            for (Point p : area.points) {
                g.fillRect(p.X, p.Y, 1, 1);
            }
            g.setColor(area.color().darker());
            /*
             * for (Border b : area.borders) { g.fillRect(b.p.X, b.p.Y, 1, 1); }
             */
        }
        // COUNTRY MAP
        // g = countryMap.createGraphics();
        // REGION MAP
        // g = regionMap.createGraphics();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (!settedUp) {
            settedUp = true;
            createPreMaps();
        }
        Rectangle r = new Rectangle((int) (location.X * zoomLevel), (int) (location.Y * zoomLevel),
                (int) (areaMap.getWidth() * zoomLevel), (int) (areaMap.getHeight() * zoomLevel));
        g.drawImage(areaMap, r.x, r.y, r.width, r.height, null);
    }

}
java eclipse swing game-development world-map
1个回答
0
投票

好的,所以我发现了一些东西。当我尝试了“三个地图”的方法时,问题就是我想绘制两个不同的图像(对于底图和其他图纸)。现在我又做了它,它工作正常。 (现在我只需要两张图片。)

如果有人需要代码:

世界地图的绘画方法:

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (!settedUp) {
        settedUp = true;
        createPreMaps();
    }
    Rectangle r = new Rectangle((int) (location.X * zoomLevel), (int) (location.Y * zoomLevel),
            (int) (areaMap.getWidth() * zoomLevel), (int) (areaMap.getHeight() * zoomLevel));
    g.drawImage(areaMap, r.x, r.y, r.width, r.height, null);
    g.drawImage(areaMap, r.x - r.width, r.y, r.width, r.height, null);
}

鼠标监听器(在地图上移动和缩放):

addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent e) {
        }

        @Override
        public void mousePressed(MouseEvent e) {
            if (map != null) {
                if (SwingUtilities.isMiddleMouseButton(e)) {
                    // System.out.println("Pressed");
                    Point point = e.getPoint();
                    int validX = (int) (point.x / map.zoomLevel);
                    int validY = (int) (point.y / map.zoomLevel);
                    map.p = new Point((int) (validX), (int) (validY));
                    // System.out.println(e.getX() + ";" + e.getY());
                }
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
        }

        @Override
        public void mouseEntered(MouseEvent e) {
        }

        @Override
        public void mouseClicked(MouseEvent e) {
        }
    });
    addMouseMotionListener(new MouseMotionListener() {

        @Override
        public void mouseMoved(MouseEvent event) {

        }

        @Override
        public void mouseDragged(MouseEvent event) {
            if (map != null) {
                if (SwingUtilities.isMiddleMouseButton(event)) {
                    Point point = event.getPoint();
                    int validX = (int) (point.x / map.zoomLevel);
                    int validY = (int) (point.y / map.zoomLevel);

                    // System.out.println("Dragged");
                    int thisX = (int) (map.location.X);
                    int thisY = (int) (map.location.Y);
                    // System.out.println("Dragged" + e.getX() + ";" + e.getY());
                    // Determine how much the mouse moved since the initial click
                    int xMoved = (thisX + validX) - (thisX + map.p.x);
                    int yMoved = (thisY + validY) - (thisY + map.p.y);
                    xMoved *= speed;
                    yMoved *= speed;
                    // Move picture to this position
                    int X = thisX + xMoved;
                    int Y = thisY + yMoved;

                    map.location = new me.fiveship.waw.objects.Point(X, Y);
                    if (map.location.Y > 0) {
                        map.location.Y = 0;
                    }
                    double a = ((-Area.max().Y + map.getBounds().getHeight() / map.zoomLevel));
                    if (a > map.location.Y) {
                        map.location.Y = (int) a;
                    }
                    int w = Area.max().X;
                    if (map.location.X > w) {
                        map.location.X = 0;
                    }
                    if (map.location.X < -w + map.getWidth()) {
                        map.location.X = map.getWidth();
                    }
                    // System.out.println(map.location.X);
                    repaint();
                }
            }
        }
    });
    addMouseWheelListener(new MouseWheelListener() {

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            // System.out.println(map);
            if (map != null) {
                double delta = 0.05d * e.getPreciseWheelRotation();
                map.zoomLevel -= delta;
                if (map.zoomLevel <= 1) {
                    map.zoomLevel = 1;
                } else if (map.zoomLevel >= Consts.c().MaxZoom) {
                    map.zoomLevel = Consts.c().MaxZoom;
                }
                // System.out.println(map.zoomLevel);
                if (map.location.Y > 0) {
                    map.location.Y = 0;
                }
                double a = ((-Area.max().Y + map.getBounds().getHeight() / map.zoomLevel));
                if (a > map.location.Y) {
                    map.location.Y = (int) a;
                }
                int w = Area.max().X;
                if (map.location.X > w) {
                    map.location.X = 0;
                }
                if (map.location.X < -w + map.getWidth()) {
                    map.location.X = map.getWidth();
                }
                map.repaint();
            }
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.