
-----------------------------------
chromium
Sun Apr 28, 2013 4:41 pm

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:

http://i.imgur.com/l7dQASv.jpg

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.

-----------------------------------
DemonWasp
Sun Apr 28, 2013 8:40 pm

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
Sat May 04, 2013 7:48 am

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)[/code]


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 -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 -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 -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
[/code]

Thanks

-----------------------------------
chromium
Sat Jun 08, 2013 9:14 am

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.

-----------------------------------
chromium
Mon Jun 10, 2013 10:16 am

Re: Java UI Help
-----------------------------------
Anyone know how?

-----------------------------------
Zren
Mon Jun 10, 2013 11:49 am

RE:Java UI Help
-----------------------------------
Each of your Frame instances is using the method defined in the Frame class (or whatever subclass defined it). In order to draw differently for a specific frame, you are going to need to subclass the Frame class and override the paint method.


Frame configFrame; // Regular
NargleFrame nargleFrame; // Custom frame

class NargleFrame extends Frame {
  @Override
  public void paint(Graphics g) {
    super.paint(g);
  }
}


Thing is, by overriding the paint method, you need to make sure to call super or the default behaviour (drawing all it's children probably) won't happen.
