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.

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;
}

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?

Works just fine for me. Run menu, run as..., java applet.
Eclipse 3.4.2, Java 1.6.14

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."

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.

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.

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.

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.

It works now on Eclipse and NetBeans, I thank both of you for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.