Results 1 to 8 of 8
  1. #1
    ChrisC is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Unhappy Homework PREREQUISITE Problem

    Hello.

    I have a homework assignment that I am not asking you to help me with -- I only need help fixing the textbook example code that is supposed to be used as a starting point.

    To wit, we are working from the textbook seen at http://condor.depaul.edu/~xjia/ , and I am assigned to make modifications to an example applet (the Bouncing Ball example in Chapter 8, for what that's worth). The code as published in the book compiles but doesn't work, and the code as modified by our instructor (who seems to have encountered the problem himself, ahead of time) doesn't even compile. I don't know enough Java/Swing to fix it myself, and don't have enough time to spend getting the prerequisite together and then doing the assignment (I work 40 hours/week in addition to school). (I've spent too much time already, e-mailing my instructor, the textbook author, and a mailinglist of assorted friends, about this.) I am therefore appealing to anyone here who might be willing to take a look at the doggoned thing.

    Please look at http://www.cs.sunyit.edu/~chiesac1/emergency.html and see what you can tell me. Thanks.

  2. #2
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default

    Example 8.12 Bouncing Ball Applet with Controls
    this is what you want?
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    public class BouncingBall3 
            extends java.applet.Applet {
      public BouncingBall3() {
        setLayout(new BorderLayout());
        canvas = new BouncingBallCanvas();
        add("Center", canvas); 
        animator = new Animator(canvas); 
        Dimension d; 
        
        controlPanel = new Panel(); 
        controlPanel.setLayout(new GridLayout(1,0));     
        Button startButton = new Button("start");
        controlPanel.add(startButton);
        Button stopButton = new Button("stop");
        controlPanel.add(stopButton);
        Choice choice = new Choice();
        choice.addItem("red");
        choice.addItem("green");
        choice.addItem("blue");
        controlPanel.add(choice); 
        add("South", controlPanel); 
        
        startButton.addActionListener(new ButtonHandler(new StartCommand())); 
        stopButton.addActionListener(new ButtonHandler(new StopCommand())); 
        choice.addItemListener(new ColorChoiceHandler());
         
      }
       
      public void init() {
        String att = getParameter("delay");
        if (att != null) { 
          int delay = Integer.parseInt(att);
          animator.setDelay(delay); 
        }
        canvas.initCanvas(); 
      }
      public void start() {
        animator.start(); 
      }
      public void stop() {
        animator.stop(); 
      }
       
      protected BouncingBallCanvas canvas;
      protected Animator animator; 
      protected Panel controlPanel; 
      
      protected class ButtonHandler implements ActionListener {  
        private Command cmd; 
          
        public ButtonHandler(Command cmd) {
          this.cmd = cmd;     
        }
           
        public void actionPerformed(ActionEvent event) { 
          if (cmd != null) 
            cmd.execute(); 
        }
      }
      protected class StartCommand implements Command {
        public void execute() {
          start(); 
        }
      }
      protected class StopCommand implements Command {
        public void execute() {
          stop(); 
        }
      }
      protected class ColorChoiceHandler implements ItemListener {
        public void itemStateChanged(ItemEvent event)  {    
          Choice choice = (Choice) event.getSource();  
          if (choice != null) {
            if ("red".equals(event.getItem()))
              canvas.setBallColor(Color.red); 
            else if ("green".equals(event.getItem()))
              canvas.setBallColor(Color.green); 
            else if ("blue".equals(event.getItem()))
              canvas.setBallColor(Color.blue); 
            canvas.repaint();
          }
        }
      }
       
    }
    or this
    Java Code:
    import java.awt.*;
    
    public class BouncingBallCanvas 
        extends Canvas implements DoubleBufferedComponent {
    
      public BouncingBallCanvas() {
        dbhandler = new DoubleBufferHandler(this); 
      }
    
      public void initCanvas() {
        d = getSize(); 
        x = d.width * 2 / 3 ;
        y = d.height - radius;
      }
    
      public void update(Graphics g) {
        dbhandler.update(g); 
      } 
    
      public void paint(Graphics g) {
        update(g); 
      } 
    
      public void paintFrame(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, d.width, d.height); 
        if (x < radius || x > d.width - radius)
          dx = -dx; 
        if (y < radius || y > d.height - radius)
          dy = -dy; 
        x += dx; y += dy; 
        g.setColor(ballcolor);
        g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
      } 
    
      public void setBallColor(Color c) {
        ballcolor = c; 
      }
    
      public void setBallPosition(int x, int y) {
        this.x = x; this.y = y; 
      }
    
      protected int x, y, dx = -2, dy = -4, radius = 20; 
      protected Color ballcolor = Color.red;
      protected Dimension d;  
      [B][COLOR="RoyalBlue"]protected DoubleBufferHandler dbhandler; [/COLOR][/B]
    }
    ?


    I've spent too much time already, e-mailing my instructor, the textbook author: Hmm.. when you got problems with your windows you email Bill Gates? lolllllllllllllllllllllllllllllllllllllllllllllll
    Last edited by Zensai; 11-25-2007 at 06:04 AM.

  3. #3
    ChrisC is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    Thank you, Zensai, for replying so quickly. I appreciate that!

    First, I don't quite understand why you are asking me whether I want [file 1] "or" [file 2]. Should I try using them together or separately?

    Second, I tried compiling using both together, and found that classes DoubleBufferHandler and Command are unrecognized here -- so I can't say that your solution works just yet.

    Third, a friend from that mailinglist I mentioned says that when he views my applet from his Macbook (with Java version 1.5.0_07) it behaves as it should: he sees a red bouncing ball! I am appalled...

    Fourth, yes, if Bill Gates had a personal webpage listing his own creations, with Errata pages for them, and I found an Erratum in one of said creations, I sure would write to him! The two situations as they stand aren't quite comparable. :-)

    Thanks,

    Chris:( :( :(
    Last edited by ChrisC; 11-25-2007 at 06:18 AM.

  4. #4
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default

    they are both in chapter 8. I dunno which one you need.
    you are using together for eclipse 2006 and you chose when you created the project this: JDK compiler compliance 5.0? (because the default is 1.4!)

    i dont care about macs :P


    if Bill Gates had a personal webpage.. you are so romantic ;)

    Anyway i dont think Jia has time to see yours corrigenda

    P.S. and why you are working 40h/week? is it legal in the States? lol
    Last edited by Zensai; 11-25-2007 at 07:05 AM.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    These run okay although I have not confirmed that it is reading the parameter from the applet tag. I tried to make minimal changes to the code you linked to.
    Java Code:
    //  <applet code="BouncingBall3" width="300" height="300"></applet>
    //  use: >appletviewer BouncingBall3.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class BouncingBall3 extends JApplet {
        protected BouncingBallCanvas canvas;
        protected Animator animator;
    
        public void init() {
            canvas = new BouncingBallCanvas();
            animator = new Animator(canvas);
            String att = getParameter("delay");
            if (att != null) {
                int delay = Integer.parseInt(att);
                animator.setDelay(delay);
            }
            Container cp = getContentPane();
            cp.setLayout(new BorderLayout());
            cp.add("Center", canvas);
            Dimension d;
    
            JPanel controlPanel = new JPanel();
            controlPanel.setLayout(new GridLayout(1,0));
            JButton startButton = new JButton("start");
            controlPanel.add(startButton);
            JButton stopButton = new JButton("stop");
            controlPanel.add(stopButton);
            JComboBox choice = new JComboBox();
            choice.addItem("red");
            choice.addItem("green");
            choice.addItem("blue");
            controlPanel.add(choice);
            cp.add("South", controlPanel);
    
            startButton.addActionListener(
                    new ButtonHandler(ButtonHandler.START_ANIMATION));
            stopButton.addActionListener(
                    new ButtonHandler(ButtonHandler.STOP_ANIMATION));
            choice.addItemListener(new ColorChoiceHandler());
        }
    
        public void start() {
            animator.start();
        }
    
        public void stop() {
            animator.stop();
        }
    
        protected class ButtonHandler implements ActionListener {
            static final int START_ANIMATION = 1;
            static final int STOP_ANIMATION = 2;
            protected int cmd;
    
            public ButtonHandler(int cmd) {
                this.cmd = cmd;
            }
    
            public void actionPerformed(ActionEvent event) {
                switch (cmd) {
                    case START_ANIMATION: start(); break;
                    case STOP_ANIMATION: stop(); break;
                }
            }
        }
    
        protected class ColorChoiceHandler implements ItemListener {
            public void itemStateChanged(ItemEvent event) {
                JComboBox choice = (JComboBox) event.getSource();
                if (choice != null) {
                    if ("red".equals(event.getItem()))
                        canvas.setBallColor(Color.red);
                    if ("green".equals(event.getItem()))
                        canvas.setBallColor(Color.green);
                    if ("blue".equals(event.getItem()))
                        canvas.setBallColor(Color.blue);
                    canvas.repaint();  // component method
                }
            }
        }
    }
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class BouncingBallCanvas extends JPanel {
        protected int x, y, dx = -2, dy = -4, radius = 20;
        protected Color ballcolor = Color.red;
        protected Dimension d;
        
        public BouncingBallCanvas() {
            super(true);  // double-buffered
        }
        
        public void paintComponent(Graphics g) {
            if(d == null ) {
                d = getSize();
                x = d.width * 2 / 3;
                y = d.height/2 - radius/2;
            }
            ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                             RenderingHints.VALUE_ANTIALIAS_ON);
            g.setColor(Color.white);
            g.fillRect(0, 0, d.width, d.height);
            g.setColor(ballcolor);
            g.fillOval(x - radius, y - radius, radius * 2, radius * 2);
        }
    
        public void setBallColor(Color c) {
            ballcolor = c;
        }
    
        public void advance() {
            if (x + dx < radius || x + radius + dx > d.width)
                dx = -dx;
            if (y + dy < radius|| y + radius + dy > d.height)
                dy = -dy;
            x += dx;
            y += dy;
            repaint();
        }
    }
    Java Code:
    public class Animator implements Runnable {
        protected BouncingBallCanvas comp;
        protected int delay = 100;
        protected Thread animationThread;
        boolean running = false;
    
        public Animator(BouncingBallCanvas comp) {
            this.comp = comp;
        }
    
        final public void setDelay(int delay) {
            this.delay = delay;
        }
    
        final public int getDelay() {
            return delay;
        }
    
        public void start() {
            if(!running) {
                running = true;
                animationThread = new Thread(this);
                animationThread.setPriority(Thread.NORM_PRIORITY);
                animationThread.start();
            }
        }
    
        public void stop() {
            running = false;
            if(animationThread != null)
                animationThread.interrupt();
            animationThread = null;
        }
    
        public void run() {
            while (running) {
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException e) {}
                comp.advance();
            }
        }
    }
    Java Code:
    <html>
    <body>
    <applet code="BouncingBall3.class"
            width="300" height="300">
        <param name="delay" value="75">
    </applet>
    </body>
    </html>

  6. #6
    ChrisC is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Smile Cancel Red Alert!

    Thank you again, Zensai, and thank you, Hardwired.

    First, I need to announce that I received a corrected, working version from my instructor in this morning's e-mail. Yay! :) Interestingly, he added the same two new classes you (Zensai) did, so I assume they are "standard" things that come from somewhere -- but when I asked him, he didn't seem to know.

    Second -- Zensai, I didn't realize you also had the book. That 'splains things! I still couldn't have told you "which one" I wanted, but now at least I understand what you were asking, and why.

    Third - Hardwired, thank you, I will try yours too, and I'm sure it will be educational to compare the now three corrected versions I have so gratefully received.

    Fourth - Romantic? Me? Don't tell/ask my wife! ;)

    Fifth: 40h/week legal? Heck yeah. Some people in this country work 60-80 h/week in pursuit of the Almighty Dollar (well, not so almighty these days, eh? :( ) And I only get three weeks' vacation a year, and had to fight for that (they wanted to give me two weeks)... *sigh*
    Last edited by ChrisC; 11-27-2007 at 04:38 AM.

  7. #7
    Zensai is offline Member
    Join Date
    Nov 2007
    Posts
    14
    Rep Power
    0

    Default

    Jia is a very famous prof. and btw the fall of the dollar is technical (though huge :) $1.487=€1). dont worry. america still rulez...just dont come in europe for the next 2-3 years cuz it gonna be too expensive for you :P

    P.S. next time you create a new thread dont put words like help, homework, emergency, cuz other persons that have the same queries cant find your thread.
    Last edited by Zensai; 11-27-2007 at 05:12 AM.

  8. #8
    ChrisC is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Smile More

    I didn't know Jia was famous... as far as I would know, he was just another prof who wrote a book and used it in all the classes he himself taught. I had profs like that back in my undergrad days (the 80s) at a big-name school that shall, uh, remain nameless...

    That being said, I find Jia'this book to be pretty good, the occasional minor erratum aside (and all books have those). It certainly beats all heck out of the first Java book I read, back when I was trying to teach myself the language. I learned more from the first chapter of Jia than from the entirety of that other book.

    The glitch with this bouncing ball business seems to just possibly have something to do with Java versions; everything I've been doing/using has been with Java 6, whereas the ONE person I know who looked at it with Java 5 saw the bouncing ball right from the start.

    Thanks for the "warning" about Europe. Now I am starting to "get" why an old high-school friend of mine went to London years ago and never came back. :)

    Oh, and thanks for the tip about thread titles. I can't imagine what else I could have titled this one, though... Ah well. With luck I won't have to pester y'all too often. :)

Similar Threads

  1. Removal of Homework Requests
    By CaptainMorgan in forum Suggestions & Feedback
    Replies: 14
    Last Post: 08-03-2008, 09:21 PM
  2. Problem using thread +rmi in my homework
    By IbrahimAbbas in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-14-2008, 09:24 PM
  3. Tough Homework Questions, PLEASE HELP!
    By passage in forum New To Java
    Replies: 21
    Last Post: 01-16-2008, 11:04 PM
  4. Help with my java servlet homework
    By jellyfish888 in forum Java Servlet
    Replies: 2
    Last Post: 12-21-2007, 05:41 PM
  5. Prerequisite advice needed
    By Hatrabbit in forum New To Java
    Replies: 2
    Last Post: 11-30-2007, 04:38 PM

Posting Permissions

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