Resizing Text Area Fields in Grid Layout

So. I have a game where little sprites randomly move down the screen, and at the bottom is a user-controlled tank. The tank fires with the up key, and moves left and right. Each shot fired that hits a sprites gives the user one point, every sprite that passes the tank subtracts 5 points. The entire layout of the interfacing is in a grid. Our problem is that the tank and the textboxes need to be resized. For some reason, our tank needs to be in 9 different pieces in order to function somewhat properly and the textbox that displays the score is too small. We need help solving these 2 problems. How do we make it so that the tank is only one image as opposed to 9 and the textbox is large enough to displays the proper scores.

[737 byte] By [TheLemminga] at [2007-11-26 16:06:04]
# 1

import javax.swing.*;import java.awt.*;import java.awt.event.*;

public class Lemmings2 extends JFrame

{

private ImageIcon lemming; //stores the image of a lemming

private JLabel lblScore; //stores the label that displays the score

private JLabel[] label = new JLabel[2000]; //stores 234 labels

private ImageIcon[] tankPiece = new ImageIcon[9]; //stores the piecesof

the tank

private ImageIcon tank; //stores any one tank piece

private TextField txtInput;

private ImageIcon bullet;

private TextField txtOutput;

public Lemmings2 ()

{

boolean lose = false; //stores whether the player has lost yet

int x = 0; //stores a control variable

int z = 0; //stores a control variable

//stores 5 random numbers

double number = 0;

double number2 = 0;

double number3 = 0;

double number4 = 0;

double number5 = 0;

int score = 0; //stores the player's score

String scoreString = "";

setTitle ("Lemmings 2.0"); //sets the title of the GUI

setSize (850, 700); //sets the opening size of the GUI

setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); //sets the program to

close when the GUI is exited

Container lemmingsPane = getContentPane(); //sets the content layout of

the frame

lemmingsPane.setLayout(new GridLayout(50, 40)); //to style GridLayout

//saves all of the images from the specified filepaths

lemming = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//lemming.jpg");

tankPiece[0] = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//tankLeftTop.jpg");

tankPiece[1] = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//tankCenterTop.jpg");

tankPiece[2] = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//tankRightTop.jpg");

tankPiece[3] = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//tankLeftCenter.jpg");

tankPiece[4] = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//tankCenterCenter.jpg");

tankPiece[5] = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//tankRightCenter.jpg");

tankPiece[6] = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//tankLeftBottom.jpg");

tankPiece[7] = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//tankCenterBottom.jpg");

tankPiece[8] = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//tankRightBottom.jpg");

bullet = new ImageIcon("G://ICS 4MI//FINAL

PROJECT//Images//bullet.jpg");

lblScore = new JLabel();

//adds all labels to the GUI with a white background

for (x = 0; x < 2000; x++)

{

label[x] = new JLabel();

label[x].setOpaque(true);

label[x].setBackground(Color.WHITE);;

//label[x].addActionListener(new actionListener());

lemmingsPane.add(label[x]);

}

//changes some labels' icons to the pieces of the tank

label[1981].setIcon(tankPiece[8]);

label[1980].setIcon(tankPiece[7]);

label[1979].setIcon(tankPiece[6]);

label[1941].setIcon(tankPiece[5]);

label[1940].setIcon(tankPiece[4]);

label[1939].setIcon(tankPiece[3]);

label[1901].setIcon(tankPiece[2]);

label[1900].setIcon(tankPiece[1]);

label[1899].setIcon(tankPiece[0]);

txtInput = new TextField(1);

txtInput.setEditable(false);

txtInput.addKeyListener(new LemmingsKeyListener());

lemmingsPane.remove(label[1999]);

lemmingsPane.add(txtInput);

lemmingsPane.addKeyListener(new LemmingsKeyListener());

setVisible (true);

do

{

number = Math.floor(Math.random() * 100000000); //saves a random

number from 0 to 99999999

if (left == true)

{

if (label[1960].getIcon() != tankPiece[7])

{

moveTankLeft(label, tank, tankPiece);

}

left = false;

}

else if (right == true)

{

if (label[1999].getIcon() != tankPiece[7])

{

moveTankRight(label, tank, tankPiece);

}

right = false;

}

else if (fire == true)

{

for (x = 1880; x < 1920; x++)

{

if (label[x].getIcon() == tankPiece[1])

{

turret = x;

}

}

score = shootBullet(turret, label, score);

fire = false;

}

if (number > 99999990)

{

for (x = 1880; x >= 0; x--)

{

if (label[x].getIcon() == lemming) //if a label has an image of a

lemming in it

{

number5 = Math.floor(Math.random() * 10); //saves a random

number from 0 to 9

if (number5 > 3)

{

label[x].setIcon(null); //deletes any icon in this label

do

{

number4 = Math.floor(Math.random() * 10);

}

while (number4 > 2);

if (getErrorChecking(x, (int)number4) == false)

{

if (! (x % 40 == 0 && number4 == 0) && ! (x % 39 == 0 &&

number4 == 2))

{

label[x + (int)number4 + 40].setIcon(lemming);

}

}

for (z = 1880; z < 1920; z++)

{

if (label[z].getIcon() == lemming)

{

label[z].setIcon(null);

score--;

txtInput.setText("" + score);

}

}

}

}

}

do

{

number2 = Math.floor(Math.random() * 10);

}

while (number2 > 6);

number3 = Math.random();

if (number3 > 0.5)

{

number2 = number2 + 7;

}

label[(int)number2].setIcon(lemming);

}

}

while (lose == false);

}

private class LemmingsKeyListener implements KeyListener

{

public void keyPressed(KeyEvent e)

{

// Check if any cursor keys have been pressed and set flags.

if (e.getKeyCode() == KeyEvent.VK_LEFT)

{

left = true;

}

if (e.getKeyCode() == KeyEvent.VK_RIGHT)

{

right = true;

}

if (e.getKeyCode() == KeyEvent.VK_UP)

{

fire = true;

}

}

public void keyTyped(KeyEvent e)

{

}

public void keyReleased(KeyEvent e)

{

}

}

int turret;

boolean left = false;

boolean right = false;

boolean fire = false;

public boolean getErrorChecking(int x, int number)

{

boolean error = false;

for (int y = x; y < 2000; y++)

{

for (int a = 0; a < 9; a++)

{

tank = tankPiece[a];

if (label[x + number + 12].getIcon() == tank)

{

error = true;

}

}

}

return error;

}

public void moveTankLeft(JLabel[] label, ImageIcon tank, ImageIcon[]

tankPiece)

{

for (int x = 1880; x < 2000; x++)

{

for (int y = 0; y < 9; y++)

{

tank = tankPiece[y];

if (label[x].getIcon() == tank)

{

label[x].setIcon(null);

label[x - 1].setIcon(tank);

}

}

}

}

public void moveTankRight(JLabel[] label, ImageIcon tank, ImageIcon[ ]tankPiece)

{

for (int x = 1998; x >= 1880; x--)

{

for (int y = 8; y >= 0; y--)

{

tank = tankPiece[y];

if (label[x].getIcon() == tank)

{

label[x].setIcon(null);

label[x + 1].setIcon(tank);

}

}

}

}

public int shootBullet(int position, JLabel[] label, int score)

{

for (int x = position; x >= 0; x = x - 40)

{

if (x - 40 > 0)

{

if (label[x - 40].getIcon() != lemming)

{

label[x - 40].setIcon(bullet);

if (label[x].getIcon() != tankPiece[1])

{

label[x].setIcon(null);

}

}

else

{

label[x].setIcon(null);

label[x - 40].setIcon(null);

x = 0;

score++;

txtInput.setText("" + score);

}

}

else

{

label[x].setIcon(null);

}

}

return score;

}

public static void main (String[] args)

{

new Lemmings2 ();

}

}

TheLemminga at 2007-7-8 22:28:11 > top of Java-index,Other Topics,Java Game Development...
# 2

Ok, I think you should re-write the whole thing. What you are making can be written in about a tenth of the code that you have.

Just use a JFrame with a contentpane set to a jpanel. Draw directly onto the jpanel. You should only need one image. If you have any specific questions about how this can be done, just ask.

Nethera at 2007-7-8 22:28:11 > top of Java-index,Other Topics,Java Game Development...
# 3

Umm, I don't know how to re-write that...see we have a grid, and the lemming sprites move down the grid at a set speed. The user shoots the lemmings from his tank, what would the syntax look like for your idea, we're having lag problems which im guessing are from the grid we created. Is there anyway to make the lemmings move smoothly as well as the tank? Can you give us some actual code to use? Thanks.

TheLemminga at 2007-7-8 22:28:11 > top of Java-index,Other Topics,Java Game Development...
# 4
> Umm, I don't know how to re-write that.Did you even write what you already have? You should also split the game up into different classes.
CaptainMorgan08a at 2007-7-8 22:28:11 > top of Java-index,Other Topics,Java Game Development...
# 5

> Is there anyway to make the

> lemmings move smoothly as well as the tank?

Yes, draw them onto a JPanel instead of your grid. Grids are for tables, guis, boardgames, but not shooting games.

> Can you

> give us some actual code to use?

No, If you have a specific question i'll be glad to help you, but broad statements like "I need help, can I have code" are not focused enough for me to answer without writing your entire game for you.

Nethera at 2007-7-8 22:28:11 > top of Java-index,Other Topics,Java Game Development...