Results 1 to 3 of 3
  1. #1
    rjavaprodev is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Default Help needed debugging Random Color code for applet

    /**
    * @(#)Clown.java
    *
    * Clown Applet application
    *
    * @author
    * @version 1.00 2011/11/18
    */

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

    public class Clown extends Applet {

    public void init() {
    }

    public void paint(Graphics g) {

    setSize(800,600);

    int red1 = (int)(Math.random()*256);
    int green1 = (int)(Math.random()*256);
    int blue1 = (int)(Math.random()*256);

    Color randomColor = new Color(red1,green1,blue1);
    setBackground(randomColor);
    repaint();

    g.setColor(Color.black);
    g.fillOval(50,50,200,200);

    }
    }




    This is the code... I works fine BUT once it starts generating random colors it wont stop.
    Therefore the backround keeps flashing different colors.
    I used Jcreator for this!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help needed debugging Random Color code for applet

    I'm not sure I'd expect any different behavior, after all, you are calling repaint from within a paint method, right? Perhaps instead you want to use a timer such as a Swing Timer and limit the number of times it repeats via a counter variable?

  3. #3
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Help needed debugging Random Color code for applet

    In addition to that what Fubarable said(repaint() in paint() is bad and using of a timer!!!) setSize in repaint is also bad, because this also means that repaint is invoked again! The same with setBackground, why do you dont use the graphics object to paint the background? (and i would use swing instead of awt :D)
    Java Code:
    	public void paint(Graphics g) {
    		super.paint(g);
    		int red1 = (int) (Math.random() * 256);
    		int green1 = (int) (Math.random() * 256);
    		int blue1 = (int) (Math.random() * 256);
    
    		Color randomColor = new Color(red1, green1, blue1);
    		g.setColor(randomColor);
    		g.fillRect(0, 0, getWidth(), getHeight());
    
    		g.setColor(Color.BLACK);
    		g.fillOval(50, 50, 200, 200);
    	}
    (move setSize into the init() method!)

Similar Threads

  1. Replies: 2
    Last Post: 10-07-2011, 07:48 AM
  2. Debugging a Java Code which is run by a Shell Script
    By ArunJava in forum New To Java
    Replies: 0
    Last Post: 09-12-2011, 03:21 PM
  3. Debugging Help Needed
    By vittoire in forum New To Java
    Replies: 3
    Last Post: 01-26-2010, 05:31 PM
  4. Debugging an applet - lost for solutions
    By olbion in forum New To Java
    Replies: 2
    Last Post: 05-08-2008, 10:13 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
  •