简体   繁体   English

如何在JPanel中移动图形的位置

[英]How to move the location of the graphic in JPanel

So I'm creating a game using Javax.swing library for my uni coursework. 因此,我正在使用Javax.swing库为我的大学课程创建游戏。 I have created a window and I have successfully written code to procedurally generate a game map. 我已经创建了一个窗口,并且已经成功编写了程序生成游戏地图的代码。

However, I am unable to change the focus of the map. 但是,我无法更改地图的焦点。 What I mean is that the map is always stuck in one corner of the screen. 我的意思是,地图始终卡在屏幕的一个角上。 (IE: Location is set to 0,0, hence the Graphics g (the map) is put in that location going outwards.) (即:位置设置为0,0,因此图形g(地图)放置在向外的位置。)

I would like to be able to move the "camera" so that different areas of the map can be viewed by the player. 我希望能够移动“相机”,以便玩家可以查看地图的不同区域。

Bellow I have pasted my method that draws the map onto the screen. 在下面,我粘贴了将地图绘制到屏幕上的方法。 Could anyone tell me what I could do to have the camera move at runtime. 谁能告诉我如何在运行时移动相机。 AKA: to shift the map left or right. 又名:向左或向右移动地图。

I thought of having a Graphics object that will hold the map, and then I'd only draw a subImage of that Graphics object, but considering how the map will be redrawn every frame (For animation purposes) that just means that I'll have even more graphics to redraw. 我想到要有一个Graphics对象来保存地图,然后只绘制该Graphics对象的subImage,但是考虑到如何在每一帧中重新绘制地图(出于动画目的),这意味着我将拥有重画更多图形。 The map is 6,400 * 6,400 Pixels 地图为6,400 * 6,400像素

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    try {
        for(int x = 0; x < OverworldMap.MAP_X_SIZE; x++){
            for(int y = 0; y < OverworldMap.MAP_Y_SIZE; y++){
                for(int layer = 0; layer < OverworldMap.MAP_LAYER_SIZE; layer++) {
                    g.drawImage(OverworldMap.getTileAt(x, y, layer).getSprite(), x * SPRITE_SIZE, y * SPRITE_SIZE, null);

                }
            }
        }
    } catch (Exception e) {
        LauncherClass.printErrorLog(e);
    }
}

The best / easiest way to solve this is to put a JScrollPane around your JPanel, and make the JPanel the size of your image. 解决此问题的最好/最简单的方法是在JPanel周围放置一个JScrollPane,并使JPanel达到图像的大小。 You don't need to worry about only repainting the right part of your image - Java is pretty smart about only drawing the parts that are on screen. 您无需担心仅重新绘制图像的正确部分-Java仅绘制屏幕上的部分非常聪明。 Note that you can show or hide the ScrollBars, but if you hide them you need to add logic to activate scrolling through some other mechanism 请注意,您可以显示或隐藏ScrollBar,但如果隐藏它们,则需要添加逻辑以通过其他机制激活滚动

You cannot store a Graphics object and use it later. 您不能存储Graphics对象,以后再使用它。 It is only valid for the duration of the paint method to which it is passed. 它仅在传递给它的paint方法期间有效。

You can, however, simply offset your painting: 但是,您可以简单地抵消绘画:

Image sprite = OverworldMap.getTileAt(x, y, layer).getSprite();
g.drawImage(sprite, x * SPRITE_SIZE - playerX, y * SPRITE_SIZE - playerY, this);

(Notice that the last argument to drawImage should be this .) (注意,drawImage的最后一个参数应该是this 。)

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

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