简体   繁体   English

如何创建一个循环的世界地图? (Java-Swing)

[英]How to create a looped world map? (Java-Swing)

So, I've started to make a grand strategy game in Java using Swing and I want to create a world map wich looped.所以,我已经开始使用 Swing 用 Ja​​va 制作一个大战略游戏,我想创建一个循环的世界地图。 (So if you reach the west end of the worldmap, the system will start drawing the east side and vica verse, like in HOI4 or EU4.) (因此,如果您到达世界地图的西端,系统将开始绘制东侧和维卡诗句,就像在 HOI4 或 EU4 中一样。)

I have no idea how to do that.我不知道该怎么做。 I tried to create 3 map and if you reach the end the system drop back to the middle, but this method ate my computer.我尝试创建 3 个地图,如果你到达终点,系统会退回到中间,但这种方法吃掉了我的电脑。

Or if it's easier to understand, I want to create a cylinder, and draw a part from its wall.或者,如果更容易理解,我想创建一个圆柱体,并从其壁上绘制一个零件。

(I'm planing to switch to libgdx, especially if there I can make this much easier.) (我打算切换到 libgdx,特别是如果我可以让这更容易的话。)

Here's my WorldMap class:这是我的 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);
    }

}

Okay, so I found out someting.好的,所以我发现了一些东西。 When I had tried the "three map" method, the problem had been that I had wanted to draw two different images (for the base and for the other drawings).当我尝试“三张图”方法时,问题是我想绘制两个不同的图像(用于基础和其他绘图)。 Now I made it again and it works fine.现在我又做了一次,效果很好。 (Now I need only two image.) (现在我只需要两张图片。)

If anyone need the code:如果有人需要代码:

The paint method of the world map:世界地图的绘制方法:

@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);
}

The mouse listeners (to move and zoom on the map):鼠标侦听器(在地图上移动和缩放):

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();
            }
        }
    });

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

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