Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

adding a JButton when there's a paint method

843806May 30 2007 — edited May 31 2007
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 ("T R A P P E D", 100, 125);
d.delay (10);
if (x != 50)
{
g.setColor (Color.black);
g.drawString ("T R A P P E D", 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);

}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 28 2007
Added on May 30 2007
11 comments
884 views