simple java applet won't run on eclipse

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 10
Reputation: johndb is an unknown quantity at this point 
Solved Threads: 0
johndb johndb is offline Offline
Newbie Poster

simple java applet won't run on eclipse

 
0
  #1
Jun 24th, 2009
Hello I am not an experienced programmer, and I entered some code from a book I am following, to see if it would work but unfortunately I have run into some difficulty. This program prompts the user for the red, green, and blue values, and then fills a rectangle with the color that the user specified.
  1. import java.applet.Applet;
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Graphics2D;
  5. import java.awt.Rectangle;
  6. import javax.swing.JOptionPane;
  7.  
  8. public class ColorSelect extends Applet
  9. { public void init()
  10. { String input;
  11.  
  12. // ask the user for red, green, blue values
  13.  
  14. input = JOptionPane.showInputDialog("red:");
  15. red = Float.parseFloat(input);
  16.  
  17. input = JOptionPane.showInputDialog("green:");
  18. green = Float.parseFloat(input);
  19.  
  20. input = JOptionPane.showInputDialog("blue:");
  21. blue = Float.parseFloat(input);
  22. }
  23.  
  24. public void paint(Graphics g)
  25. { final int SQUARE_LENGTH = 100;
  26.  
  27. Graphics2D g2 = (Graphics2D)g;
  28.  
  29. // select color into graphics context
  30.  
  31. Color fillColor = new Color(red, green, blue);
  32. g2.setColor(fillColor);
  33.  
  34. // construct and fill a square whose center is
  35. // the center of the window
  36.  
  37. Rectangle square = new Rectangle(
  38. (getWidth() - SQUARE_LENGTH) / 2,
  39. (getHeight() - SQUARE_LENGTH) / 2,
  40. SQUARE_LENGTH,
  41. SQUARE_LENGTH);
  42.  
  43. g2.fill(square);
  44. }
  45.  
  46. private float red;
  47. private float green;
  48. private float blue;
  49. }
The code is exactly right as it is in the book but when I go to run as, there is no option to run as a java applet, and when I just select run, a window pops up and says "Select what to run: Ant build or Ant build..." If I select either the build fails and it say it is unable to find an Ant file to run. I haven't come across this kind of error before. Does anyone know of a solution?
Last edited by johndb; Jun 24th, 2009 at 8:00 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 975
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: simple java applet won't run on eclipse

 
0
  #2
Jun 24th, 2009
Works just fine for me. Run menu, run as..., java applet.
Eclipse 3.4.2, Java 1.6.14
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 10
Reputation: johndb is an unknown quantity at this point 
Solved Threads: 0
johndb johndb is offline Offline
Newbie Poster

Re: simple java applet won't run on eclipse

 
0
  #3
Jun 24th, 2009
Maybe my version of eclipse 3.4.2 has a bug in it then. I have tried on NetBeans IDE 6.5.1 and it tells me "class ColorSelect is public, should be declared in a file named ColorSelect.java" but I have named the file and project by that, and if I try to run it I get an error "Main Class wasn't found in ColorSelect.java project."
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 975
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: simple java applet won't run on eclipse

 
0
  #4
Jun 24th, 2009
Probability of it being an Eclipse bug is negligible. Problem is in project setup - wrong file names, wrong options etc. Start again from scratch and do it step by step according to the book. It will work if you get it right.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 10
Reputation: johndb is an unknown quantity at this point 
Solved Threads: 0
johndb johndb is offline Offline
Newbie Poster

Re: simple java applet trouble on IDEs

 
0
  #5
Jun 24th, 2009
Okay you were right about Eclipse, when I set the "Use project folder as root for the sources and class files" option on it worked. Unfortunately it didn't work for me on NetBeans and it doesn't have that exact same option, but I still created the main class with the same name as the project and it doesn't work. Both when the name of the file and project are the same as the class and when I do a default main method it tells me Main Class not found.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 975
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: simple java applet won't run on eclipse

 
0
  #6
Jun 24th, 2009
The file name MUST match the public class name, but the project name can be anything you want. I only use Eclipse, so I can't help with Netbeans. Sorry.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,827
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is online now Online
Senior Poster

Re: simple java applet won't run on eclipse

 
0
  #7
Jun 24th, 2009
Are you using NetBeans or Eclipse? In NetBeans, create a new project, category = "Java", Project = "Java Class Library", then add a new class to the Default Package called "ColorSelect". Copy and paste your code into that file. Do a "Clean and Build" on the project, then go into the "Project" tab, navigate to the ColorSelect class, then right-click "Run file" and the applet should run.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 10
Reputation: johndb is an unknown quantity at this point 
Solved Threads: 0
johndb johndb is offline Offline
Newbie Poster

Re: simple java applet won't run on eclipse

 
0
  #8
Jun 24th, 2009
It works now on Eclipse and NetBeans, I thank both of you for your help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC