Results 1 to 16 of 16
  1. #1
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default Java Gave Question

    Hey Guys, I am posting this in a new post because i got something different.

    This is my Assignment:

    This assignment handles topics of events and GUI-elements. It's a little game. The task: create a button that randomly relocates to a different position on the screen each time it is hit. The player has to hit the button 15 times, the score is the time needed.

    Your first task: write the necessary event handling and the game-framework (counter, score etc.). Measure the time using java's System.currentTimeMillis() method (see API). Display the time elapsed and the number of remaining hits on separate JLabels.


    I have worked on this code for 4 days straight now. It is due in 24 hours and i really need your guys help.

    This is the code i have so far. I need to add a north and south panel and make the timing and clicker work.
    The members have helped me so much, but i still cannot get it.
    Can some1 please help me:

    Java Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    
    public class JumpingPanel extends JFrame{
        public static final int BUTTON_WIDTH = 45;
        public static final int BUTTON_HEIGHT = 45;
        private static final Dimension screenSize =
        		Toolkit.getDefaultToolkit().getScreenSize(); // Dimension of the screen
    
        private JPanel middle; // The content panel of this frame
        private TargetPanel target; // The button array
    
        private static void resizeComponent(Component c) {
    	    Dimension d = c.getSize(null);
                c.setPreferredSize(new Dimension((int)(0.9*d.width), (int)(0.9*d.height)));
        }
    
    
       // The panel with 3x3 array of buttons
        private static class TargetPanel extends JPanel implements ActionListener {
                private JButton theMainButton; // Central button in button array
                private Random rand; // Random number generator used in relocating
    
                /** When we hit a button, if the central one, we relocate
                    the target panel
                */
            public void actionPerformed(ActionEvent ae) {
                JButton source = (JButton)ae.getSource();
                if (source == theMainButton) {
    
    		resizeComponent(this);
                    setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
                                rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));
                    validate();
                }
            }
            // Here is the target panel with the nine buttons
            public TargetPanel() {
                rand = new Random();
                setLayout(new GridLayout(3,3));
    
                for (int r = 0; r < 3; r++) {
                    for (int c = 0; c < 3; c++) {
                        JButton button = new JButton();
                        if (r == 1 && c == 1) {
                            theMainButton = button;
                            theMainButton.addActionListener(this);
                        }
                        button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
                        add(button);
                    }
                }
                setMaximumSize(new Dimension(3*BUTTON_WIDTH,3*BUTTON_HEIGHT));
                Border border = BorderFactory.createLineBorder(Color.BLACK, 4);
                setBorder(border);
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JumpingPanel frame = new JumpingPanel();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Jumping Panel");
            frame.setMinimumSize(screenSize);
    
            frame.middle = (JPanel)frame.getContentPane();
            frame.middle.setLayout(new FlowLayout());
    
            frame.target = new TargetPanel();
            frame.middle.add(frame.target);
    
            frame.setVisible(true);
    	}
    }

  2. #2
    s2sgateway is offline Member
    Join Date
    Sep 2010
    Posts
    17
    Rep Power
    0

    Default

    hey i have made some changes to print the clicks whenever the panel moves its location the value is printed in console the varaible is n
    Java Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    
    public class JumpingPanel extends JFrame{
        public static final int BUTTON_WIDTH = 45;
        public static final int BUTTON_HEIGHT = 45;
    static int n=0;
        private static final Dimension screenSize =
        		Toolkit.getDefaultToolkit().getScreenSize(); // Dimension of the screen
    
        private JPanel middle; // The content panel of this frame
        private TargetPanel target; // The button array
    
        private static void resizeComponent(Component c) {
    	    Dimension d = c.getSize(null);
                c.setPreferredSize(new Dimension((int)(0.9*d.width), (int)(0.9*d.height)));
        }
    
    
       // The panel with 3x3 array of buttons
        private static class TargetPanel extends JPanel implements ActionListener {
                private JButton theMainButton; // Central button in button array
                private Random rand; // Random number generator used in relocating
    
                /** When we hit a button, if the central one, we relocate
                    the target panel
                */
            public void actionPerformed(ActionEvent ae) {
                JButton source = (JButton)ae.getSource();
                if (source == theMainButton) {
    
    		resizeComponent(this);
                    setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
                                rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));
    
    n++;
    
    System.out.println(n);                validate();
                }
    
            }
            // Here is the target panel with the nine buttons
            public TargetPanel() {
                rand = new Random();
                setLayout(new GridLayout(3,3));
    
                for (int r = 0; r < 3; r++) {
                    for (int c = 0; c < 3; c++) {
                        JButton button = new JButton();
                        if (r == 1 && c == 1) {
                            theMainButton = button;
                            theMainButton.addActionListener(this);
    
                        }
                        button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
                        add(button);
    
                    }
                }
                setMaximumSize(new Dimension(3*BUTTON_WIDTH,3*BUTTON_HEIGHT));
                Border border = BorderFactory.createLineBorder(Color.BLACK, 4);
                setBorder(border);
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JumpingPanel frame = new JumpingPanel();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Jumping Panel");
            frame.setMinimumSize(screenSize);
    
            frame.middle = (JPanel)frame.getContentPane();
            frame.middle.setLayout(new FlowLayout());
    
            frame.target = new TargetPanel();
            frame.middle.add(frame.target);
    
            frame.setVisible(true);
    	}
    }

  3. #3
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Hey thanks,

    I dont see anything different. Where is it printing the time and the counts of clicks??

  4. #4
    s2sgateway is offline Member
    Join Date
    Sep 2010
    Posts
    17
    Rep Power
    0

    Default

    see in console that is command prompt you are running it will show the values.If you are running in eclipse see server console

  5. #5
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Hey Okay i got it to now say how many times it clicked, but i want it to show after 15 clicks.

    Right now it shows after each click, How can i do it only after 15 clicks?


    Java Code:
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    
    
    public class JumpingPanel extends JFrame{
        public static final int BUTTON_WIDTH = 45;
        public static final int BUTTON_HEIGHT = 45;
    static int n=0;
        private static final Dimension screenSize =
        		Toolkit.getDefaultToolkit().getScreenSize(); // Dimension of the screen
    
        private JPanel middle; // The content panel of this frame
        private TargetPanel target; // The button array
    
        private static void resizeComponent(Component c) {
    	    Dimension d = c.getSize(null);
                c.setPreferredSize(new Dimension((int)(0.9*d.width), (int)(0.9*d.height)));
        }
    
    
       // The panel with 3x3 array of buttons
        private static class TargetPanel extends JPanel implements ActionListener {
                private JButton theMainButton; // Central button in button array
                private Random rand; // Random number generator used in relocating
    
                /** When we hit a button, if the central one, we relocate
                    the target panel
                */
            public void actionPerformed(ActionEvent ae) {
                JButton source = (JButton)ae.getSource();
                if (source == theMainButton) {
    
    		resizeComponent(this);
                    setLocation(rand.nextInt(screenSize.width-3*BUTTON_WIDTH),
                                rand.nextInt(screenSize.height-3*BUTTON_HEIGHT));
    
    n++;
    
    JOptionPane.showMessageDialog(null, "You Clicked it " +n);
    validate();
                }
    
            }
            // Here is the target panel with the nine buttons
            public TargetPanel() {
                rand = new Random();
                setLayout(new GridLayout(3,3));
    
                for (int r = 0; r < 3; r++) {
                    for (int c = 0; c < 3; c++) {
                        JButton button = new JButton();
                        if (r == 1 && c == 1) {
                            theMainButton = button;
                            theMainButton.addActionListener(this);
    
                        }
                        button.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
                        add(button);
    
                    }
                }
                setMaximumSize(new Dimension(3*BUTTON_WIDTH,3*BUTTON_HEIGHT));
                Border border = BorderFactory.createLineBorder(Color.BLACK, 4);
                setBorder(border);
            }
        }
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            JumpingPanel frame = new JumpingPanel();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setTitle("Jumping Panel");
            frame.setMinimumSize(screenSize);
    
            frame.middle = (JPanel)frame.getContentPane();
            frame.middle.setLayout(new FlowLayout());
    
            frame.target = new TargetPanel();
            frame.middle.add(frame.target);
    
            frame.setVisible(true);
    	}
    }

  6. #6
    s2sgateway is offline Member
    Join Date
    Sep 2010
    Posts
    17
    Rep Power
    0

    Default

    just check a condition say if(n>15) print the values that's it

  7. #7
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Hey thanks.
    I finished that part.

    Now what i need to do is, From the time i start clicking to the 15th click i need to record the time and then display it.

    Can you please help?



    p.s. You really are a life saver, I am in debt to you.
    Thank you again

  8. #8
    s2sgateway is offline Member
    Join Date
    Sep 2010
    Posts
    17
    Rep Power
    0

    Default

    hey some network problem that's why late to respond import these packages
    import java.util.*;
    import java.util.Date;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;

    and after every 15 click add these code in if block


    Java Code:
    
    
        Calendar calendar = new GregorianCalendar();
        String am_pm;
        int hour = calendar.get(Calendar.HOUR);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        if(calendar.get(Calendar.AM_PM) == 0)
          am_pm = "AM";
        else
          am_pm = "PM";
        System.out.println("Current Time : " + hour + ":" 
    + minute + ":" + second + " " + am_pm);
      }
    }

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    @s2sgateway: please don't keep on spoonfeeding the OPs; they won't learn anything from it. Giving hints in the right direction is much more effective and they have to do the work they have to do; now they only copy and paste your work and claim it's theirs.

    kind regards,

    Jos

  10. #10
    s2sgateway is offline Member
    Join Date
    Sep 2010
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    @s2sgateway: please don't keep on spoonfeeding the OPs; they won't learn anything from it. Giving hints in the right direction is much more effective and they have to do the work they have to do; now they only copy and paste your work and claim it's theirs.

    kind regards,

    Jos
    Sure man he is trying hard to find the answer that's why i given answer thanks for your advice

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    @s2sgateway
    When you do give an answer you should explain how you decided that the answer was the correct one and document ALL of your code so the OP can easily understand what you did.
    Just giving an answer is the lazy way. Try to help the OP learn by showing him what you had to do to find the answer and how the answer works.

  12. #12
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Guys he is not just giving the answers. I no the syntex behind these, but i just cant it to work correctly.

    @s2sgateway - thank you very much for your help. You actualy teached me more than my own teacher.

  13. #13
    acash229 is offline Member
    Join Date
    Sep 2010
    Posts
    60
    Rep Power
    0

    Default

    Hey i need to make a timer like a stopwatch. That code that you provided was the actual time.
    I need like a stopwatch method.
    thanks

  14. #14
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Use System.currentTimeMillis()... store it once at the start, and check it again once in a while to see when the difference is 15000 different (15 seconds = 15000 milliseconds).

  15. #15
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default

    Is this the same problem as: Need Help with timer (Java)

    Please only use one thread for each problem
    Which thread do you want the answer on? Which thread is finished?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Need help with java question
    By ccie007 in forum New To Java
    Replies: 23
    Last Post: 05-18-2010, 06:32 PM
  2. Java Question :D
    By thisisIT in forum New To Java
    Replies: 6
    Last Post: 03-12-2010, 04:04 PM
  3. Java question
    By TGH in forum New To Java
    Replies: 12
    Last Post: 11-27-2009, 02:05 PM
  4. question about java rmi
    By hakimade in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2009, 07:15 AM
  5. Java Question
    By Jay-1.1 in forum New To Java
    Replies: 11
    Last Post: 05-01-2008, 04:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •