因此,我已经开始使用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);
}
}
答案 0 :(得分: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();
}
}
});