1

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. 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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319

2 Answers2

2

Draw the objects of interest to a BufferedImage and put it in a label. Note that it will not resize automatically.

Alternately you might draw the objects to a BI and simply scale it to the frame size. 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.

Apparently you understand how to deal with a Graphics object. One can be obtained from a BI using either getGraphics() or createGraphics().

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • If i were to do that, i would have to somehow condense 80 BI into one BI. My code loops through and places a new BI every 64 pixels until it reaches the end of a row. So id have to save all of those into one large BI, which i dont know how to do. – sekimberly52 Feb 12 '12 at 15:47
2

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. This method is called every time Swing determines the component needs to be repainted. Resizing the frame is one case where compnents need to be repainted.

For this reason your paintComponent() method should be as efficient as possible. You should not be doing I/O in this method.

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. The only time you use the i, j variable is for the drawImage() method.

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.

Also, since the drawImage() method uses the random() method, every time the paintComponent() method is invoked you will be changing the image. This is probably not what you want, so in reality you probably need to create your entire BufferedImage externally. 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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Ok so ive been reworking my code to try this method as well. Ive tried getting everything out of paintComponent except for the drawImage, except that the drawImage never knows what to draw because it seems to be impossible to pass anything into PaintComponent. Also, the entire point of my map making program was to have a random map. if i get rid of the random in the draw code, itll never do what i want it to. My code was confusing but what your suggesting makes even less sense. – sekimberly52 Feb 14 '12 at 15:03
  • What i dont understand even beyond all that is how do you ever call the paintComponent method? It always complains about Graphics g not being defined but im pretty sure you can never actually define it until the system just runs the method and it makes it by default. – sekimberly52 Feb 14 '12 at 15:08
  • I assume you have a method called "createMap". This should create a random map on a buffered image. This buffered image is then painted in the paintComponent() method. If you want to change the map then you need to invoke the "createMap" method again. The randomizing of the map should NOT be done in the paintComponent() method itself. The force the repainting of a component you invoke the repaint() method of the component. Maybe the `Draw on Image` example from [Custom Painting Approaches](http://tips4java.wordpress.com/2009/05/08/custom-painting-approaches/) will help with the basics. – camickr Feb 14 '12 at 15:58
  • So i figured out what was going wrong and now im to the point where i can draw images randomly each time the program is run and it doesnt change. But when it prints the images, only the first row is semi-random and then all the other rows are identical to the first. My Create method randomly adds my images to an ArrayList and my paintComponent looks at that list and displays them in a fashion i like, theyre just not really random. I feel like im doing this the wrong way, but its hard to really explain without my actual code. – sekimberly52 Feb 15 '12 at 00:48