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 939375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:46:48+00:00 2026-05-15T21:46:48+00:00

I am adding an image into a JLabel and trying to set the width

  • 0

I am adding an image into a JLabel and trying to set the width and height… but it’s not working… help?

as you can see here… the tiles are not 32 by 32…

alt text

code:

import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
import java.lang.*;
import java.util.*;

public class LayoutExample extends JApplet {

    Label msgL = new Label("Messages");
    Label statusL = new Label("Status");
    Label inventoryTitleL = new Label("Your Inventory");
    Label idL = new Label("Name, Stats");
    Label messageInL = new Label("read message here");
    Label messageOutL = new Label("type message here");
    JLabel label2;
    JPanel topP;
    JPanel topRightP;
    JPanel gameGridP;
    JPanel inventoryP;
    JPanel inventoryTitleP;
    JPanel inventoryGridP;
    JPanel idP;
    JPanel messageInP;
    JPanel messageOutP;
    JButton slot;
    TextArea chat;
    TextField msg;

    /** Game variables **/
    Image[] tiles;
    Image[] weapon;
    Image player; // player image
    int x, y, px, py, tx, ty; // x tile - y tile // player x - player y // tile x - tile y
    boolean left, right, down, up, canMove, respawn;
    boolean drawFlail, drawBattleaxe;
    public static int[][] board;
    final int NUM_TILES = 522;
    final int NUM_WEAPS = 2;
    boolean[] weaponPicked;
    Label lx, ly; // to see where we are!
    int r1, r2, u1,u2,l1,l2,d1,d2, spawnX, spawnY;
    int rightSide = 480;
    int downSide = 320;
    int mapX = 15;
    int mapY = 10;
    String txtLastFX, txtLastFY;
    boolean boolRC, boolLC, boolUC, boolDC;
    int leftMap, upMap, rightMap, downMap, currentMap, map;
    public static TextArea c = new TextArea();
    public static TextArea stats = new TextArea();
    public static String newline;
    public static java.awt.List list;

    public static Font TimesR = new Font("MonoSpaced", Font.BOLD, 15);
    public static ArrayList<String> arr;
    public static tileGen anItem;
    public static boolean firstpush = false;
    String name, desc, typeOf, attackAdd, defenseAdd, canSell, canEat,earnedCoins,canEquip;
    public static int blkStr;


    int lastX, lastY, row, col;
    Label lbl1, lbl2, p1, p2, lblRC;

        String username;
    int attack, defense, gold, level, exp;

    public void init() {

        setMinimumSize(new Dimension(840,520));

        tiles = new Image[522];
        for(int i = 0;i < 522;i++) {
        tiles[i] = getImage(getClass().getResource(String.format("line_tile/t%d.png", i)));
        }

board = loadBoard(1);

        GridLayout frameGL = new GridLayout(3,2);

        frameGL.setVgap(0);
        frameGL.setHgap(0);

        //setLayout(new GridLayout(3,1));
        setLayout(frameGL);


        topP = new JPanel(new GridLayout(1,2));
        //topP.setBounds(0,0,640,520);
        topP.setMinimumSize(new Dimension(840,520));
        topP.setSize(new Dimension(840,520));
        GridLayout gameGridGL = new GridLayout(15,10);
        gameGridGL.setVgap(0);
        gameGridGL.setHgap(0);
        gameGridP = new JPanel(gameGridGL);

        gameGridP.setPreferredSize(new Dimension(580,320));



        topP.add(gameGridP);

        topRightP = new JPanel(new GridLayout(2,1));
        inventoryP = new JPanel(new GridLayout(2,1));
        inventoryTitleP = new JPanel(new FlowLayout());
        inventoryTitleP.setBounds(480,0,160,30);
        inventoryGridP = new JPanel(new GridLayout(5,4));
        //inventoryGridP.setBounds(480,0,160,128);
        //inventoryGridP.setHeight("160px");
        inventoryGridP.setPreferredSize(new Dimension(100,128));
        idP = new JPanel(new GridLayout(3,1));

        messageInP = new JPanel(new FlowLayout());
        messageOutP = new JPanel(new FlowLayout());

        chat = new TextArea(10,50);
        msg = new TextField(50);


        inventoryTitleP.add(inventoryTitleL);
        inventoryP.add(inventoryTitleP);

        topP.add(inventoryGridP);
        inventoryP.add(inventoryTitleP);
        inventoryP.add(inventoryGridP);
        topRightP.add(inventoryP);

        for (int number = 1; number <= 20; number++) {
        inventoryGridP.add(slot = new JButton("Slot " + number));
        }

        idP.add(idL);
        topRightP.add(idP);
        topP.add(topRightP);

        add(topP);

        messageOutP.add(chat);
        messageOutP.setSize(640,20);
        //add(messageOutP);

        messageInP.add(msg);
        messageInP.setSize(150,640);
        //add(messageInP);

        player = getImage(getClass().getResource("me.gif")); // our player

    }

private static final HashSet<Integer> BLOCKED_TILES = new HashSet<Integer>();
static {

BLOCKED_TILES.add(118);
BLOCKED_TILES.add(67);
BLOCKED_TILES.add(190);
BLOCKED_TILES.add(40);
BLOCKED_TILES.add(135);
BLOCKED_TILES.add(520);
BLOCKED_TILES.add(25);
BLOCKED_TILES.add(91);
BLOCKED_TILES.add(394);
BLOCKED_TILES.add(1);
BLOCKED_TILES.add(121);
//add more tiles here
}


    public void stop() {
    }

    public void paint(Graphics g) {



        for (row = 0; row < board.length; row++) {
        for (col = 0; col < board[row].length; col++) {
        int index = board[row][col];
        Icon newTile = new ImageIcon(tiles[index]);
        label2 = new JLabel(newTile);



    //g.drawImage(tiles[index], 32 * col, 32 * row, this);
        gameGridP.add(label2);
        System.out.println(row + " " + col);
        }
        }
    }

    public void update(Graphics g)
    {
    paint(g);
    }

    public int[][] loadBoard(int map) {
        if (map == 1) {
     return new int[][] {
    { 118, 118, 118, 118, 34, 343, 67, 1, 1, 1, 1, 1, 1, 1, 343, 343
     },
    { 118, 118, 118, 118, 34, 343, 1, 1, 1, 1, 7, 40, 7, 1, 343, 343
     },
    { 118, 118, 34, 34, 34, 34, 135, 167, 167, 1, 167, 167, 167, 1, 343, 343
     },
    { 118, 118, 34, 343, 343, 343, 1, 167, 167, 167, 167, 167, 167, 1, 343, 343
     },
    { 118, 67, 34, 343, 343, 343, 1, 1, 1, 1, 1, 1, 1, 1, 343, 343
     },
    { 118, 67, 34, 343, 394, 343, 1, 167, 167, 167, 167, 167, 167, 1, 343, 343
     },
    { 67, 67, 34, 343, 343, 343, 1, 167, 167, 167, 167, 167, 167, 1, 343, 343
     },
    { 34, 34, 34, 34, 34, 343, 1, 1, 167, 167, 167, 167, 167, 1, 343, 343
     },
    { 67, 121, 121, 67, 34, 343, 70, 1, 439, 25, 91, 25, 439, 1, 343, 343
     },
    { 118, 190, 190, 67, 34, 343, 343, 343, 343, 343, 34, 343, 343, 343, 343, 520
     },
    { 118, 190, 190, 67, 34, 34, 34, 34, 34, 34, 34, 520, 520, 520, 520, 520
     }
    };
     }else if (map == 4) {

      return new int[][] {

    { 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67, 343
     },
    { 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     }
    };

     }else if (map == 2) {

      return new int[][] {

    { 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67, 343
     },
    { 343, 67, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67, 67
     },
    { 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 67
     }
    };

     }else if (map == 3) {

      return new int[][] {

    { 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 124, 343
     },
    { 343, 118, 343, 118, 343, 118, 118, 118, 343, 118, 343, 343, 118, 343, 124, 343
     },
    { 343, 118, 343, 118, 343, 118, 343, 343, 343, 118, 343, 343, 118, 343, 124, 343
     },
    { 343, 118, 118, 118, 343, 118, 118, 343, 343, 118, 343, 343, 118, 343, 343, 124
     },
    { 343, 118, 343, 118, 343, 118, 343, 343, 343, 118, 343, 343, 118, 343, 343, 124
     },
    { 343, 118, 343, 118, 343, 118, 118, 118, 343, 118, 118, 343, 118, 118, 343, 124
     },
    { 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 124
     },
    { 343, 121, 343, 121, 121, 343, 121, 121, 121, 343, 121, 121, 121, 343, 343, 124
     },
    { 343, 121, 343, 121, 343, 343, 121, 343, 121, 121, 121, 343, 121, 343, 343, 124
     },
    { 343, 121, 343, 343, 121, 343, 121, 343, 121, 343, 121, 343, 121, 343, 343, 124
     },
    { 343, 121, 343, 121, 121, 343, 121, 121, 121, 121, 121, 121, 124, 124, 124, 124
     }
    };
     }else if (map == 5) {

      return new int[][] {

    { 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 343
     },
    { 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 133, 133, 133, 133, 343, 343, 343, 1, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 133, 167, 167, 133, 343, 343, 1, 1, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 133, 167, 167, 133, 343, 1, 343, 1, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 133, 167, 167, 133, 343, 1, 343, 1, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 133, 167, 167, 133, 343, 1, 1, 1, 1, 343, 343, 343, 343, 343, 343
     },
    { 343, 133, 133, 140, 133, 343, 343, 343, 1, 343, 343, 343, 343, 343, 343, 343
     },
    { 343, 343, 343, 343, 343, 343, 343, 343, 1, 343, 343, 343, 343, 343, 1, 1
     },
    { 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 343, 1, 1
     }
    };

     }

    return board;
    }


    public boolean blocked(int tx, int ty) {
    return BLOCKED_TILES.contains(board[ty][tx]);
}

}
  • 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-15T21:46:48+00:00Added an answer on May 15, 2026 at 9:46 pm

    Normally, you can call label.setMinimumSize(new Dimension(width, height)) to make something be forced at a size. Not sure exactly what you’re trying to do here – your SSCCE isn’t all that short.

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

Sidebar

Related Questions

Here is my code. This is adding image but images are being appeared in
I am trying to filter an image through a category. It works but not
I can draw an image into the navbar by adding this to the bottom
I'm trying to implement 'lever' into my android game, here's image showing what do
I developed a custom file manager for tinymce. But even if adding image to
I am dynamically adding an image to a canvas object which was working for
I'm trying to get the thumbnails from Youtube and insert the corresponding image into
I want to store image into my datatable and while adding colum I want
I am working with a wsdl which is automatically adding extra elements into the
I've been working on making my website compatible with not only chrome, but firefox

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.