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 16 of 16

Thread: Java Program - Graphics - Ball Drop and Retrieve

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Java Program - Graphics - Ball Drop and Retrieve

    I am trying to make a display window with three buttons, "Drop", "Retrieve", and "Quit". When "Drop" is clicked, a ball must drop from the top of the screen and stop at the bottom. When "Retrieve" is clicked, a line must ascend from the top of the screen to retrieve the ball, and bring the ball back to the top of the screen. "Quit" should do the obvious. I was given this to start with:

    /**
     * A class that puts a graphics window on your display
     */
     
    import java.awt.*;
    import javax.swing.*;
     
    public class DisplayWindow extends JFrame {
     /**
      * Content pane that will hold the added Jpanel
      */
     private Container c;
     
     /**
      * DisplayWindow constructor - no parameters
      */
     public DisplayWindow() {
      super("Display");
      c = this.getContentPane();
     }
     
     /**
      * Adds panel to content pane
      * 
      * @parameter the panel to be added
      */
     public void addPanel(JPanel p) {
      c.add(p);
     }
     
     /**
      * consolidates the frame, makes it visible, causes program termination when
      * window is closed manually
      */
     public void showFrame() {
      this.pack();
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
    }

    public class DropDriver {
     
     public static void main(String[] args) {
      DisplayWindow d = new DisplayWindow();
      DropPanel b = new DropPanel();
      d.addPanel(b);
      d.showFrame();
     }
    }

    And I must create the DropPanel class. This is what I have so far:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class DropPanel extends JPanel implements ActionListener{
      int width = 300;
      int height = 600;
      Timer ticker = new Timer(20, this);
      int x = 0;
      int y = 0;
    }
    public Drop(){
      set preferred size(new Dimension (width,height));
      ticker.start();
    }
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      g.drawOval(x,y,50);
    }
    public void actionPerformed(ActionEvent e){
      if(e.getSource() == ticker){
        y = y-2;
      }
      repaint();
      }
    }

    any suggestions on where to go from here? I'm having a hard time.


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

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    What have you gotten to work so far? What step are you currently working on?
    Do you have any specific question about the step you are working on now?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    Well, right now my part of the program is pretty much all compilation errors. I have yet to even add the part where the ball is retrieved, nor have I added any buttons. I am mostly concerned with the ball dropping portion. I am getting the following errors:

    9 errors found:
    File: C:\Users\Edward\DropPanel.java [line: 12]
    Error: C:\Users\Edward\DropPanel.java:12: class, interface, or enum expected
    File: C:\Users\Edward\DropPanel.java [line: 14]
    Error: C:\Users\Edward\DropPanel.java:14: class, interface, or enum expected
    File: C:\Users\Edward\DropPanel.java [line: 15]
    Error: C:\Users\Edward\DropPanel.java:15: class, interface, or enum expected
    File: C:\Users\Edward\DropPanel.java [line: 16]
    Error: C:\Users\Edward\DropPanel.java:16: class, interface, or enum expected
    File: C:\Users\Edward\DropPanel.java [line: 18]
    Error: C:\Users\Edward\DropPanel.java:18: class, interface, or enum expected
    File: C:\Users\Edward\DropPanel.java [line: 19]
    Error: C:\Users\Edward\DropPanel.java:19: class, interface, or enum expected
    File: C:\Users\Edward\DropPanel.java [line: 20]
    Error: C:\Users\Edward\DropPanel.java:20: class, interface, or enum expected
    File: C:\Users\Edward\DropPanel.java [line: 23]
    Error: C:\Users\Edward\DropPanel.java:23: class, interface, or enum expected
    File: C:\Users\Edward\DropPanel.java [line: 25]
    Error: C:\Users\Edward\DropPanel.java:25: class, interface, or enum expected

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

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    Looks like the {}s are not properly placed. Make sure the } that ends the class is at the end of the class's definition.
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    kbrady481 (May 1st, 2013)

  6. #5
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    OK thanks! I've updated my code, an attempted to add some buttons...

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
     
    public class DropPanel extends JPanel implements ActionListener{
     
      JButton drop = new JButton("Drop");
      JButton retrieve = new JButton("Retrieve");
      int width = 300;
      int height = 600;
      Timer ticker = new Timer(20, this);
      int x = 0;
      int y = 200;
     
    public DropPanel(){
      setPreferredSize(new Dimension (width,height));
      this.add(drop);
      this.add(retrieve);
      drop.addActionListener(this);
      retrieve.addActionListener(this);
      ticker.start();
    }
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      g.setColor(Color.green);
      g.fillOval(x,y,50,50);
    }
    public void actionPerformed(ActionEvent e){
      if(e.getSource() == ticker){
        y = y+2;
      }
      repaint();
      }
    }

    Everything compiles nicely, but when I try to run the program I get an error saying that there is no static void main method to accept the string. But I was pretty sure I didn't need a p.s.v.m.(s[]a) line in this program.

    --- Update ---

    There is a static void main method in the driver already

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    get an error saying that there is no static void main method
    What class are you using with the java command? Does that class have a proper main() method?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    Yes there are three classes in total for this program:

    /**
     * A class that puts a graphics window on your display
     */
     
    import java.awt.*;
    import javax.swing.*;
     
    public class DisplayWindow extends JFrame {
     /**
      * Content pane that will hold the added Jpanel
      */
     private Container c;
     
     /**
      * DisplayWindow constructor - no parameters
      */
     public DisplayWindow() {
      super("Display");
      c = this.getContentPane();
     }
     
     /**
      * Adds panel to content pane
      * 
      * @parameter the panel to be added
      */
     public void addPanel(JPanel p) {
      c.add(p);
     }
     
     /**
      * consolidates the frame, makes it visible, causes program termination when
      * window is closed manually
      */
     public void showFrame() {
      this.pack();
      this.setVisible(true);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
    }

    public class DropDriver {
     
     public static void main(String[] args) {
      DisplayWindow d = new DisplayWindow();
      DropPanel b = new DropPanel();
      d.addPanel(b);
      d.showFrame();
     }
    }

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
     
    public class DropPanel extends JPanel implements ActionListener{
     
      JButton drop = new JButton("Drop");
      JButton retrieve = new JButton("Retrieve");
      int width = 300;
      int height = 600;
      Timer ticker = new Timer(20, this);
      int x = 0;
      int y = 200;
     
    public DropPanel(){
      setPreferredSize(new Dimension (width,height));
      this.add(drop);
      this.add(retrieve);
      drop.addActionListener(this);
      retrieve.addActionListener(this);
      ticker.start();
    }
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      g.setColor(Color.green);
      g.fillOval(x,y,50,50);
    }
    public void actionPerformed(ActionEvent e){
      if(e.getSource() == ticker){
        y = y+2;
      }
      repaint();
      }
    }


    --- Update ---

    The first two were given, the third is the one that I have created so far.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    What class are you using with the java command when you try to execute the program?
    Does that class have a proper main() method?
    If you don't understand my answer, don't ignore it, ask a question.

  10. The Following User Says Thank You to Norm For This Useful Post:

    kbrady481 (May 1st, 2013)

  11. #9
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    Omg exciting! I was running the DropPanel but was supposed to be running DropDriver. So, when I run DropDriver, A window pops up with my two buttons, and a green ball starts dropping from about the middle of the window before I even click a button. Then, it doesn't stop and rest on the button of the window like it should, but rather moves right off the page.

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    Sounds like you're making progress.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #11
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    Changed some things. Now, when I run the program, the window pops up with two buttons and a green ball at the top of the screen. When I click "Drop", the ball only drops 20 pixels, but does not continue to drop. I am going to try and figure out how to get the timer to work.

    --- Update ---

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
     
    public class DropPanel extends JPanel implements ActionListener{
     
      JButton drop = new JButton("Drop");
      JButton retrieve = new JButton("Retrieve");
      int width = 300;
      int height = 600;
      Timer ticker = new Timer(20, this);
      int x = 0;
      int y = 0;
     
    public DropPanel(){
      setPreferredSize(new Dimension (width,height));
      this.add(drop);
      drop.addActionListener(this);
      ticker.start();
      this.add(retrieve);
      retrieve.addActionListener(this);
      ticker.start();
    }
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      g.setColor(Color.green);
      g.fillOval(x,y,50,50);
    }
    public void actionPerformed(ActionEvent e){
      if(e.getSource() == drop){
        y = y+2;
      }
      repaint();
      }
    }


    --- Update ---

    I can't figure out how to implement a ticker so that when the buttons are clicked, that action happens. I think I need to use a boolean, but I don't know how.

    --- Update ---

    I have this much so far:

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
     
    public class DropPanel extends JPanel implements ActionListener{
     
     private boolean clicked=true;
     
      JButton drop = new JButton("Drop");
      JButton retrieve = new JButton("Retrieve");
      int width = 300;
      int height = 600;
      private Timer ticker = new Timer(20, this);
      int x = 0;
      int y = 0;
     
    public DropPanel(){
      setPreferredSize(new Dimension (width,height));
      this.add(drop);
      ticker.start();
      drop.addActionListener(this);
      this.add(retrieve);
      retrieve.addActionListener(this);
    }
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      g.setColor(Color.green);
      g.fillOval(x,y,50,50);
      g.drawLine(25,y,25,0);
    }
    public void actionPerformed(ActionEvent e){
      if(e.getSource() == ticker){
        ticker.start();
        y += 2;
      }
      repaint();
      }
    }

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    when the buttons are clicked, that action happens.
    If the "action" is controlled by the timer, start the timer when you want there to be action.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #13
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    I updated my code to this and it still isn't doing what it's supposed to

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
     
    public class DropPanel extends JPanel implements ActionListener{
     
     private boolean clicked=true;
     
      JButton drop = new JButton("Drop");
      JButton retrieve = new JButton("Retrieve");
      JButton quit = new JButton("Quit");
      int width = 300;
      int height = 600;
      Timer ticker = new Timer(20, this);
      int x = 0;
      int y = 0;
      int z = 0;
     
    public DropPanel(){
      setPreferredSize(new Dimension (width,height));
      this.add(drop);
      drop.addActionListener(this);
      this.add(retrieve);
      retrieve.addActionListener(this);
      this.add(quit);
      quit.addActionListener(this);
    }
    public void paintComponent(Graphics g){
      super.paintComponent(g);
      g.setColor(Color.green);
      g.fillOval(x,y,50,50);
      g.drawLine(25,z,25,0);
    }
    public void actionPerformed(ActionEvent e){
      if(e.getSource() == drop){
        ticker.start();
        y += 2;
        repaint();
      }
      if(e.getSource() == retrieve){
        ticker.start();
        z += 2;
      }
      }
    }

  16. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    isn't doing what it's supposed
    Please explain what the code is supposed to do. What is it doing now that is wrong? Where does the execution flow go now as events like button presses are made and the timer is started?

    You should make a design/list of steps the code is supposed to do BEFORE trying to write any code.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #15
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    I have already outlined earlier in this forum what my code is supposed to do. It is supposed to display a window with three buttons, "Drop", "Retrieve", and "Quit", and a ball. My code accomplished these things. However, when "Drop" is clicked, the ball is supposed to drop to the bottom of the window, and sit there. When I click my "Drop" button, however, the ball only moves down a tiny bit. It moves further with each click. When "Retrieve" is clicked, a string is supposed to drop down to the ball, and then the ball and the string are supposed to move back up to the top of the page, as if the string has descended to retrieve the ball. However, nothing happens when I click the "Retrieve" button. In an earlier version of my code, when I replaced the statement if(e.getSource == drop) with if(e.getSource == ticker) , I would run the program, and the both the ball and the string would drp to the bottom of the page and keep moving off the page, without me clicking any buttons at all.

  18. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Program - Graphics - Ball Drop and Retrieve

    You're describing what you want the code to do. Now you need to make a list of the steps the code needs to do to make that happen. After the list of steps is made, then code them.

    What steps does the code need to do when the Drop button is pressed?

    When the timer starts what is the timer method code supposed to do?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 16
    Last Post: December 15th, 2012, 06:05 PM
  2. [SOLVED] create/drop FB table from Java App
    By beruska in forum JDBC & Databases
    Replies: 1
    Last Post: October 21st, 2011, 09:51 AM
  3. Bouncing Ball Program Random Color Change
    By coderEvolution in forum What's Wrong With My Code?
    Replies: 10
    Last Post: March 3rd, 2011, 04:01 PM
  4. OOP graphics program
    By conmanva in forum Object Oriented Programming
    Replies: 2
    Last Post: January 2nd, 2011, 04:20 PM
  5. Program that will retrieve information from website.
    By minaru in forum Java Theory & Questions
    Replies: 2
    Last Post: December 18th, 2010, 05:50 PM