| | |
simple java applet won't run on eclipse
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 10
Reputation:
Solved Threads: 0
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. 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?
Java Syntax (Toggle Plain Text)
import java.applet.Applet; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Rectangle; import javax.swing.JOptionPane; public class ColorSelect extends Applet { public void init() { String input; // ask the user for red, green, blue values input = JOptionPane.showInputDialog("red:"); red = Float.parseFloat(input); input = JOptionPane.showInputDialog("green:"); green = Float.parseFloat(input); input = JOptionPane.showInputDialog("blue:"); blue = Float.parseFloat(input); } public void paint(Graphics g) { final int SQUARE_LENGTH = 100; Graphics2D g2 = (Graphics2D)g; // select color into graphics context Color fillColor = new Color(red, green, blue); g2.setColor(fillColor); // construct and fill a square whose center is // the center of the window Rectangle square = new Rectangle( (getWidth() - SQUARE_LENGTH) / 2, (getHeight() - SQUARE_LENGTH) / 2, SQUARE_LENGTH, SQUARE_LENGTH); g2.fill(square); } private float red; private float green; private float blue; }
Last edited by johndb; Jun 24th, 2009 at 8:00 am.
•
•
Join Date: Jun 2009
Posts: 10
Reputation:
Solved Threads: 0
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."
•
•
Join Date: Jun 2009
Posts: 10
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Jan 2008
Posts: 3,827
Reputation:
Solved Threads: 501
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.
![]() |
Similar Threads
- Best compiler to use java applet on online games? (Java)
- Java Applet HTML Issues (Java)
- Can I run an ASP file from a Java Applet Button? (Java)
- Java Applet [Move Image] (Java)
- help!! My applet can't run... Urgent!! (Java)
- where to get the java applet? (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: Logging in to a website
- Next Thread: Reading File Runtime Error
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code compile component consumer database desktop developmenthelp eclipse error event exception fractal freeze ftp game givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online page print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing template textfields threads time title tree tutorial-sample update windows working






