我正在將圖像添加到JLabel並嘗試設置寬度和高度...但它不工作...幫助?Java - 設置JLabel的寬度/高度
,你可以在這裏看到...瓷磚不是32×32 ...
alt text http://i25.tinypic.com/339ks9x.png
代碼:
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]);
}
}