Results 1 to 16 of 16
Thread: Java Gave Question
- 09-12-2010, 06:06 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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); } }
- 09-12-2010, 07:10 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 17
- Rep Power
- 0
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); } }
- 09-12-2010, 07:24 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
Hey thanks,
I dont see anything different. Where is it printing the time and the counts of clicks??
- 09-12-2010, 07:27 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 17
- Rep Power
- 0
see in console that is command prompt you are running it will show the values.If you are running in eclipse see server console
- 09-12-2010, 07:27 AM #5
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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); } }
- 09-12-2010, 07:29 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 17
- Rep Power
- 0
just check a condition say if(n>15) print the values that's it
- 09-12-2010, 07:33 AM #7
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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
- 09-12-2010, 09:56 AM #8
Member
- Join Date
- Sep 2010
- Posts
- 17
- Rep Power
- 0
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); } }
- 09-12-2010, 10:21 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,401
- Blog Entries
- 7
- Rep Power
- 17
@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
- 09-12-2010, 11:44 AM #10
Member
- Join Date
- Sep 2010
- Posts
- 17
- Rep Power
- 0
- 09-12-2010, 03:21 PM #11
@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.
- 09-12-2010, 07:45 PM #12
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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.
- 09-12-2010, 09:54 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 60
- Rep Power
- 0
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
- 09-12-2010, 10:10 PM #14
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).
- 09-12-2010, 10:10 PM #15
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?
-
Yes, let's not split the discussion up. As the main question in this thread has been answered, let's lock this one and stick to the other thread.
Similar Threads
-
Need help with java question
By ccie007 in forum New To JavaReplies: 23Last Post: 05-18-2010, 06:32 PM -
Java Question :D
By thisisIT in forum New To JavaReplies: 6Last Post: 03-12-2010, 04:04 PM -
Java question
By TGH in forum New To JavaReplies: 12Last Post: 11-27-2009, 02:05 PM -
question about java rmi
By hakimade in forum Advanced JavaReplies: 1Last Post: 07-01-2009, 07:15 AM -
Java Question
By Jay-1.1 in forum New To JavaReplies: 11Last Post: 05-01-2008, 04:04 PM


LinkBack URL
About LinkBacks


Bookmarks