Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Cookie Jar Program Issues

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Cookie Jar Program Issues

    I was assigned the task of creating a 'comprehensive' java program for my programming class. Task; create a program where 2 people must take from same cookie jar. The jar will start with 12 cookies in the jar. Click a button for the specific person to take a cookie and place it on their plate. It also must display person1's total, person2's total, and total left in the jar.

    Keep in mind that this is only to use the most basic java skills (.awt, .swing, 2d java, etc). I was able to both draw and scenery and show a few JButtons/JTextAreas. My issue is assigning actionListeners to the buttons and getting the program functional. If anyone could help me I would be most obliged. Here is the code;


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class CookieJarJPanel extends JPanel
    {
     
    	public void paintComponent (Graphics g)
    	{
    		super.paintComponent (g);
     
    		Graphics2D g2d = (Graphics2D ) g;
     
    		g.setColor (new Color (169, 39, 19));
    		g.fillRect (0, 600, 900, 450);  //Table
     
    		//Jar
    		g.setColor(Color.BLACK);
    		g.drawOval (325, 300, 250, 50);
    		g.drawLine (325, 325, 375, 400);
    		g.drawLine (575, 325, 525, 400);
    		g.drawLine (375, 400, 325, 500);
    		g.drawLine (525, 400, 575, 500);
    		g.drawLine (325, 500, 375, 650);
    		g.drawLine (575, 500, 525, 650);
    		g.drawOval (375, 625, 150, 50);
    		g.drawArc (375, 375, 150, 50, 0, -180);
     
    		//Plates		
    		g.setColor (Color.WHITE);
    		g.fillOval (25, 625, 250, 100); //Left Plate
    		g.fillOval (625 ,625, 250, 100); //Right Plate
     
    		g.setColor (Color.BLACK);
    		g.drawOval (50, 640, 200, 65);
    		g.drawOval (650, 640, 200, 65);
     
    		//Cookies (12)
    		g.setColor (new Color(205, 133, 63));
    		g.fillOval (340, 500, 50, 50);
    		g.fillOval (350, 525, 50, 50);
    		g.fillOval (380, 550, 50, 50);
    		g.fillOval (405, 490, 50, 50);
    		g.fillOval (415, 520, 50, 50);
    		g.fillOval (370, 600, 50, 50);
    		g.fillOval (400, 615, 50, 50);
    		g.fillOval (450, 610, 50, 50);
    		g.fillOval (500, 525, 50, 50);
    		g.fillOval (450, 550, 50, 50);
    		g.fillOval (475, 575, 50, 50);
    		g.fillOval (400, 575, 50, 50);
     
     
    	}
     
    	ImageIcon icon1;
    	JTextArea text;
    	JCheckBox checkBox; 
    	JComboBox number;
    	Container container;
    	FlowLayout layout;
     
    	public CookieJarJPanel()
    	{
    		layout = new FlowLayout();
    		setLayout(layout);
     
    		JButton noranEat, johnEat;
    		JTextArea noranName, johnName, noranCount, johnCount, cookieTotal;
    		int noranCookieCount, johnCookieCount, cookieTotalCount;
    		noranCookieCount = 12;
    		johnCookieCount = 4;
    		cookieTotalCount = 2;
     
    		noranEat = new JButton ("Have Noran take a cookie");
    		johnEat = new JButton ("Have John take a cookie");
    		noranName = new JTextArea ("Noran's Amout: ");
    		johnName = new JTextArea ("John's Amount: ");
    		//noranCount = new JTextArea (noranCookieCount);
    		//johnCount = new JTextArea (johnCookieCount);
    		//cookieTotal = new JTextArea (cookieTotalCount);
     
    		noranEat.setBounds (25,25, 50, 50);
    		add(noranEat);
    		add(noranName);
    		add(johnEat);
    		add(johnName);
    		//add(noranCount);
    		//add(johnCount);
    		//add(cookieTotal);
    	}
     
    }








    and, of course, the Main;


    import java.awt.*;
    import javax.swing.*;
     
    public class CookieJar
    {
     
    	public static void main(String[] args)
    		{
    			JFrame frame = new JFrame ("Cookie Jar");
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    			CookieJarJPanel cookiejar = new CookieJarJPanel();
    			cookiejar.setBackground (Color.cyan);
    			frame.add (cookiejar);
    			frame.setSize(900, 900);
    			frame.setVisible(true);
    		}
    }


    Thanks in advance!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cookie Jar Program Issues

    assigning actionListeners to the buttons
    Can you explain the problems you are having?
    Have you seen this:
    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Cookie Jar Program Issues

    Well, drawing it, that's something I'd like to know too, but you can alter a cookie count, if that's what you're going for, by doing something like this:

     
    // Assume that there is already such fields as below declared already
     
    field = new JTextField();
    field.setText("5");
     
    addCookie = new JButton("Add a cookie");
     
    addCookie.addActionListener(new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
    int cookies = Integer.parseInt(field.getText());
     
    Integer added = cookies + 1;
     
    field.setText(added.toString());
     
    }});

    Wait, you're using JTextArea, though it should still be the same as both have a setText() and getText() method.

    Also, how would it know which cookie to take? Would it take from the top of the jar, from the bottom, or in the middle?

    As for these three lines, I think this might have been what you were trying to do:

       noranCount = new JTextArea (Integer.toString(noranCookieCount));
     
          johnCount = new JTextArea (Integer.toString(johnCookieCount));
          cookieTotal = new JTextArea (Integer.toString(cookieTotalCount));
    Last edited by javapenguin; May 15th, 2012 at 06:44 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cookie Jar Program Issues

    Please format your posted code correctly with proper indentations. It should not all start in the first column.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cookie Jar Program Issues

    Thanks for the replies, guys. Basically, it doesn't matter what cookie you take from the jar, as long as it disappears from the jar and appears on the corresponding plate. @Norm, basically my issues is I have this program setup to draw a table with two plates and a jar of 12 cookies. I don't know how to write the programming to 1) Insert buttons onto the page that say things like "Have Noran eat a cookie" and "Have John eat a cookie" AND have the buttons actually perform the command, not just sit there and look pretty. 2) How to show a JTextArea that says something like "Number of cookies left in jar: ", "Number of cookies Noran has: ", and "Number of cookies John has: ". It was suggested that I create an array for the cookies. Unfortunately, this isn't a question you can answer with a few lines of code (although that would at least point me in the right direction). In order to get this program working, it needs a decent amount of code to finish.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cookie Jar Program Issues

    don't know how to write the programming to 1) Insert buttons onto the page
    That's very basic programming: Create a panel and add the buttons to the panel and add the panel to the frame.
    Same for a text area. Add it to a panel that is showing in the frame.

    One problem with the drawing in the paintComponent method is that ALL the parameters to the draw methods are hardcoded numbers vs using variables that would let you change the drawn shapes location.

    My screen is 1024X600
    Last edited by Norm; May 16th, 2012 at 11:44 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Cookie Jar Program Issues

    You don't need an array for this, since you are merely counting. You should have 3 ints that are accessible by the entire CookieJarJPanel class. One int will represent the number of cookies in the jar, another int will represent the number of cookies Noran has, and the last int will represent the number of cookies John has. Every time Noran takes a cookie from the jar, you decrement the number of cookies in the jar and increment the number of cookies Noran has (do the same with John). Then, each time Noran eats a cookie, you just decrement the number of cookies he has (and do the same for John).

    javapenguin's post should give you an idea of how to add action listeners to your buttons. Inside the action listeners, you would do the sort of thing that I described above.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. #8
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cookie Jar Program Issues

    UPDATE: I have fixed all issues except for 1; I can't get the number of cookies to update when you click one of the buttons. Here is my updated code;

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class CookieJarJPanel extends JPanel
    {
     
    	public void paintComponent (Graphics g)
    	{
    		super.paintComponent (g);
     
    		Graphics2D g2d = (Graphics2D ) g;
     
    		g.setColor (new Color (169, 39, 19));
    		g.fillRect (0, 600, 900, 450);  //Table
     
    		//Jar
    		g.setColor(Color.BLACK);
    		g.drawOval (325, 300, 250, 50);
    		g.drawLine (325, 325, 375, 400);
    		g.drawLine (575, 325, 525, 400);
    		g.drawLine (375, 400, 325, 500);
    		g.drawLine (525, 400, 575, 500);
    		g.drawLine (325, 500, 375, 650);
    		g.drawLine (575, 500, 525, 650);
    		g.drawOval (375, 625, 150, 50);
    		g.drawArc (375, 375, 150, 50, 0, -180);
     
    		//Plates		
    		g.setColor (Color.WHITE);
    		g.fillOval (25, 625, 250, 100); //Left Plate
    		g.fillOval (625 ,625, 250, 100); //Right Plate
     
    		g.setColor (Color.BLACK);
    		g.drawOval (50, 640, 200, 65);
    		g.drawOval (650, 640, 200, 65);
     
    		//Cookies (12)
    		g.setColor (new Color(205, 133, 63));
    		g.fillOval (340, 500, 50, 50);
    		g.fillOval (350, 525, 50, 50);
    		g.fillOval (380, 550, 50, 50);
    		g.fillOval (405, 490, 50, 50);
    		g.fillOval (415, 520, 50, 50);
    		g.fillOval (370, 600, 50, 50);
    		g.fillOval (400, 615, 50, 50);
    		g.fillOval (450, 610, 50, 50);
    		g.fillOval (500, 525, 50, 50);
    		g.fillOval (450, 550, 50, 50);
    		g.fillOval (475, 575, 50, 50);
    		g.fillOval (400, 575, 50, 50);
     
     
    	}
     
    	ImageIcon icon1;
    	JTextArea text;
    	JCheckBox checkBox; 
    	JComboBox number;
    	Container container;
    	FlowLayout layout;
     
     
    	JButton noranEat, johnEat;
    		JTextArea noranName, johnName, noranCount, johnCount, cookieTotal;
    		double noranCookieCount = 0.0;
    		double johnCookieCount = 0.0;
    		double cookieTotalCount = 12.0;
     
     
    	public CookieJarJPanel()
    	{
    		layout = new FlowLayout();
    		setLayout(layout);
     
     
    		noranEat = new JButton ("Have Noran take a cookie");
    		johnEat = new JButton ("Have John take a cookie");
    		cookieTotal = new JTextArea ("Cookies in jar: " + cookieTotalCount);
    		noranName = new JTextArea ("Noran's Amout: " + noranCookieCount);
    		johnName = new JTextArea ("John's Amount: " + johnCookieCount);
     
     
    		noranEat.setBounds (25,25, 50, 50);
    		add(noranEat);
    		add(noranName);
    		add(cookieTotal);
    		add(johnEat);
    		add(johnName);
     
    		ButtonHandler handler = new ButtonHandler();
    		noranEat.setActionCommand("Noran");
    		noranEat.addActionListener(handler);
    		johnEat.setActionCommand("John");
    		johnEat.addActionListener(handler);	
     
    	}
     
     
    	public class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e) 
    		{
    			if(e.getActionCommand()== "Noran") 
    			{
    				updateCount1();
     
    			}else if (e.getActionCommand() == "John") 
    			{
    				updateCount2();
    			}
    		}
     
    		public void updateCount1()
    		{
    			double newCookieAmount = 2.0;
    			newCookieAmount = noranCookieCount + 1;
    			noranCookieCount = newCookieAmount;
    			cookieTotalCount = cookieTotalCount - 1;
    		}
     
    		public void updateCount2()
    		{
    			double newCookieAmount2 = 3.0;
    			newCookieAmount2 = johnCookieCount + 1;
    			johnCookieCount = newCookieAmount2;
    			cookieTotalCount = cookieTotalCount - 1;
     
    		}
     
    	}
     
     
    }

  9. #9
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Cookie Jar Program Issues

    You should compare Strings with the .equals method. The ActionEvent.getActionCommand() method returns a String. So, in your actionPerformed() method of your ButtonHandler, instead of saying: if(e.getActionCommand()== "Noran") , you should say: if(e.getActionCommand().equals("Noran")) .
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Cookie Jar Program Issues

    This is a lot of code to add 1 to a variable:
           double newCookieAmount2 = 3.0;
    	newCookieAmount2 = johnCookieCount + 1;
    	johnCookieCount = newCookieAmount2;
    vs
    	johnCookieCount++;  // add 1 to John's cookie count
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Cookie Jar Program Issues

    Quote Originally Posted by Norm View Post
    This is a lot of code to add 1 to a variable:
           double newCookieAmount2 = 3.0;
    	newCookieAmount2 = johnCookieCount + 1;
    	johnCookieCount = newCookieAmount2;
    vs
    	johnCookieCount++;  // add 1 to John's cookie count

    Good idea, my brain is mush right now and I missed that. Thanks guys, my code is fully functional! (other than removing the cookie and placing it on the plate) :-)
    Last edited by NoranPrease; May 16th, 2012 at 02:20 PM.

Similar Threads

  1. Jar Issues
    By DMinton in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 6th, 2012, 02:32 PM
  2. [SOLVED] Searching issues.
    By Saintroi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2012, 07:26 PM
  3. [SOLVED] Having Issues Wish this Program
    By userct in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 9th, 2012, 03:46 PM
  4. Address Book Program Issues
    By Gamb1t in forum What's Wrong With My Code?
    Replies: 208
    Last Post: August 25th, 2011, 08:43 PM
  5. Issues with ascending number program
    By newtojava2011 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 30th, 2011, 06:23 PM