Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Java UI Help
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chromium




PostPosted: Sun Apr 28, 2013 4:41 pm   Post subject: Java UI Help

Hello. Ive just started using Java in school, and unfortunately we are using Ready to Program. My final project is coming up and im planning making a game with a decent ui.
Ive read/watched some tutorials but rtp gives me errors when running apps that are using javax.swing. Im thinking of making some sort of whack-a-mole game where theres a 5x5 grid of buttons and a mole will randomly appear on one, then you have to click on it to get points. The game also has to have an educational component, so ill probably have a popup that comes up every so often where the user needs to complete a math question, which will then increment the level counter. Heres what im thinking it should look like:

Posted Image, might have been reduced in size. Click Image to view fullscreen.

I really need help here, so if anyone could suggest some tips about where i should get started or point me in the right direction.
Also from what ive dealt with over the past couple days, rtp seems useless, so i might also consider using a different ide. If anyone could let me know if
there are any ide's/editors that would be better in this case, that would be great. I cant use anything thats too different from rtp, but anything thats more practical would be nice.

Thanks in advance.
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Sun Apr 28, 2013 8:40 pm   Post subject: RE:Java UI Help

JCreator is the IDE I would call most similar to Turing. Most professionals use Eclipse, Netbeans or IntelliJ, but they are massive overkill for a beginner.

The most likely reason that you're getting errors when dealing with RTP and javax.swing is that RTP uses a built-in JVM with version 1.4.2 (or something like that), which means you can't just refer to the latest documentation or tutorials, you have to use versions that are around 10 years out of date.

The API documentation is here: http://docs.oracle.com/javase/1.4.2/docs/api/index.html . There's a tutorial on Swing here: http://docs.oracle.com/javase/tutorial/uiswing/TOC.html , but keep in mind that a lot of things may have changed between 1.4.2 and now.

If your teacher needs to be able to run your code, then you need to make sure you set up your IDE to the same compatibility level as your teacher will be using (probably whatever RTP uses). The exact procedure for that will vary depending on your IDE, and will require that you install the appropriate JVM first (you may not be able to do that at school).

For graphics, here's the tutorial: http://docs.oracle.com/javase/tutorial/2d/ ; but again keep in mind that some of those facilities may not exist in 1.4.2 .
chromium




PostPosted: Sat May 04, 2013 7:48 am   Post subject: Re: Java UI Help

Thanks the API was very useful. Ive managed to setup the ui, and ive added actionlisteners to all buttons. Ive set it to setText of a textfield to the variable score, when the buttons are clicked, but when i run the program and click on any button i get this error:

code:
Exception occurred during event dispatching:
java.lang.NullPointerException
        at WAM.actionPerformed(WAM.java:299)
        at java.awt.Button.processActionEvent(Unknown Source)
        at java.awt.Button.processEvent(Unknown Source)
        at java.awt.Component.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.run(Unknown Source)



Heres my code:
code:

import java.awt.*;
import java.util.Random;
import java.awt.event.*;

public class WAM implements ActionListener
{
    static Random rand;
    static Button b1, b2, b3, b4, b5, b6, b7, b8, b9;
    static TextField points;
    static int score;
   
    public WAM ()
    {

        rand = new Random ();
        int score = 0;
       
        Frame f = new Frame (); //creates console frame (f)
        f.setVisible (true);
        f.setTitle ("MyConsole");
        f.setSize (600, 500);
        f.setLocation (200, 200);
        Color greenBG = new Color (0x30A156);
        f.setBackground (greenBG);


        Label l = new Label ("Whack-A-Mole"); //title
        Color greenTitle = new Color (0x0A4414);
        l.setForeground (greenTitle);
        Font myFont = new Font ("sansserif", Font.BOLD + Font.ITALIC, 27);
        l.setFont (myFont);
        l.setVisible (true);
        l.setSize (200, 30);
        l.setLocation (205, 75);
        f.add (l);

        Label l2 = new Label ("Game by Priyesh Patel"); //subtitle
        Color greenSubTitle = new Color (0x7CFC92);
        l2.setForeground (greenSubTitle);
        Font myFont2 = new Font ("monospaced", Font.PLAIN, 13);
        l2.setFont (myFont2);
        l2.setVisible (true);
        l2.setSize (200, 30);
        l2.setLocation (215, 100);
        f.add (l2);

        TextField t = new TextField (); //textfield
        t.setVisible (true);
        t.setEditable (false);
        t.setSize (200, 20);
        t.setLocation (200, 200);
        f.add (t);

       
        b1 = new Button ("1"); //row 1
        b1.setVisible (true);
        b1.setBackground (Color.white);
        b1.setSize (60, 50);
        b1.setLocation (200, 240);
        b1.addActionListener (this);
        f.add (b1);

        b2 = new Button ("2");
        b2.setVisible (true);
        b2.setBackground (Color.white);
        b2.setSize (60, 50);
        b2.setLocation (270, 240);
        b2.addActionListener (this);
        f.add (b2);

        b3 = new Button ("3");
        b3.setVisible (true);
        b3.setBackground (Color.white);
        b3.setSize (60, 50);
        b3.setLocation (340, 240);
        b3.addActionListener (this);
        f.add (b3);

        b4 = new Button ("4"); //row 2
        b4.setVisible (true);
        b4.setBackground (Color.white);
        b4.setSize (60, 50);
        b4.setLocation (200, 300);
        b4.addActionListener (this);
        f.add (b4);

        b5 = new Button ("5");
        b5.setVisible (true);
        b5.setBackground (Color.white);
        b5.setSize (60, 50);
        b5.setLocation (270, 300);
        b5.addActionListener (this);
        f.add (b5);

        b6 = new Button ("6");
        b6.setVisible (true);
        b6.setBackground (Color.white);
        b6.setSize (60, 50);
        b6.setLocation (340, 300);
        b6.addActionListener (this);
        f.add (b6);

        b7 = new Button ("7"); //row 3
        b7.setVisible (true);
        b7.setBackground (Color.white);
        b7.setSize (60, 50);
        b7.setLocation (200, 360);
        b7.addActionListener (this);
        f.add (b7);

        b8 = new Button ("8");
        b8.setVisible (true);
        b8.setBackground (Color.white);
        b8.setSize (60, 50);
        b8.setLocation (270, 360);
        b8.addActionListener (this);
        f.add (b8);

        b9 = new Button ("9");
        b9.setVisible (true);
        b9.setBackground (Color.white);
        b9.setSize (60, 50);
        b9.setLocation (340, 360);
        b9.addActionListener (this);
        f.add (b9);

        TextField points = new TextField ("0"); //textfield for points
        points.setVisible (true);
        points.setEditable (true);
        points.setSize (150, 20);
        points.setLocation (15, 430);
        f.add (points);

        TextField level = new TextField (); //textfield for level
        level.setText ("Level: ");
        level.setVisible (true);
        level.setEditable (false);
        level.setSize (150, 20);
        level.setLocation (15, 450);
        f.add (level);

        for (int x = 0 ; x <= 50 ; x++) //loop which assigns mole to buttons randomly
        {
            int speed = 700; //how fast the color switches in milliseconds
            int num = rand.nextInt (9); //random num generator
            Color greenButton = new Color (0x34C318); //flash color

            if (num == 1)
            {
                b1.setBackground (greenButton);
                try
                {
                    Thread.sleep (speed);
                }
                catch (InterruptedException ex)
                {
                }
                b1.setBackground (Color.white);
            }
            else if (num == 2)
            {
                b2.setBackground (greenButton);
                try
                {
                    Thread.sleep (speed);
                }
                catch (InterruptedException ex)
                {
                }
                b2.setBackground (Color.white);
            }
            else if (num == 3)
            {
                b3.setBackground (greenButton);
                try
                {
                    Thread.sleep (speed);
                }
                catch (InterruptedException ex)
                {
                }
                b3.setBackground (Color.white);
            }
            else if (num == 4)
            {
                b4.setBackground (greenButton);
                try
                {
                    Thread.sleep (speed);
                }
                catch (InterruptedException ex)
                {
                }
                b4.setBackground (Color.white);
            }
            else if (num == 5)
            {
                b5.setBackground (greenButton);
                try
                {
                    Thread.sleep (speed);
                }
                catch (InterruptedException ex)
                {
                }
                b5.setBackground (Color.white);
            }
            else if (num == 6)
            {
                b6.setBackground (greenButton);
                try
                {
                    Thread.sleep (speed);
                }
                catch (InterruptedException ex)
                {
                }
                b6.setBackground (Color.white);
            }
            else if (num == 7)
            {
                b7.setBackground (greenButton);
                try
                {
                    Thread.sleep (speed);
                }
                catch (InterruptedException ex)
                {
                }
                b7.setBackground (Color.white);
            }
            else if (num == 8)
            {
                b8.setBackground (greenButton);
                try
                {
                    Thread.sleep (speed);
                }
                catch (InterruptedException ex)
                {
                }
                b8.setBackground (Color.white);
            }
            else if (num == 9)
            {
                b9.setBackground (greenButton);
                try
                {
                    Thread.sleep (speed);
                }
                catch (InterruptedException ex)
                {
                }
                b9.setBackground (Color.white);
            }
        }

    }


    public void actionPerformed (ActionEvent e)
    {

        if (e.getSource () == b1)
        {   
            score = score + 10;
            points.setText (Integer.toString (score));
        }
        else if (e.getSource () == b2)
        {   
            score = score + 10;
            points.setText (Integer.toString (score));
        } 
        else if (e.getSource () == b3)
        {   
            score = score + 10;
            points.setText (Integer.toString (score));
        }   
        else if (e.getSource () == b4)
        {   
            score = score + 10;
            points.setText (Integer.toString (score));
        }     
        else if (e.getSource () == b5)
        {   
            score = score + 10;
            points.setText (Integer.toString (score));
        }   
        else if (e.getSource () == b6)
        {   
            score = score + 10;
            points.setText (Integer.toString (score));
        }   
        else if (e.getSource () == b7)
        {   
            score = score + 10;
            points.setText (Integer.toString (score));
        }   
        else if (e.getSource () == b8)
        {   
            score = score + 10;
            points.setText (Integer.toString (score));
        }         
        else if (e.getSource () == b9)
        {   
            score = score + 10;
            points.setText (Integer.toString (score));
        }             
    }


    public static void main (String[] args)
    {
        new WAM ();
    } // main method
} // WAM class


Thanks.
DemonWasp




PostPosted: Sat May 04, 2013 12:54 pm   Post subject: RE:Java UI Help

The cause is right there in the exception: something is null but you tried to use it anyway, so you got a NullPointerException. If you look at the stack trace, the top line is where the exception was initially thrown (WAM.actionPerformed() at line 299 of WAM.java) and each successive line represents part of the call stack. In your case, the points variable is null (we know that it isn't score because score is an int and cannot be null).

There are also some serious code-repetition problems here. In general, don't repeat yourself: say any given thing exactly once.

For example, you often want to sleep for some number of milliseconds. Since you're ignoring the possible InterruptedException anyway, why not add a method:

Java:

private static void sleep ( long millis ) {
    try {
        Thread.sleep (speed);
    } catch (InterruptedException ex) {
        // Ignored
    }
}


Then you can replace 9 instances of that try-catch with a single sleep(speed).

You can do similar things to make setting up buttons easier.

You should also put your buttons in an array, which will make your life much easier. For example, the "game loop" that moves the moles around could be:

Java:

// If these don't change in your loop, then you can leave them outside.
// This could be important for the Color object, because you keep creating new ones on each iteration--oops!
int speed = 700; //how fast the color switches in milliseconds
Color green = new Color (0x34C318); //flash color

for (int x = 0 ; x <= 50 ; x++) {
    // Note: according to the documentation, random.nextInt(9) will generate numbers 0, 1, ..., 7, 8 (but not 9!)
    int num = rand.nextInt (9); //random num generator

    buttons[num].setBackground ( green );
    sleep ( speed );
    buttons[num].setBackground ( Color.white );
}


If you follow this pattern throughout your program, you should be able to delete the majority of your code. This means you can replace a lot of long, difficult-to-read code with shorter, easier-to-read code, which means fewer bugs and less hassle to change/improve things.
chromium




PostPosted: Thu May 09, 2013 1:12 pm   Post subject: Re: Java UI Help

Thanks again for your help. I fixed the random number generator by declaring it like this:
code:
int num = (rand.nextInt (9)) + 1;


I also used the method for the delay instead of remaking it each button.

However i still cant figure out how to fix the error when clicking on the buttons. Ive had a few people check it and they dont know how either. Could you please be a bit more specific in how to fix it? Im not really sure what you meant.

Thanks
DemonWasp




PostPosted: Thu May 09, 2013 1:53 pm   Post subject: RE:Java UI Help

This line:

code:
TextField points = new TextField ("0"); //textfield for points


declares a local variable "points" and assigns it to a new TextField.

However, this static member:

code:
static TextField points;


is never initialized because you initialized the local variable instead. That means it has the default value of null, so when execution gets to:

code:
points.setText (Integer.toString (score));


you are trying to reference through a null value ("points") rather than the text field you initialized earlier.


In short:
code:

public class Example {
    private static TextField points;   // this is the field you wanted to initialize

    public Example() {
        ...
        TextField points = new TextField(...); // this does NOT initialize the 'points' field above (it creates and initializes a local variable with the same name, which "shadows" the class member above).
        ...

        ...
        points = new TextField(...); // this will NOT create a local variable; it will initialize the member field above.
        ...
    }
}
chromium




PostPosted: Fri May 10, 2013 4:27 pm   Post subject: Re: Java UI Help

Thank you very much Smile ...Fixed it
chromium




PostPosted: Sun May 26, 2013 6:41 pm   Post subject: Re: Java UI Help

Hello again. I need some help on how to add an image to the background of a java awt button.
I understand that you can use this for JButtons but it doesnt seem to work on my buttons.
code:
ImageIcon pic = new ImageIcon("pic.gif");
button1.setIcon(pic);


Any advice would be greatly appreciated. Smile
Sponsor
Sponsor
Sponsor
sponsor
chromium




PostPosted: Tue May 28, 2013 12:22 pm   Post subject: Re: Java UI Help

anyone?...i need HAAAAAAAAAAAALLLLPPPPPP Sad Sad Sad Sad Sad
DemonWasp




PostPosted: Tue May 28, 2013 1:06 pm   Post subject: RE:Java UI Help

Try reducing the size of the problem. That is, try the icons-on-buttons thing in isolation.

Open a new file, and try to write the smallest possible program to put a button in a frame with an icon set. If it works there, then try to figure out what's different between that smaller example and the larger code you were working on.

If it still doesn't work, post your code here with an explanation of what it does, what you expect it to do, any errors you encounter, etc.
Zren




PostPosted: Tue May 28, 2013 1:13 pm   Post subject: RE:Java UI Help

I'm guessing there was an error when you ran that code, what was it? How do you interpret that error?

~

First look in the docs. Specifically, you'd be looking for a property you can set. So look at the setters (functions starting with "set" Eg: setBlarg). Look at both the list of functions that are defined in this class, and the list of those that are inherited.
http://docs.oracle.com/javase/6/docs/api/java/awt/Button.html

I can't see one. So we check with google.
https://www.google.ca/search?q=java.awt.button+image

Which has a link to stack overflow.
http://stackoverflow.com/questions/6718944/how-to-make-awt-button-and-use-imageicon-icon

Which says that the AWT buttons don't natively support images on them. Instead it links to a tutorial on extending the class to create that feature.
chromium




PostPosted: Fri May 31, 2013 1:22 pm   Post subject: Re: Java UI Help

Ive decided to convert my program UI to swing (JFrame, JButtons, etc.). With awt it seemed inefficient and a lot of the resources i found werent working properly.
I managed to do this in about 5 mintues but i have an issue. When i run the program it comes up with a blank window (without any of my components) however if i maximise the window everything shows up as it should.

Heres my code:

code:

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

public class WAM extends JFrame implements ActionListener
{
   
    //frame components
    private static JButton bb1, bb2, bb3, bb4, bb5, bb6, b1, b2, b3, b4, b5, b6, b7, b8, b9, bbb1; //buttons
    private static JTextField points = new JTextField ("0"); //textfield for points
    private static JTextField level = new JTextField (); //textfield for level
    private static JTextField mathJTextField = new JTextField ();   
    private static JLabel selected;
    private static JLabel mathJLabel1;
    private static JLabel mathJLabel2;
    static JPanel test1;
   
    //frames
    static JFrame f0 = new JFrame ("Configuration"); //config window
    static JFrame f = new JFrame (); //creates game console frame (f)
    static JFrame f1 = new JFrame (); //creates math frame
   
    //Colors
    static Color greenJButton = new Color (0x34C318); //green flash color;
    static Color greenBG = new Color (0x30A156); //green background color           
   
    //numbers
    static int pointsNum = 0; //number located in points textfield
    static int levelNum = 1; //number located in level textfield
    static int speed = 700; //how fast the color switches in milliseconds (defaults to 700 if no speed selected)
    static Random rand;   
   
    //fonts
    static Font myFont = new Font ("sansserif", Font.BOLD + Font.ITALIC, 27); //Bold Italic
    static Font myFont2 = new Font ("monospaced", Font.PLAIN, 13); //Courier New
    static Font myFont3 = new Font ("sansserif", Font.BOLD, 14);  //Bold     
    static Font myFont4 = new Font ("sansserif", Font.PLAIN, 15); //Normal 
           
    public WAM ()
    {
        //Configuration Window
        f0.setVisible (true);
       // f0.setBackground (greenBG);
        f0.setSize (500, 500);
        f0.setLocation (0,0);
       
        JLabel welcome = new JLabel ("Welcome to Whack-a-Mole");
        welcome.setVisible (true);
        welcome.setSize (350, 50);
        welcome.setLocation (75, 40);
        welcome.setFont (myFont);
        f0.getContentPane().add (welcome);
       
        JLabel speedSubtitle = new JLabel ("Choose a Speed:");
        speedSubtitle.setVisible (true);
        speedSubtitle.setSize (120, 20);
        speedSubtitle.setLocation (130, 150);
        speedSubtitle.setFont (myFont3);
        f0.getContentPane().add (speedSubtitle);
       
        bb1 = new JButton ("N00b");
        bb1.setVisible (true);
        bb1.setSize (60, 40);
        bb1.setLocation (80, 190);
        bb1.addActionListener (this);
        f0.getContentPane().add (bb1);   
       
        bb2 = new JButton ("Easy");
        bb2.setVisible (true);
        bb2.setSize (60, 40);
        bb2.setLocation (150, 190);
        bb2.addActionListener (this);
        f0.getContentPane().add (bb2);
       
        bb3 = new JButton ("Medium");
        bb3.setVisible (true);
        bb3.setSize (60, 40);
        bb3.setLocation (220, 190);
        bb3.addActionListener (this);
        f0.getContentPane().add (bb3);
       
        bb4 = new JButton ("Hard");
        bb4.setVisible (true);
        bb4.setSize (60, 40);
        bb4.setLocation (290, 190);
        bb4.addActionListener (this);
        f0.getContentPane().add (bb4);

        bb5 = new JButton ("Insane");
        bb5.setVisible (true);
        bb5.setSize (60, 40);
        bb5.setLocation (360, 190);
        bb5.addActionListener (this);
        f0.getContentPane().add (bb5);
       
        bb6 = new JButton ("Start Game");
        bb6.setVisible (true);
        bb6.setSize (80, 40);
        bb6.setLocation (410, 450);
        bb6.addActionListener (this);
        f0.getContentPane().add (bb6);
       
        selected = new JLabel (); //displays the speed that the user has selected
        selected.setVisible (true);
        selected.setLocation (260,151);
        selected.setSize (100,20);
        f0.getContentPane().add (selected);
       
        JLabel TimeJLabel = new JLabel ("Game Starting in:");
        TimeJLabel.setFont (myFont2);
        TimeJLabel.setVisible (true);
        TimeJLabel.setLocation (20,460);
        TimeJLabel.setSize (135,20);
        f0.getContentPane().add (TimeJLabel);
       
        JLabel TimeL = new JLabel (); //Countdown label
        TimeL.setFont (myFont);
        TimeL.setVisible (true);
        TimeL.setLocation (165,460);
        TimeL.setSize (40,20);
        f0.getContentPane().add (TimeL); 

        for (int i = 3; i>-1; i--) //countdown
        {
        TimeL.setText (""+i);
        sleep2 (1000);
        }
       
        f0.dispose (); //close config window
 
        //Game Window
        f.setVisible (true);
        f.setTitle ("MyConsole");
        f.setSize (600, 500);
        f.setLocation (0,0);
        //f.setBackground (greenBG);

        JLabel l = new JLabel ("Whack-A-Mole"); //title
        Color greenTitle = new Color (0x0A4414);
        l.setForeground (greenTitle);
        l.setFont (myFont);
        l.setVisible (true);
        l.setSize (200, 30);
        l.setLocation (205, 75);
        f.getContentPane().add (l);

        JLabel l2 = new JLabel ("Game by Priyesh Patel"); //subtitle
        Color greenSubTitle = new Color (0x7CFC92);
        l2.setForeground (greenSubTitle);
        l2.setFont (myFont2);
        l2.setVisible (true);
        l2.setSize (200, 30);
        l2.setLocation (215, 100);
        f.getContentPane().add (l2);
       
        b1 = new JButton ("1"); //row 1
        b1.setVisible (true);
        b1.setBackground (Color.white);
        b1.setSize (60, 50);
        b1.setLocation (200, 240);
        b1.addActionListener (this);
        f.getContentPane().add (b1);

        b2 = new JButton ("2");
        b2.setVisible (true);
        b2.setBackground (Color.white);
        b2.setSize (60, 50);
        b2.setLocation (270, 240);
        b2.addActionListener (this);
        f.getContentPane().add (b2);

        b3 = new JButton ("3");
        b3.setVisible (true);
        b3.setBackground (Color.white);
        b3.setSize (60, 50);
        b3.setLocation (340, 240);
        b3.addActionListener (this);
        f.getContentPane().add (b3);

        b4 = new JButton ("4"); //row 2
        b4.setVisible (true);
        b4.setBackground (Color.white);
        b4.setSize (60, 50);
        b4.setLocation (200, 300);
        b4.addActionListener (this);
        f.getContentPane().add (b4);

        b5 = new JButton ("5");
        b5.setVisible (true);
        b5.setBackground (Color.white);
        b5.setSize (60, 50);
        b5.setLocation (270, 300);
        b5.addActionListener (this);
        f.getContentPane().add (b5);

        b6 = new JButton ("6");
        b6.setVisible (true);
        b6.setBackground (Color.white);
        b6.setSize (60, 50);
        b6.setLocation (340, 300);
        b6.addActionListener (this);
        f.getContentPane().add (b6);

        b7 = new JButton ("7"); //row 3
        b7.setVisible (true);
        b7.setBackground (Color.white);
        b7.setSize (60, 50);
        b7.setLocation (200, 360);
        b7.addActionListener (this);
        f.getContentPane().add (b7);

        b8 = new JButton ("8");
        b8.setVisible (true);
        b8.setBackground (Color.white);
        b8.setSize (60, 50);
        b8.setLocation (270, 360);
        b8.addActionListener (this);
        f.getContentPane().add (b8);

        b9 = new JButton ("9");
        b9.setVisible (true);
        b9.setBackground (Color.white);
        b9.setSize (60, 50);
        b9.setLocation (340, 360);
        b9.addActionListener (this);
        f.getContentPane().add (b9);

        points.setVisible (true); //points textfield
        points.setEditable (false);
        points.setSize (80, 20);
        points.setLocation (80, 430);
        f.getContentPane().add (points);

        JLabel pointsJLabel = new JLabel ("Points:"); //points label
        pointsJLabel.setFont (myFont3);
        pointsJLabel.setLocation (20, 430);
        pointsJLabel.setSize (80, 20);
        f.getContentPane().add (pointsJLabel);

        level.setVisible (true); //level textfield
        level.setText (Integer.toString (levelNum));
        level.setEditable (false);
        level.setSize (80, 20);
        level.setLocation (80, 450);
        f.getContentPane().add (level);

        JLabel levelJLabel = new JLabel ("Level:"); //level label
        levelJLabel.setFont (myFont3);
        levelJLabel.setLocation (20, 450);
        levelJLabel.setSize (80, 20);
        f.getContentPane().add (levelJLabel);

        rand = new Random ();
        for (int x = 0 ; x <= 50 ; x++) //loop which assigns mole to buttons randomly
        {
            int num = (rand.nextInt (9)) + 1; //random num generator

            if (num == 1)
            {
                b1.setBackground (greenJButton);
                sleep (speed);
                b1.setBackground (Color.white);
            }
            else if (num == 2)
            {
                b2.setBackground (greenJButton);
                sleep (speed);
                b2.setBackground (Color.white);
            }
            else if (num == 3)
            {
                b3.setBackground (greenJButton);
                sleep (speed);
                b3.setBackground (Color.white);
            }
            else if (num == 4)
            {
                b4.setBackground (greenJButton);
                sleep (speed);
                b4.setBackground (Color.white);
            }
            else if (num == 5)
            {
                b5.setBackground (greenJButton);
                sleep (speed);
                b5.setBackground (Color.white);
            }
            else if (num == 6)
            {
                b6.setBackground (greenJButton);
                sleep (speed);
                b6.setBackground (Color.white);
            }
            else if (num == 7)
            {
                b7.setBackground (greenJButton);
                sleep (speed);
                b7.setBackground (Color.white);
            }
            else if (num == 8)
            {
                b8.setBackground (greenJButton);
                sleep (speed);
                b8.setBackground (Color.white);
            }
            else if (num == 9)
            {
                b9.setBackground (greenJButton);
                sleep (speed);
                b9.setBackground (Color.white);
            }
           
            if (pointsNum == 50) //opens math window when user reaches 50 points
            {
            pointsNum = 51;
            levelNum = levelNum + 1;
            level.setText (Integer.toString (levelNum));           
            f.dispose();
           
            f1.setVisible (true);
            f1.setTitle ("Math");
            f1.setSize (300, 150);
            f1.setLocation (300, 250);
           // f1.setBackground (greenBG);
           
            int math1 = (rand.nextInt (50)) + 1; //random math int1
            int math2 = (rand.nextInt (50)) + 1; //random math int2
           
            mathJLabel1 = new JLabel ("" + math1); //label for first random number
            mathJLabel1.setVisible (true);
            mathJLabel1.setFont (myFont3);
            mathJLabel1.setLocation (90, 75);
            mathJLabel1.setSize (20, 20);
            f1.getContentPane().add (mathJLabel1);

            JLabel operator = new JLabel ("+");
            operator.setFont (myFont3);
            operator.setVisible (true);
            operator.setLocation (110, 75);
            operator.setSize (20, 20);
            f1.getContentPane().add (operator);

            mathJLabel2 = new JLabel ("" + math2); //label for second random number
            mathJLabel2.setFont (myFont3);
            mathJLabel2.setVisible (true);
            mathJLabel2.setLocation (130, 75);
            mathJLabel2.setSize (20, 20);
            f1.getContentPane().add (mathJLabel2);

            JLabel equals = new JLabel ("=");
            equals.setFont (myFont3);
            equals.setVisible (true);
            equals.setLocation (150, 75);
            equals.setSize (20, 20);
            f1.getContentPane().add (equals);   
               
            mathJTextField = new JTextField (); //answer input field
            mathJTextField.setVisible (true);
            mathJTextField.setLocation (170, 75);
            mathJTextField.setSize (30, 20);
            f1.getContentPane().add (mathJTextField);
               
            JLabel result = new JLabel ("answer");
            result.setFont (myFont3);
            result.setVisible (true);
            result.setLocation (120, 110);
            result.setSize (50, 20);
            f1.getContentPane().add (result);
           
            bbb1 = new JButton ("Enter");
            bbb1.setVisible (true);
            bbb1.setLocation (210, 75);
            bbb1.setSize (50, 20);
            f1.getContentPane().add (bbb1);
               
            int ans = math1 + math2;
            int userAns = Integer.parseInt (mathJTextField.getText ());

            if (userAns == ans)
            {
                 result.setText ("Correct");
            }
            else
            {
                 result.setText ("Wrong");
            }
            }
        }

    }

    public void actionPerformed (ActionEvent e)
    {   //speed assignment from choosing buttons
        if (e.getSource () == bb1)
        {
            speed = 1000;     
            selected.setText ("Noob Selected");           
        }
        if (e.getSource () == bb2)
        {
            speed = 800;
            selected.setText ("Easy Selected");             
        }
        if (e.getSource () == bb3)
        {
            speed = 700;
            selected.setText ("Medium Selected");             
        }
        if (e.getSource () == bb4)
        {
            speed = 400;
            selected.setText ("Hard Selected");             
        }
        if (e.getSource () == bb5)
        {
            speed = 200;
            selected.setText ("Insane Selected");             
        }   
        if (e.getSource () == bb6)
        {
            f0.dispose ();
        }         
        //adds points to textfield if the buttons are pressed and if the mole is on it 
        if (e.getSource () == b1 && b1.getBackground () == greenJButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b2 && b2.getBackground () == greenJButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b3 && b3.getBackground () == greenJButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b4 && b4.getBackground () == greenJButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b5 && b5.getBackground () == greenJButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b6 && b6.getBackground () == greenJButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b7 && b7.getBackground () == greenJButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b8 && b8.getBackground () == greenJButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b9 && b9.getBackground () == greenJButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        if (e.getSource () == bbb1)
        {
       
        }       
    }


    public static void main (String[] args)
    {
        new WAM ();
    } // main method


    private static void sleep (long millis)
    { //delay method for mole switching
        try
        {
            Thread.sleep (speed);
        }
        catch (InterruptedException ex)
        {
        }
    }
   
    private static void sleep2 (long millis2)
    { //delay method for config screen countdown
        try
        {
            Thread.sleep (1000);
        }
        catch (InterruptedException ex)
        {
        }
    }
   
   
} // WAM class


If also attached some screenshots of how it looks normal and maximised. Any help would be much appreciated.



normal.JPEG
 Description:
 Filesize:  11.67 KB
 Viewed:  6500 Time(s)

normal.JPEG



maximised.JPG
 Description:
 Filesize:  33.82 KB
 Viewed:  182 Time(s)

maximised.JPG


DemonWasp




PostPosted: Fri May 31, 2013 2:45 pm   Post subject: RE:Java UI Help

Google is your friend. Try the phrase "jframe blank". The top link is to a very similar question asked (and answered!) on StackOverflow. Read the answer posted there, it will help.
chromium




PostPosted: Mon Jun 03, 2013 8:08 pm   Post subject: Re: Java UI Help

Ok ive gone back to using awt. Do you guys have any ideas of how i can make my main game frame restart after completing the first round?
Ive made it so that after getting to 50 points (where the next level will start) it will make the frame invisible and then after asnwering the math question correct it will set it to visible again. However this doesnt seem like a good way to go about this. All of the variables stay the same and it doesnt work well.
Heres my code so far:

code:

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

public class WAM implements ActionListener
{
    static Random rand;
   
    //frame components
    static Button bb1, bb2, bb3, bb4, bb5, bb6, b1, b2, b3, b4, b5, b6, b7, b8, b9, bbb1; //buttons
    private static TextField points = new TextField ("0"); //textfield for points
    private static TextField level = new TextField (); //textfield for level
    private static TextField tf1 = new TextField ();
    private static TextField mathTextField = new TextField ();   
    static Label selected;
    static Label mathLabel1;
    static Label mathLabel2;
    static Label result;
    static Label mathCounter;
   
    //frames
    static Frame f0 = new Frame ("Configuration"); //config window
    static Frame f = new Frame (); //creates game console frame (f)
    static Frame f1 = new Frame (); //creates math frame
   
    //Colors
    static Color greenButton = new Color (0x34C318); //green flash color;
    static Color greenBG = new Color (0x30A156); //green background color           
   
    //numbers
    static int pointsNum = 0; //number located in points textfield
    static int levelNum = 1; //number located in level textfield
    static int speed = 700; //how fast the color switches in milliseconds (defaults to 700 if no speed selected)
    static int math1; //random math int1
    static int math2; //random math int2   
   
    //fonts
    static Font myFont = new Font ("sansserif", Font.BOLD + Font.ITALIC, 27); //Bold Italic
    static Font myFont2 = new Font ("monospaced", Font.PLAIN, 13); //Courier New
    static Font myFont3 = new Font ("sansserif", Font.BOLD, 14);  //Bold     
    static Font myFont4 = new Font ("sansserif", Font.PLAIN, 15); //Normal 
           
    public WAM ()
    {

        //Configuration Window
        f0.setVisible (true);
        f0.setBackground (greenBG);
        f0.setSize (500, 500);
        f0.setLocation (200, 200);
       
        Label welcome = new Label ("Welcome to Whack-a-Mole");
        welcome.setVisible (true);
        welcome.setSize (350, 50);
        welcome.setLocation (75, 40);
        welcome.setFont (myFont);
        f0.add (welcome);
       
        Label speedSubtitle = new Label ("Choose a Speed:");
        speedSubtitle.setVisible (true);
        speedSubtitle.setSize (120, 20);
        speedSubtitle.setLocation (130, 150);
        speedSubtitle.setFont (myFont3);
        f0.add (speedSubtitle);
       
        bb1 = new Button ("N00b");
        bb1.setVisible (true);
        bb1.setSize (60, 40);
        bb1.setLocation (80, 190);
        bb1.addActionListener (this);
        f0.add (bb1);   
       
        bb2 = new Button ("Easy");
        bb2.setVisible (true);
        bb2.setSize (60, 40);
        bb2.setLocation (150, 190);
        bb2.addActionListener (this);
        f0.add (bb2);
       
        bb3 = new Button ("Medium");
        bb3.setVisible (true);
        bb3.setSize (60, 40);
        bb3.setLocation (220, 190);
        bb3.addActionListener (this);
        f0.add (bb3);
       
        bb4 = new Button ("Hard");
        bb4.setVisible (true);
        bb4.setSize (60, 40);
        bb4.setLocation (290, 190);
        bb4.addActionListener (this);
        f0.add (bb4);

        bb5 = new Button ("Insane");
        bb5.setVisible (true);
        bb5.setSize (60, 40);
        bb5.setLocation (360, 190);
        bb5.addActionListener (this);
        f0.add (bb5);
       
        bb6 = new Button ("Start Game");
        bb6.setVisible (true);
        bb6.setSize (80, 40);
        bb6.setLocation (410, 450);
        bb6.addActionListener (this);
        f0.add (bb6);
       
        selected = new Label (); //displays the speed that the user has selected
        selected.setVisible (true);
        selected.setLocation (260,151);
        selected.setSize (100,20);
        f0.add (selected);
       
        Label TimeLabel = new Label ("Game Starting in:");
        TimeLabel.setFont (myFont2);
        TimeLabel.setVisible (true);
        TimeLabel.setLocation (20,460);
        TimeLabel.setSize (135,20);
        f0.add (TimeLabel);
       
        Label TimeL = new Label (); //Countdown label
        TimeL.setFont (myFont);
        TimeL.setVisible (true);
        TimeL.setLocation (165,460);
        TimeL.setSize (40,20);
        f0.add (TimeL); 

        for (int i = 3; i>-1; i--) //countdown
        {
        TimeL.setText (""+i);
        sleep2 (1000);
        }
       
        f0.dispose (); //close config window
 
        //Game Window
        f.setVisible (true);
        f.setTitle ("MyConsole");
        f.setSize (600, 500);
        f.setLocation (200, 200);
        f.setBackground (greenBG);
       
        f.addWindowListener(new WindowAdapter(){ //makes game window closable
        public void windowClosing(WindowEvent we){
        System.exit(0);
        }});       

        Label l = new Label ("Whack-A-Mole"); //title
        Color greenTitle = new Color (0x0A4414);
        l.setForeground (greenTitle);
        l.setFont (myFont);
        l.setVisible (true);
        l.setSize (200, 30);
        l.setLocation (205, 75);
        f.add (l);

        Label l2 = new Label ("Game by Priyesh Patel"); //subtitle
        Color greenSubTitle = new Color (0x7CFC92);
        l2.setForeground (greenSubTitle);
        l2.setFont (myFont2);
        l2.setVisible (true);
        l2.setSize (200, 30);
        l2.setLocation (215, 100);
        f.add (l2);
       
        b1 = new Button ("1"); //row 1
        b1.setVisible (true);
        b1.setBackground (Color.white);
        b1.setSize (60, 50);
        b1.setLocation (200, 240);
        b1.addActionListener (this);
        f.add (b1);

        b2 = new Button ("2");
        b2.setVisible (true);
        b2.setBackground (Color.white);
        b2.setSize (60, 50);
        b2.setLocation (270, 240);
        b2.addActionListener (this);
        f.add (b2);

        b3 = new Button ("3");
        b3.setVisible (true);
        b3.setBackground (Color.white);
        b3.setSize (60, 50);
        b3.setLocation (340, 240);
        b3.addActionListener (this);
        f.add (b3);

        b4 = new Button ("4"); //row 2
        b4.setVisible (true);
        b4.setBackground (Color.white);
        b4.setSize (60, 50);
        b4.setLocation (200, 300);
        b4.addActionListener (this);
        f.add (b4);

        b5 = new Button ("5");
        b5.setVisible (true);
        b5.setBackground (Color.white);
        b5.setSize (60, 50);
        b5.setLocation (270, 300);
        b5.addActionListener (this);
        f.add (b5);

        b6 = new Button ("6");
        b6.setVisible (true);
        b6.setBackground (Color.white);
        b6.setSize (60, 50);
        b6.setLocation (340, 300);
        b6.addActionListener (this);
        f.add (b6);

        b7 = new Button ("7"); //row 3
        b7.setVisible (true);
        b7.setBackground (Color.white);
        b7.setSize (60, 50);
        b7.setLocation (200, 360);
        b7.addActionListener (this);
        f.add (b7);

        b8 = new Button ("8");
        b8.setVisible (true);
        b8.setBackground (Color.white);
        b8.setSize (60, 50);
        b8.setLocation (270, 360);
        b8.addActionListener (this);
        f.add (b8);

        b9 = new Button ("9");
        b9.setVisible (true);
        b9.setBackground (Color.white);
        b9.setSize (60, 50);
        b9.setLocation (340, 360);
        b9.addActionListener (this);
        f.add (b9);

        points.setVisible (true); //points textfield
        points.setEditable (false);
        points.setSize (80, 20);
        points.setLocation (80, 430);
        f.add (points);

        Label pointsLabel = new Label ("Points:"); //points label
        pointsLabel.setFont (myFont3);
        pointsLabel.setLocation (20, 430);
        pointsLabel.setSize (80, 20);
        f.add (pointsLabel);

        level.setVisible (true); //level textfield
        level.setText (Integer.toString (levelNum));
        level.setEditable (false);
        level.setSize (80, 20);
        level.setLocation (80, 450);
        f.add (level);

        Label levelLabel = new Label ("Level:"); //level label
        levelLabel.setFont (myFont3);
        levelLabel.setLocation (20, 450);
        levelLabel.setSize (80, 20);
        f.add (levelLabel);

        rand = new Random ();
        for (int x = 0 ; x <= 10000 ; x++) //loop which assigns mole to buttons randomly
        {
            int num = (rand.nextInt (9)) + 1; //random num generator

            if (num == 1)
            {
                b1.setBackground (greenButton);
                sleep (speed);
                b1.setBackground (Color.white);
            }
            else if (num == 2)
            {
                b2.setBackground (greenButton);
                sleep (speed);
                b2.setBackground (Color.white);
            }
            else if (num == 3)
            {
                b3.setBackground (greenButton);
                sleep (speed);
                b3.setBackground (Color.white);
            }
            else if (num == 4)
            {
                b4.setBackground (greenButton);
                sleep (speed);
                b4.setBackground (Color.white);
            }
            else if (num == 5)
            {
                b5.setBackground (greenButton);
                sleep (speed);
                b5.setBackground (Color.white);
            }
            else if (num == 6)
            {
                b6.setBackground (greenButton);
                sleep (speed);
                b6.setBackground (Color.white);
            }
            else if (num == 7)
            {
                b7.setBackground (greenButton);
                sleep (speed);
                b7.setBackground (Color.white);
            }
            else if (num == 8)
            {
                b8.setBackground (greenButton);
                sleep (speed);
                b8.setBackground (Color.white);
            }
            else if (num == 9)
            {
                b9.setBackground (greenButton);
                sleep (speed);
                b9.setBackground (Color.white);
            }
           
            if (pointsNum == 50) //opens math window when user reaches 50 points
            {
            pointsNum = 51; //stops loop from repeating
            levelNum = levelNum + 1;
            level.setText (Integer.toString (levelNum));           
            f.setVisible(false);
           
            f1.setVisible (true);
            f1.setTitle ("Math");
            f1.setSize (300, 150);
            f1.setLocation (300, 250);
            f1.setBackground (greenBG);
           
            math1 = (rand.nextInt (50)) + 1; //random math int1
            math2 = (rand.nextInt (50)) + 1; //random math int2
           
            mathLabel1 = new Label ("" + math1); //label for first random number
            mathLabel1.setVisible (true);
            mathLabel1.setFont (myFont3);
            mathLabel1.setLocation (90, 75);
            mathLabel1.setSize (20, 20);
            f1.add (mathLabel1);

            Label operator = new Label ("+");
            operator.setFont (myFont3);
            operator.setVisible (true);
            operator.setLocation (110, 75);
            operator.setSize (20, 20);
            f1.add (operator);

            mathLabel2 = new Label ("" + math2); //label for second random number
            mathLabel2.setFont (myFont3);
            mathLabel2.setVisible (true);
            mathLabel2.setLocation (130, 75);
            mathLabel2.setSize (20, 20);
            f1.add (mathLabel2);

            Label equals = new Label ("=");
            equals.setFont (myFont3);
            equals.setVisible (true);
            equals.setLocation (150, 75);
            equals.setSize (20, 20);
            f1.add (equals);   
               
            mathTextField = new TextField (); //answer input field
            mathTextField.setVisible (true);
            mathTextField.setLocation (170, 75);
            mathTextField.setSize (30, 20);
            f1.add (mathTextField);
               
            result = new Label ();
            result.setFont (myFont3);
            result.setVisible (true);
            result.setLocation (120, 110);
            result.setSize (70, 20);
            f1.add (result); 
           
            mathCounter = new Label ();
            mathCounter.setVisible (true);
            mathCounter.setFont (myFont3);
            mathCounter.setLocation (30,30);
            mathCounter.setSize (250,20);
            f1.add (mathCounter);
           
            bbb1 = new Button ("Enter");
            bbb1.setVisible (true);
            bbb1.setLocation (210, 75);
            bbb1.setSize (50, 20);
            bbb1.addActionListener (this);
            f1.add (bbb1);
           
            for (int z = 10; z>-1; z--) //math countdown
            {
            mathCounter.setText ("You have "+z+" second left to answer.");
            sleep2 (1000);
            }
           
            }
        }

    }

    public void actionPerformed (ActionEvent e)
    {   //speed assignment from choosing buttons
        if (e.getSource () == bb1)
        {
            speed = 1000;     
            selected.setText ("Noob Selected");           
        }
        if (e.getSource () == bb2)
        {
            speed = 800;
            selected.setText ("Easy Selected");             
        }
        if (e.getSource () == bb3)
        {
            speed = 700;
            selected.setText ("Medium Selected");             
        }
        if (e.getSource () == bb4)
        {
            speed = 400;
            selected.setText ("Hard Selected");             
        }
        if (e.getSource () == bb5)
        {
            speed = 200;
            selected.setText ("Insane Selected");             
        }   
        if (e.getSource () == bb6)
        {
            f0.dispose ();
        }         
        //adds points to textfield if the buttons are pressed and if the mole is on it 
        if (e.getSource () == b1 && b1.getBackground () == greenButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b2 && b2.getBackground () == greenButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b3 && b3.getBackground () == greenButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b4 && b4.getBackground () == greenButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b5 && b5.getBackground () == greenButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b6 && b6.getBackground () == greenButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b7 && b7.getBackground () == greenButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b8 && b8.getBackground () == greenButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        else if (e.getSource () == b9 && b9.getBackground () == greenButton)
        {
            pointsNum = pointsNum + 10;
            points.setText (Integer.toString (pointsNum));
        }
        if (e.getSource () == bbb1)
        { //user clicks on answer button
            int ans = math1 + math2;
            int userAns = Integer.parseInt (mathTextField.getText ());

            if (userAns == ans)
            {
                 result.setText ("Correct!");
                 f1.dispose();
                 f.setVisible(true);
                 
                 pointsNum = 0;
                 points.setText (Integer.toString (pointsNum));
            }
            else
            {
                 result.setText ("Wrong!");
            }       
           
        }       
    }


    public static void main (String[] args)
    {
        new WAM ();
    } // main method


    private static void sleep (long millis)
    { //delay method for mole switching
        try
        {
            Thread.sleep (speed);
        }
        catch (InterruptedException ex)
        {
        }
    }
   
    private static void sleep2 (long millis2)
    { //delay method for config screen countdown
        try
        {
            Thread.sleep (1000);
        }
        catch (InterruptedException ex)
        {
        }
    }
   
   
} // WAM class


Thanks
chromium




PostPosted: Sat Jun 08, 2013 9:14 am   Post subject: Re: Java UI Help

How to I add a background image to my frame?
I understand that I should use paint since I'm using awt, but I have 3 different frames and I'm confused about how I would specificy which frame to paint the image to.
Please halp me, and thanks in advance.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: