adding a JButton when there's a paint method

I'm creating a game application, sort of like a maze, and I want to add buttons to the levelOne panel to be able to move around the maze. When I add the buttons to the panel they don't appear, I assume the paint method is the reason for this. here's my code, I have 3 files, ill post the user_interface, and the levels class, where the level is created and where i tried to add the button. I tried putting the buttons in a JOptionPane, but then my JMenu wouldn't work at the same time the OptionPane was opened. If anyone knows a way around this instead, please let me know. I also tried using a separate class with a paintComponent method in it, and then adding the button (saw on a forum, not sure if it was this one), but that didn't work either. Also, if there is a way just to simply have the user press the directional keys on the keyboard and have the program perform a certain function when certain keys are pressed, I'd like to know, as that would solve my whole problem. Thanks.

//Libraries

//

import java.awt.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.text.*;

public class User_Interface extends JFrame

{

static Levels l = new Levels ();

static Delay d = new Delay ();

private Container contentPane = getContentPane ();

private JPanel main, levelOne;

private String level;

private CardLayout cc = new CardLayout ();

private GridBagConstraints gbc = new GridBagConstraints ();

private JPanel c = new JPanel ();

private String label = "MainMenu";

public User_Interface ()

{

//Generates the User-Interface

//

super ("Trapped");

main = new JPanel ();

GridBagLayout gbl = new GridBagLayout ();

main.setLayout (gbl);

c.setLayout (cc);

// set_gbc (gbc, 2, 2, 5, 5, GridBagConstraints.NONE);

c.add (main, "Main Page");

contentPane.add (c, BorderLayout.CENTER);

cc.show (c, "Main Page");

levelOne = new JPanel ();

levelOne.setLayout (new GridBagLayout ());

levelOne.setBackground (Color.black);

c.add (levelOne, "LevelOne");

JMenuBar menu = new JMenuBar ();

JMenu file = new JMenu ("File");

JMenu help = new JMenu ("Help");

JMenuItem about = new JMenuItem ("About");

JMenuItem mainmenu = new JMenuItem ("Main Menu");

mainmenu.addActionListener (

new ActionListener ()

{

public void actionPerformed (ActionEvent event)

{

cc.show (c, "Main Page");

label = "MainMenu";

}

}

);

JMenuItem newGame = new JMenuItem ("New Game");

newGame.addActionListener (

new ActionListener ()

{

public void actionPerformed (ActionEvent event)

{

l.xCord = 2;

l.yCord = 2;

l.xLoc = 140;

l.yLoc = 140;

JPanel entrance = new JPanel ();

entrance.setLayout (new FlowLayout ());

c.add (entrance, "Entrance");

label = "Entrance";

level = "ONE";

cc.show (c, "Entrance");

}

}

);

JMenuItem loadgame = new JMenuItem ("Load Game");

JMenuItem savegame = new JMenuItem ("Save Game");

JMenuItem exit = new JMenuItem ("Exit");

exit.addActionListener (

new ActionListener ()

{

public void actionPerformed (ActionEvent event)

{

JFrame frame = new JFrame ();

frame.setLocation (10, 10);

JOptionPane.showMessageDialog (frame, "Come Back Soon!", "TRAPPED", JOptionPane.INFORMATION_MESSAGE);

System.exit (0);

}

}

);

file.add (about);

file.add (mainmenu);

file.add (newGame);

file.add (loadgame);

file.add (savegame);

file.add (exit);

menu.add (file);

menu.add (help);

setJMenuBar (menu);

//Sets the size of the container (columns by rows)

//

setSize (750, 550);

//Sets the location of the container

setLocation (10, 10);

//Sets the background colour to a dark blue colour

//

main.setBackground (Color.black);

//Makes the container visible

//

setVisible (true);

}

public void paint (Graphics g)

{

super.paint (g);

g.setColor (Color.white);

if (label == "MainMenu")

{

for (int x = 0 ; x <= 50 ; ++x)

{

g.setColor (Color.white);

g.setFont (new Font ("Arial", Font.BOLD, x));

g.drawString ("TRAPPED", 100, 125);

d.delay (10);

if (x != 50)

{

g.setColor (Color.black);

g.drawString ("TRAPPED", 100, 125);

}

}

}

if (label == "Entrance")

{

l.Entrance ("ONE", g);

label = "LevelOne";

}

if (label == "LevelOne")

{

l.LevelOne (g, levelOne, gbc);

label = "";

}

}

public static void main (String[] args)

{

// calls the program

//

User_Interface application = new User_Interface ();

application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

} //End of Main Method

}

//Libraries

//

import java.awt.*;

import java.io.*;

import javax.swing.*;

import java.awt.event.*;

public class Levels extends Objects

{

private JFrame frame = new JFrame ();

{

//Sets location, size, and visiblity to the frame where the JOptionPane

//will be placed

//

frame.setLocation (600, 600);

frame.setSize (1, 1);

frame.setVisible (false);

}

public int xCord = 2;

public int yCord = 2;

public void LevelOne (Graphics g, JPanel one, GridBagConstraints gbc)

{

***Trying to add the button, doesn't appear. Tried adding to the container, and any JPanel i had created***

JButton button1 = new JButton ("TEST");

one.add (button1);

g.setColor (Color.white);

g.fillRect (500, 100, 200, 300);

g.setColor (Color.black);

g.drawRect (500, 100, 200, 300);

g.setFont (new Font ("Verdana", Font.BOLD, 25));

g.setColor (Color.black);

g.drawString ("LEVEL ONE", 525, 80);

//**********************************************************

//ROW ONE

//**********************************************************

counter = -80;

counter2 = 200;

for (int a = 1 ; a <= 7 ; ++a)

{

if (xCord < a && yCord == 0)

{

g.setColor (darkGray);

g.fillRect (xLoc + counter, yLoc - 80, 40, 40);

g.setColor (Color.black);

g.drawRect (xLoc + counter, yLoc - 80, 40, 40);

}

if (xCord > a - 1 && yCord == 0)

{

g.setColor (darkGray);

g.fillRect (xLoc + counter2, yLoc - 80, 40, 40);

g.setColor (Color.black);

g.drawRect (xLoc + counter2, yLoc - 80, 40, 40);

}

counter += 40;

counter2 += 40;

}

*****Theres 9 more rows, just edited out to save space****

int y = 100;

int x = 100;

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

{

g.setColor (Color.white);

g.fillRoundRect (x, y, 40, 40, 5, 5);

g.setColor (Color.black);

g.drawRoundRect (x, y, 40, 40, 5, 5);

y += 40;

if (a == 9)

{

x += 320;

y = 100;

}

}

x = 140;

y = 100;

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

{

g.setColor (Color.white);

g.fillRoundRect (x, y, 40, 40, 5, 5);

g.setColor (Color.black);

g.drawRoundRect (x, y, 40, 40, 5, 5);

x += 40;

if (a == 6)

{

x = 140;

y += 360;

}

}

g.setColor (Color.black);

g.drawRect (100, 100, 360, 400);

ImageIcon[] images = new ImageIcon [4];

images [0] = new ImageIcon ("arrow_left.gif");

images [1] = new ImageIcon ("arrow_up.gif");

images [2] = new ImageIcon ("arrow_down.gif");

images [3] = new ImageIcon ("arrow_right.gif");

int direction = -1;

*****This is where I tried to have the OptionPane show the directional buttons******

//frame.setVisible (true);

// direction = JOptionPane.showOptionDialog (frame, "Choose Your Path:", "Trapped", JOptionPane.YES_NO_CANCEL_OPTION,

// JOptionPane.QUESTION_MESSAGE,

// null,

// images,

// images [3]);

//

// if (direction == 0)

// {

//if (xCord - 1 > 0)

//{

// xCord -= 1;

// xLoc += 40;

//

//}

// }

//

//

// if (direction == 1)

// {

//if (yCord - 1 > 0)

//{

// yCord -= 1;

// yLoc += 40;

//}

// }

//

//

// if (direction == 2)

// {

//if (yCord + 1 < 9)

//{

// yCord += 1;

// yLoc -= 40;

//}

// }

//

//

// if (direction == 3)

// {

//if (xCord + 1 < 13)

//{

// xCord += 1;

// xLoc -= 40;

//}

// }

//LevelOne (g, one, gbc);

}

}

[9061 byte] By [jacob2932a] at [2007-11-27 5:57:26]
# 1

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting. I'm not even going to attempt to read 100s of line of unformatted code.

Don't override the paint() method of the frame. You should not have any control logic in that method. The method is strictly used for painting components that have been added to the frame.

> Also, if there is a way just to simply have the user press the

> directional keys on the keyboard and have the program perform a

> certain function when certain keys are pressed

[url http://java.sun.com/docs/books/tutorial/uiswing/misc/keybinding.html]How to Use Key Bindings[/url]

camickra at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 2

i tried adding a keylistener, that didn't work too well, i had tried to add it to a button and a textarea which i added to the 'one' frame, it didn't show up, and it didn't work. How would i go about changing the paint method so that it doesn't contain any logic? That's the only way I could think of getting the program to move forward in the game. Thanks.

//Libraries

//

import java.awt.*;

import java.io.*;

import java.util.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.text.*;

public class User_Interface extends JFrame

{

static Levels l = new Levels ();

static Delay d = new Delay ();

private Container contentPane = getContentPane ();

private JPanel main, levelOne;

private String level;

private CardLayout cc = new CardLayout ();

private GridBagConstraints gbc = new GridBagConstraints ();

private JPanel c = new JPanel ();

private String label = "MainMenu";

public User_Interface ()

{

//Generates the User-Interface

//

super ("Trapped");

main = new JPanel ();

GridBagLayout gbl = new GridBagLayout ();

main.setLayout (gbl);

c.setLayout (cc);

// set_gbc (gbc, 2, 2, 5, 5, GridBagConstraints.NONE);

c.add (main, "Main Page");

contentPane.add (c, BorderLayout.CENTER);

cc.show (c, "Main Page");

levelOne = new JPanel ();

levelOne.setLayout (new GridBagLayout ());

levelOne.setBackground (Color.black);

c.add (levelOne, "LevelOne");

JMenuBar menu = new JMenuBar ();

JMenu file = new JMenu ("File");

JMenu help = new JMenu ("Help");

JMenuItem about = new JMenuItem ("About");

JMenuItem mainmenu = new JMenuItem ("Main Menu");

mainmenu.addActionListener (

new ActionListener ()

{

public void actionPerformed (ActionEvent event)

{

cc.show (c, "Main Page");

label = "MainMenu";

}

}

);

JMenuItem newGame = new JMenuItem ("New Game");

newGame.addActionListener (

new ActionListener ()

{

public void actionPerformed (ActionEvent event)

{

l.xCord = 2;

l.yCord = 2;

l.xLoc = 140;

l.yLoc = 140;

JPanel entrance = new JPanel ();

entrance.setLayout (new FlowLayout ());

c.add (entrance, "Entrance");

label = "Entrance";

level = "ONE";

cc.show (c, "Entrance");

}

}

);

JMenuItem loadgame = new JMenuItem ("Load Game");

JMenuItem savegame = new JMenuItem ("Save Game");

JMenuItem exit = new JMenuItem ("Exit");

exit.addActionListener (

new ActionListener ()

{

public void actionPerformed (ActionEvent event)

{

JFrame frame = new JFrame ();

frame.setLocation (10, 10);

JOptionPane.showMessageDialog (frame, "Come Back Soon!", "TRAPPED", JOptionPane.INFORMATION_MESSAGE);

System.exit (0);

}

}

);

file.add (about);

file.add (mainmenu);

file.add (newGame);

file.add (loadgame);

file.add (savegame);

file.add (exit);

menu.add (file);

menu.add (help);

setJMenuBar (menu);

//Sets the size of the container (columns by rows)

//

setSize (750, 550);

//Sets the location of the container

setLocation (10, 10);

//Sets the background colour to a dark blue colour

//

main.setBackground (Color.black);

//Makes the container visible

//

setVisible (true);

}

public void paint (Graphics g)

{

super.paint (g);

g.setColor (Color.white);

if (label == "MainMenu")

{

for (int x = 0 ; x <= 50 ; ++x)

{

g.setColor (Color.white);

g.setFont (new Font ("Arial", Font.BOLD, x));

g.drawString ("TRAPPED", 100, 125);

d.delay (10);

if (x != 50)

{

g.setColor (Color.black);

g.drawString ("TRAPPED", 100, 125);

}

}

}

if (label == "Entrance")

{

l.Entrance ("ONE", g);

label = "LevelOne";

}

if (label == "LevelOne")

{

l.LevelOne (g, levelOne, gbc);

label = "";

}

//g.setColor (new Color

}

public static void main (String[] args)

{

// calls the program

//

User_Interface application = new User_Interface ();

application.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

} //End of Main Method

}

//Libraries

//

import java.awt.*;

import java.io.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.ActionMap;

import javax.swing.plaf.*;

public class Levels extends Objects

implements KeyListener

{

//static final String newline = System.getProperty ("line.separator");

JButton button;

private JFrame frame = new JFrame ();

{

//Sets location, size, and visiblity to the frame where the JOptionPane

//will be placed

//

frame.setLocation (600, 600);

frame.setSize (1, 1);

frame.setVisible (false);

}

JButton button1;

public int xCord = 2;

public int yCord = 2;

public void LevelOne (Graphics g, JPanel one, GridBagConstraints gbc)

{

//button = new JButton ("TEST");

//ButtonHandler handler = new ButtonHandler ();

//button.addActionListener (handler);

// one.add (button);

g.setColor (Color.white);

g.fillRect (500, 100, 200, 300);

g.setColor (Color.black);

g.drawRect (500, 100, 200, 300);

g.setFont (new Font ("Verdana", Font.BOLD, 25));

g.setColor (Color.black);

g.drawString ("LEVEL ONE", 525, 80);

//**********************************************************

//ROW ONE

//**********************************************************

counter = -80;

counter2 = 200;

for (int a = 1 ; a <= 7 ; ++a)

{

if (xCord < a && yCord == 0)

{

g.setColor (darkGray);

g.fillRect (xLoc + counter, yLoc - 80, 40, 40);

g.setColor (Color.black);

g.drawRect (xLoc + counter, yLoc - 80, 40, 40);

}

if (xCord > a - 1 && yCord == 0)

{

g.setColor (darkGray);

g.fillRect (xLoc + counter2, yLoc - 80, 40, 40);

g.setColor (Color.black);

g.drawRect (xLoc + counter2, yLoc - 80, 40, 40);

}

counter += 40;

counter2 += 40;

}

int y = 100;

int x = 100;

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

{

g.setColor (Color.white);

g.fillRoundRect (x, y, 40, 40, 5, 5);

g.setColor (Color.black);

g.drawRoundRect (x, y, 40, 40, 5, 5);

y += 40;

if (a == 9)

{

x += 320;

y = 100;

}

}

x = 140;

y = 100;

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

{

g.setColor (Color.white);

g.fillRoundRect (x, y, 40, 40, 5, 5);

g.setColor (Color.black);

g.drawRoundRect (x, y, 40, 40, 5, 5);

x += 40;

if (a == 6)

{

x = 140;

y += 360;

}

}

g.setColor (Color.black);

g.drawRect (100, 100, 360, 400);

ImageIcon[] images = new ImageIcon [4];

images [0] = new ImageIcon ("arrow_left.gif");

images [1] = new ImageIcon ("arrow_up.gif");

images [2] = new ImageIcon ("arrow_down.gif");

images [3] = new ImageIcon ("arrow_right.gif");

int direction = -1;

//frame.setVisible (true);

// direction = JOptionPane.showOptionDialog (frame, "Choose Your //\Path:", "Trapped", JOptionPane.YES_NO_CANCEL_OPTION,

// JOptionPane.QUESTION_MESSAGE,

// null,

// images,

// images [3]);

//

// if (direction == 0)

// {

//if (xCord - 1 > 0)

//{

// xCord -= 1;

// xLoc += 40;

//

//}

// }

//

//

// if (direction == 1)

// {

//if (yCord - 1 > 0)

//{

// yCord -= 1;

// yLoc += 40;

//}

// }

//

//

// if (direction == 2)

// {

//if (yCord + 1 < 9)

//{

// yCord += 1;

// yLoc -= 40;

//}

// }

//

//

// if (direction == 3)

// {

//if (xCord + 1 < 13)

//{

// xCord += 1;

// xLoc -= 40;

//}

// }

one.addKeyListener (this);

// one.add (button1);

if (xCord == 1)

{

LevelOne (g, one, gbc);

}

}

/** Handle the key typed event from the text field. */

public void keyTyped (KeyEvent e)

{

}

/** Handle the key pressed event from the text field. */

public void keyPressed (KeyEvent e)

{

if (e.getSource () == "Up")

{

JOptionPane.showMessageDialog (null, "Hi", "Hi", 0, null);

}

}

/** Handle the key released event from the text field. */

public void keyReleased (KeyEvent e)

{

// displayInfo (e, "KEY RELEASED: ");

}

}

jacob2932a at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 3

A quick scan of your code and I noticed you are using:

If (someThing == "?")

Use the equals() method to compare Objects, not "==".

> i tried adding a keylistener, that didn't work too well,

Who said anything about a KeyListener. I suggested a better approach which is to use KeyBindings.

You are still overriding the paint() method.

I guess I won't waste my time with other suggestions.

camickra at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 4
I read the page you linked to on key bindings, I don't know how to make Actions though, and I couldn't get it to work properly. And i said i don't know how to not override the paint method. Sorry for troubling you, I'm not that experienced with Java.
jacob2932a at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 5

You might want to have a look at this article:

[url=http://java.sun.com/products/jfc/tsc/articles/painting/index.html]Painting in AWT and Swing[/url]

I'm no expert in this, but I think that if you are using Swing components, you might want to look into overriding paintComponent not paint. This article goes into this.

petes1234a at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 6

I tried using paintcomponent before, and it said that paintcomponent was not found in javax.swing.JFrame. When i changed the extends to JPanel instead of JFrame, it worked, but then I got a bunch of other errors such as with my getcontentpane() line. I tried KeyBindings again, still no luck at getting it to work, I found an example program on the forum which moves an arrow around, thought I could use that to change the variables i need to change each time the arrow keys are pressed, but no luck.

jacob2932a at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 7

> I tried using paintcomponent before, and it said that

> paintcomponent was not found in javax.swing.JFrame.

> When i changed the extends to JPanel instead of

> JFrame, it worked, but then I got a bunch of other

> errors such as with my getcontentpane() line. I

> tried KeyBindings again, still no luck at getting it

> to work, I found an example program on the forum

> which moves an arrow around, thought I could use

> that to change the variables i need to change each

> time the arrow keys are pressed, but no luck.

Again, I'm no expert in this, but I think you should avoid painting a JFrame. I bet that you'll want to paint a jpanel or label or some such.

I think that you're trying to bite off more than you can chew here. First try a simple painting routine that does nothing fancy, just paints a picture on a jpanel or something like that. make sure it works and if it doesn't post the problems here. Then add functionality only after the previous simpler versions work. The divide and conquer method of coding.

Good luck!

petes1234a at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 8

thanks for your help, I searched a bit more around the forums and I found code for a key listener in a separate frame. I'm going to have buttons that can be clicked or be activated by using the arrow keys. If that doesn't work, I guess I'll have to start from the beginning with a JPanel. Thanks again.

jacob2932a at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 9

I hate being so negative but you aren't giving us anything to work with.

You post a program and we have no idea what it is supposed to do. You ask a question about using a KeyListener. You are told the using Key Bindings is a better approach. You read the tutorial but say you don't understand it, yet you post no code showing what you attempted to do.

You say you don't know how to write an Action. Well, the tutorial also has a link the shows you how to write an Action and even has a working example you can look at.

Now you say you want to use a KeyEvent and a JButton to do something. Well that is exactly what the purpose of creating an Action is for. You write one Action and then create the JButton using the Action. You can then also bind the Action to a KeyStroke. So you write the code once and use it in two places. That is why you don't want to use a KeyListener as I have told you twice before.

Now we move on to the paint() method. There is never any reason to override the paint() method of a JFrame. I have no idea what the code you have in that method is supposed to do so I can't suggest an alternative approach, but it does belong there. The paint methods in Swing are used to paint components, they are not used to control program logic flow. You can't control when the paint() method gets invoked. The JVM will determine how often and when the method gets invoked. And you still didn't even bother to change the code to not use the "==" operator. So yet again you haven't listened to my advice.

So step back a bit and start to write your program one step at a time. First write a simple program that uses a JButton that was created by using an Action. All the Action has to do is use System.out.println(..) do display a message. Next step is to map a KeyStroke to the same action. Once you master that you move forward. But there is no reason to post key with menu bars and menu items when you haven't even mastered the basics of writing a simple program. Learn to wak before you run.

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the posted code retains its original formatting.

camickra at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 10

I haven't changed the paint method, as I don't know how else to do it, but I changed the == to .equals, and I've used a keylistener because i couldn't make the keybinding work. I've got a wait () command now at the end of my method before it returns to the paint method, and when i run it the following error appears. If i click on the frame with the keylistener and actionlistener, i can still click the buttons and press the keys and it works fine, just the error message keeps appearing each time.Below the error is part of my code with the keylistener and the wait method being called. After it's done waiting, it returns to the original class and repaints the paint method. Any idea how to get rid of the error message? EDIT: I also tried adding synchronized (this) {} before the wait(), but the JFrame with the button with the keylistener on it didn't appear then.

java.lang.IllegalMonitorStateException: current thread not owner

at java.lang.Object.wait(Native Method)

at java.lang.Object.wait(Unknown Source)

at Levels.LevelOne(Levels.java:631)

at User_Interface.paint(User_Interface.java:182)

at javax.swing.JFrame.update(Unknown Source)

at sun.awt.RepaintArea.paint(Unknown Source)

at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

up.addKeyListener (new KeyAdapter ()

{

public void keyPressed (KeyEvent ke)

{

if (ke.getKeyCode () == 37)

{

//JOptionPane.showMessageDialog (f, "KeyCode: " + ke.getKeyCode (), "test", 0, null);

xCord -= 1;

xLoc += 40;

label = "LevelOne";

f.setVisible (false);

ok = true;

//repaint ();

}

else if (ke.getKeyCode () == 38)

{

yCord -= 1;

yLoc += 40;

label = "LevelOne";

f.setVisible (false);

ok = true;

}

else if (ke.getKeyCode () == 39)

{

xCord += 1;

xLoc -= 40;

ok = true;

label = "LevelOne";

f.setVisible (false);

}

else if (ke.getKeyCode () == 40)

{

yCord += 1;

yLoc -= 40;

label = "LevelOne";

f.setVisible (false);

ok = true;

}

else

{

label = "LevelOne";

}

}

}

);

up.addActionListener (new ActionListener ()

{

public void actionPerformed (ActionEvent e)

{

yCord -= 1;

yLoc += 40;

label = "LevelOne";

ok = true;

f.setVisible (false);

}

}

);

left.addActionListener (new ActionListener ()

{

public void actionPerformed (ActionEvent e)

{

xCord -= 1;

xLoc += 40;

label = "LevelOne";

ok = true;

f.setVisible (false);

}

}

);

right.addActionListener (new ActionListener ()

{

public void actionPerformed (ActionEvent e)

{

xCord += 1;

xLoc -= 40;

ok = true;

label = "LevelOne";

f.setVisible (false);

}

}

);

down.addActionListener (new ActionListener ()

{

public void actionPerformed (ActionEvent e)

{

yCord += 1;

yLoc -= 40;

label = "LevelOne";

ok = true;

f.setVisible (false);

}

}

);

while (!ok)

{

try

{

wait ();

}

catch (InterruptedException e)

{

}

}

ok = false;

}

Message was edited by:

jacob2932

Message was edited by:

jacob2932

jacob2932a at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...
# 11
> I haven't changed the paint method, as I don't know> how else to do it, .....Please don't say that. What you are doing is then wrong. Read the tutorial and learn how to do it. Else you will fail miserably.
petes1234a at 2007-7-12 16:30:24 > top of Java-index,Desktop,Core GUI APIs...