简体   繁体   English

添加滚动条和图像大小调整问题

[英]Adding scrollbars and image resizing issues

Ok so for those of you that have actually seen my posts, my code has evolved nicely to the point that i can now display my sprite sheet the way i want. 好的,对于那些真正看过我帖子的人,我的代码发展得很好,现在我可以按自己的方式显示我的Sprite表了。 Here is my code: 这是我的代码:

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

public class AnimTest
{
public static void main(String[] args)
{
    AnimTest test = new AnimTest();
    test.go();
}

public void go()
{
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MyDrawP drawP = new MyDrawP();
    frame.getContentPane().add(drawP);
    frame.setSize(640,640);
    frame.setVisible(true);
    frame.setResizable(true);

}
}

class MyDrawP extends JPanel
{

public void paintComponent(Graphics g)
{
    super.paintComponent(g);
    try {

        BufferedImage bigImg = ImageIO.read(new File("C:/Users/scott/Desktop/Personal Work/Pixel Art/terrain.png"));

        final int width = 64;
        final int height = 64;
        final int rows = 5;
        final int cols = 16;
        final int mapX = 10;
        final int mapY = 8;

        BufferedImage[][] sprites = new BufferedImage[cols][rows];

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                //System.out.println("J is: " + j);
                //System.out.println("I is: " + i);
                //System.out.println("i * width is: " + i * width);
                //System.out.println("j * height is: " + j * height);
                //System.out.println("");

                sprites[j][i] = bigImg.getSubimage(j * width, i * height, width, height);
                //g.drawImage(sprites[j][i], 5, 5, this);
            }
        }
            for (int i = 0; i < mapX; i++)
            {
                for (int j = 0; j < mapY; j++)
                {
                ArrayList<BufferedImage> spriteListRoad = new ArrayList<BufferedImage>();
                BufferedImage road4way = sprites[0][0];
                spriteListRoad.add(road4way);
                BufferedImage roadS = sprites[1][0];
                spriteListRoad.add(roadS);
                BufferedImage roadV = sprites[2][0];
                spriteListRoad.add(roadV);
                BufferedImage roadH = sprites[3][0];
                spriteListRoad.add(roadH);
                BufferedImage roadEndU = sprites[4][0];
                spriteListRoad.add(roadEndU);
                BufferedImage roadEndL = sprites[5][0];
                spriteListRoad.add(roadEndL);
                BufferedImage roadEndR = sprites[6][0];
                spriteListRoad.add(roadEndR);
                BufferedImage roadEndD = sprites[7][0];
                spriteListRoad.add(roadEndD);
                BufferedImage roadLU = sprites[8][0];
                spriteListRoad.add(roadLU);
                BufferedImage roadRU = sprites[9][0];
                spriteListRoad.add(roadRU);
                BufferedImage roadLD = sprites[10][0];
                spriteListRoad.add(roadLD);
                BufferedImage roadRD = sprites[11][0];
                spriteListRoad.add(roadRD);
                BufferedImage roadUT = sprites[12][0];
                spriteListRoad.add(roadUT);
                BufferedImage roadLT = sprites[13][0];
                spriteListRoad.add(roadLT);
                BufferedImage roadRT = sprites[14][0];
                spriteListRoad.add(roadRT);
                BufferedImage roadDT = sprites[15][0];
                spriteListRoad.add(roadDT);

                ArrayList<BufferedImage> spriteListRiver = new ArrayList<BufferedImage>();
                BufferedImage riverV = sprites[0][1];
                spriteListRiver.add(riverV);
                BufferedImage riverH = sprites[1][1];
                spriteListRiver.add(riverH);
                BufferedImage riverLU = sprites[2][1];
                spriteListRiver.add(riverLU);
                BufferedImage riverRU = sprites[3][1];
                spriteListRiver.add(riverRU);
                BufferedImage riverLD = sprites[4][1];
                spriteListRiver.add(riverLD);
                BufferedImage riverRD = sprites[5][1];
                spriteListRiver.add(riverRD);
                BufferedImage riverRoadV = sprites[6][1];
                spriteListRiver.add(riverRoadV);
                BufferedImage riverRoadH = sprites[7][1];
                spriteListRiver.add(riverRoadH);

                ArrayList<BufferedImage> spriteListTerrain = new ArrayList<BufferedImage>();
                BufferedImage grass = sprites[0][2];
                spriteListTerrain.add(grass);
                BufferedImage trees = sprites[1][2];
                spriteListTerrain.add(trees);
                BufferedImage mountain = sprites[2][2];
                spriteListTerrain.add(mountain);

                ArrayList<BufferedImage> spriteListOre = new ArrayList<BufferedImage>();
                BufferedImage copper = sprites[0][3];
                spriteListOre.add(copper);
                BufferedImage dornite = sprites[1][3];
                spriteListOre.add(dornite);
                BufferedImage kryz = sprites[2][3];
                spriteListOre.add(kryz);
                BufferedImage iron = sprites[3][3];
                spriteListOre.add(iron);
                BufferedImage shallux = sprites[4][3];
                spriteListOre.add(shallux);
                BufferedImage mithril = sprites[5][3];
                spriteListOre.add(mithril);

                ArrayList<BufferedImage> spriteListWood = new ArrayList<BufferedImage>();
                BufferedImage oak = sprites[0][4];
                spriteListWood.add(oak);
                BufferedImage olive = sprites[1][4];
                spriteListWood.add(olive);
                BufferedImage rath = sprites[2][4];
                spriteListWood.add(rath);
                BufferedImage yew = sprites[3][4];
                spriteListWood.add(yew);
                BufferedImage eeth = sprites[4][4];
                spriteListWood.add(eeth);
                BufferedImage ebony = sprites[5][4];
                spriteListWood.add(ebony);

                g.drawImage(spriteListTerrain.get((int) (Math.random() * 3)), i* width, j * height, this); //this one works by itself

                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

} }

And here is my question. 这是我的问题。 Why is it that when i execute this code, every time i resize the window it runs again and remakes my map? 为什么在执行此代码时,每次我调整窗口大小时都会再次运行并重新制作地图? I thought adding scrollbars might fix it, but i cant figure out how to add them in. How can i stop it from re-looping through the code every time i resize? 我以为添加滚动条可能会解决它,但是我不知道如何添加它们。每次调整大小时,如何阻止它重新遍历代码?

Thanks. 谢谢。

Draw the objects of interest to a BufferedImage and put it in a label. 将感兴趣的对象绘制到BufferedImage并将其放在标签中。 Note that it will not resize automatically. 请注意,它不会自动调整大小。

Alternately you might draw the objects to a BI and simply scale it to the frame size. 或者,您可以将对象绘制到BI并简单地将其缩放为框架大小。 This would only make sense if it takes a long time to construct. 只有花费很长时间才能构建,这才有意义。


id have to save all of those into one large BI, which i dont know how to do. id必须将所有这些保存到一个大型BI中,我不知道该怎么做。

Apparently you understand how to deal with a Graphics object. 显然您了解如何处理Graphics对象。 One can be obtained from a BI using either getGraphics() or createGraphics() . 可以使用getGraphics()createGraphics()从BI获得一个。

Why is it that when i execute this code, every time i resize the window it runs again and remakes my map? 为什么在执行此代码时,每次我调整窗口大小时都会再次运行并重新制作地图?

Your code for creating the map is located in the paintComponent() method. 用于创建地图的代码位于paintComponent()方法中。 This method is called every time Swing determines the component needs to be repainted. 每当Swing确定需要重新粉刷组件时,都会调用此方法。 Resizing the frame is one case where compnents need to be repainted. 调整框架大小是需要重新粉刷零件的一种情况。

For this reason your paintComponent() method should be as efficient as possible. 因此,您的paintComponent()方法应尽可能高效。 You should not be doing I/O in this method. 您不应该使用这种方法进行I / O。

Your current code doesn't make much sense to me. 您当前的代码对我来说意义不大。 You have two loops using variables i, j, but you build the ArrayLists using hard coded values. 您有两个使用变量i,j的循环,但是您使用硬编码值构建了ArrayList。 The only time you use the i, j variable is for the drawImage() method. 您唯一使用i,j变量的是drawImage()方法。

So, I would guess, the code to create the map should be done in an external method and then the paintComponent() method should only invoke the drawImage(..) within your loops. 因此,我猜想,用于创建地图的代码应在外部方法中完成,然后paintComponent()方法应仅在循环内调用drawImage(..)。

Also, since the drawImage() method uses the random() method, every time the paintComponent() method is invoked you will be changing the image. 另外,由于drawImage()方法使用random()方法,因此每次调用paintComponent()方法时,您都将更改图像。 This is probably not what you want, so in reality you probably need to create your entire BufferedImage externally. 这可能不是您想要的,因此实际上您可能需要在外部创建整个BufferedImage。 Then the image can be added to a JLabel and add the label to the frame so you don't need custom painting on the panel. 然后可以将图像添加到JLabel并将标签添加到框架,这样您就不需要在面板上自定义绘画。

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

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