Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7495283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:01:03+00:00 2026-05-29T18:01:03+00:00

Ok so for those of you that have actually seen my posts, my code

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:01:05+00:00Added an answer on May 29, 2026 at 6:01 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's actually a noticeable difference that I've seen but cannot explain. These timers have
I have seen System.Transactions namespace, and wondered, can I actually make a RDMBS with
I have three tables in my DB (actually a few more than that) these
I'm starting to learn Python and I've come across generator functions, those that have
It's one of those things that seems to have an odd curve where the
I have a GridView A that have many columns, one of those contains price
I have a few classes, such as those that outline database table structure or
Suppose that I have those three files: a.h //a.h header #include <stdio.h> int int_variable;
Say that I have two tables like those: Employers (id, name, .... , deptId).
Given that I have a table that holds vehicle information and one of those

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.