Results 1 to 4 of 4
  1. #1
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default my code works fine but don't know if its the right way

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

    Java Code:
    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: " + parmStringRED + "," + parmStringGREEN + "," + parmStringBLUE);
    
    		setBackground(color);
    	}
    
    	public void paint(Graphics g) {
    		System.out.println("In paint: n = " + n);
    		n++;
    	}
    
    	int n;
    }
    Java Code:
    <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="120">
    		<PARAM name="blue" value="250">
    
    
    	</APPLET>
    </BODY>
    </HTML>

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: my code works fine but don't know if its the right way

    There is a lot of unnecessary work. For instance, you don't need to initialize the color variable at the class level, you can simply declare it since it's being initialized in the init method.

    Java Code:
    public class .... extends Applet{
      int r, g, b;
      Color color;
      ...
    }
    You also shouldn't print with an err printstream if it isn't actually an error. Since you aren't printing errors, change System.err.println to System.out.println. The rest seems fine.

  3. #3
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: my code works fine but don't know if its the right way

    Thank you !!!!!

  4. #4
    mike28 is offline Member
    Join Date
    Oct 2011
    Posts
    20
    Rep Power
    0

    Default Re: my code works fine but don't know if its the right way

    but if i want to call setBackground(color); from public void paint(Graphics g) it wont work for some reason

Similar Threads

  1. Replies: 1
    Last Post: 02-12-2011, 05:36 PM
  2. Code runs fine but get compile error
    By BillyB in forum New To Java
    Replies: 4
    Last Post: 11-29-2010, 02:18 PM
  3. Replies: 1
    Last Post: 11-03-2010, 08:49 AM
  4. Please explain how this bit of code works.
    By Allspark in forum New To Java
    Replies: 4
    Last Post: 09-03-2010, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •