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?