i need to Pass in three integers corresponding to an RGB value and use the appropriate Color constructor to set the background color.

import java.applet.*;
import java.awt.*;

public class AnAppletSubclass2b extends Applet {
	int r;
	int g;
	int b;
	//Color color = new Color (r,g,b);
	public void init()
	{
		String parmStringRED =  getParameter("red");
		 r = Integer.parseInt(parmStringRED);
		String parmStringGREEN =  getParameter("green");
		 g = Integer.parseInt(parmStringGREEN);
		String parmStringBLUE =  getParameter("blue");
		 b = Integer.parseInt(parmStringBLUE);
		Color color = new Color (r,g,b);
		System.err.println("The parameter are: red: " + parmStringRED + ", green: " + parmStringGREEN + ", blue: " + parmStringBLUE);

		setBackground(color);
	}

	public void paint(Graphics g) {
		System.out.println("In paint: n = " + n);
		n++;
	}

	int n;
}
<HTML>
	<HEAD>
		<TITLE> AnApplet With Parms </TITLE>
	</HEAD>
<BODY>

	<p>Here is the output of my  Program:</p>
	<APPLET CODE="AnAppletSubclass2b.class" WIDTH=150 HEIGHT=100>
		<PARAM name="red" value="25" >
		<PARAM name="green" value="250">
		<PARAM name="blue" value="250">


	</APPLET>
</BODY>
</HTML>

Dani AI

Generated

Nice, the applet approach works — a few small changes will make it safer and easier to maintain.

As already did the parameter parsing, add a robust parser that (1) tolerates missing or malformed params, (2) optionally accepts decimal input and rounds it, and (3) clamps values to 0-255 so the Color constructor never throws an exception. Rename the fields from r, g, b to red, green, blue to avoid shadowing the Graphics g parameter in paint. As suggested, validation is important; ’s point about alternate input (runtime UI) is valid if parameters are not ideal for the use case.

Example helper to parse-and-clamp (place in the applet class):

private int parseColorParam(String name, int defaultValue) {
    String s = getParameter(name);
    if (s == null) return defaultValue;
    try {
        int v;
        if (s.indexOf('.') >= 0) {
            v = (int)Math.round(Double.parseDouble(s));
        } else {
            v = Integer.parseInt(s);
        }
        if (v < 0) v = 0;
        if (v > 255) v = 255;
        return v;
    } catch (NumberFormatException ex) {
        System.err.println("Bad param " + name + ": " + s + " - using " + defaultValue);
        return defaultValue;
    }
}

Use it in init and then call setBackground(new Color(red, green, blue)). For paint, avoid console prints for UI output and avoid the g name clash; draw strings via the Graphics object instead:

public void paint(Graphics gfx) {
    gfx.setColor(Color.BLACK);
    gfx.drawString("In paint: n = " + n, 10, 20);
    n++;
}

Finally, note that Java applets are effectively obsolete in modern browsers. For long-term projects consider a Swing/JavaFX UI or a small web front end; for quick testing, use appletviewer or run the code as a standalone frame.

Recommended Answers

All 5 Replies

why do u think this is not the right way? it works doesnt it??
and btw, whats with that paint method() ???

The OP posted one earlier with the same code.

Anyway, I think I got what the OP wants. He/she wants to add the part that the program asks user to enter a color value. Then apply the color to the applet background... The code is on the right track, but you just simply need to add the input part and verify the value before applying the value...

thank you

The OP posted one earlier with the same code.

Anyway, I think I got what the OP wants. He/she wants to add the part that the program asks user to enter a color value. Then apply the color to the applet background... The code is on the right track, but you just simply need to add the input part and verify the value before applying the value...

Ohh I get it...thx :)

@OP please write what you want clearly..

You should probably add a small check to make sure the integers added don't fall out of the 0-255 range. Maybe cast them as integer as well in case the integer's input is as a decimal.

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.