Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-25-2007, 06:33 AM
Member
 
Join Date: Nov 2007
Posts: 4
ChrisC is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-25-2007, 06:52 AM
Member
 
Join Date: Nov 2007
Posts: 14
Zensai is on a distinguished road
Example 8.12 Bouncing Ball Applet with Controls
this is what you want?
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
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; protected DoubleBufferHandler dbhandler; }
?


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 07:04 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-25-2007, 07:15 AM
Member
 
Join Date: Nov 2007
Posts: 4
ChrisC is on a distinguished road
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 07:18 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-25-2007, 07:59 AM
Member
 
Join Date: Nov 2007
Posts: 14
Zensai is on a distinguished road
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 08:05 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-25-2007, 11:34 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
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.
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 } } } }
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(); } }
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(); } } }
Code:
<html> <body> <applet code="BouncingBall3.class" width="300" height="300"> <param name="delay" value="75"> </applet> </body> </html>
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-27-2007, 05:35 AM
Member
 
Join Date: Nov 2007
Posts: 4
ChrisC is on a distinguished road
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 05:38 AM.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-27-2007, 05:57 AM
Member
 
Join Date: Nov 2007
Posts: 14
Zensai is on a distinguished road
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 06:12 AM.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-27-2007, 06:36 AM
Member
 
Join Date: Nov 2007
Posts: 4
ChrisC is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Removal of Homework Requests CaptainMorgan Suggestions & Feedback 14 08-03-2008 10:21 PM
Problem using thread +rmi in my homework IbrahimAbbas Threads and Synchronization 10 04-14-2008 10:24 PM
Tough Homework Questions, PLEASE HELP! passage New To Java 21 01-17-2008 12:04 AM
Help with my java servlet homework jellyfish888 Java Servlet 2 12-21-2007 06:41 PM
Prerequisite advice needed Hatrabbit New To Java 2 11-30-2007 05:38 PM


All times are GMT +3. The time now is 03:46 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org