Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By doWhile

Thread: How to draw a circle with different colours. PLEASE HELP

  1. #1
    ramin is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default How to draw a circle with different colours. PLEASE HELP

    Hello

    I need help with this exercise. I hope someone can help me.

    I need to create a program that draws circles in the middle of a DrawingPanel and draw them in different colors from red to blue via green. The program will ask the user how many circles to be drawn out and then plot them. Part of the program should be a method drawColoredCircles that draws circles, another part asks the user how many people will be drawn out. The circles should be drawn up from the inside out, they should be completely symmetrical circles and not ovals, and they should be drawn up for each other delays. Each circle should be larger than the previous one and the circles color codes will vary so that the transitions between the three primary colors are as soft as possible.
    Use it properly documented version of DrawingPanel.java for this task.

    I allready have this code but I don't know how to continue from it.

    public static void main(String[] args) {
    DrawingPanel panel = new DrawingPanel(500, 400);
    Graphics2D g = panel.getGraphics();
    int distance = 10;
    int max = 16;
    for (int i = 0; i < max; i++) {
    g.setColor(new Color(255 - i, 0, i));
    g.setStroke(new BasicStroke(2));
    int width = 10 + i * distance;
    g.drawOval(0, 0, width, width);
    }
    }

    These are the steps i need to do after:
    Step 2 I need to extend the code so it just don't make circles that goes from red to blue but it goes also from red to blue via green. "Use a for loop and an if statement in the loop to determine if one is to mix red and green or green and blue."

    Step 3 I need a delay between each circle so that i can see when they are drawn up. And the program should also ask the user how many circles to be plotted

    This is how it is supposed to look like after all is done.



    thanks in advance.

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

    Default Re: How to draw a circle with different colours. PLEASE HELP

    Quote Originally Posted by ramin View Post
    Hello

    I need help with this exercise. I hope someone can help me.

    I need to create a program that draws circles in the middle of a DrawingPanel and draw them in different colors from red to blue via green. The program will ask the user how many circles to be drawn out and then plot them. Part of the program should be a method drawColoredCircles that draws circles, another part asks the user how many people will be drawn out. The circles should be drawn up from the inside out, they should be completely symmetrical circles and not ovals, and they should be drawn up for each other delays. Each circle should be larger than the previous one and the circles color codes will vary so that the transitions between the three primary colors are as soft as possible.
    Use it properly documented version of DrawingPanel.java for this task.

    I allready have this code but I don't know how to continue from it.

    Java Code:
     public static void main(String[] args) {
     		DrawingPanel panel = new DrawingPanel(500, 400);
    		Graphics2D g = panel.getGraphics();
    		int distance = 10;
    		int max = 16;
    		for (int i = 0; i < max; i++) {
    			g.setColor(new Color(255 - i, 0, i));
    			g.setStroke(new BasicStroke(2));
    			int width = 10 + i * distance;
    			g.drawOval(0, 0, width, width);
    		}
    	}
    These are the steps i need to do after:
    Step 2 I need to extend the code so it just don't make circles that goes from red to blue but it goes also from red to blue via green. "Use a for loop and an if statement in the loop to determine if one is to mix red and green or green and blue."

    Step 3 I need a delay between each circle so that i can see when they are drawn up. And the program should also ask the user how many circles to be plotted

    This is how it is supposed to look like after all is done.



    thanks in advance.

    Is that code that you created, or is it code given to you by your instructor or downloaded from some source on the internet?

    The reason I ask is that it is incorrect code since it obtains a Graphics object from a component by calling the getGraphics() method, something that shouldn't be done in this situation.

    Instead I suggest that you do all of your drawing in a JComponent or JPanel's paintComponent method and that you use the Graphics object passed into this method's parameter. Google for the Swing tutorials and check out the section and painting in Swing for more on this.

    As for "Step 2", I recommend that you check out the Color class, especially its constructor that uses 3 ints to represent red, green, and blue, and use it and a little simple math to set your colors.

    For "Step 3", you should use a Swing Timer, and in the timer increment a number that is used by a for loop in the paintComponent method. At the end of the Swing Timer's code, you would call repaint on the component that's doing the drawing so that the JVM will call the paintComponent method (you almost never directly call this method yourself). There's a decent tutorial on how to use Swing Timers that is easily found through Google.

  3. #3
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: How to draw a circle with different colours. PLEASE HELP

    DarrylBurke likes this.

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

    Default Re: How to draw a circle with different colours. PLEASE HELP

    Quote Originally Posted by doWhile View Post
    Thanks for the warning. I will proceed with caution.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: How to draw a circle with different colours. PLEASE HELP

    Quote Originally Posted by doWhile View Post
    Thanks for the heads-up.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. outputting different letters in different colours
    By Sarah24 in forum Java Applets
    Replies: 4
    Last Post: 03-02-2011, 03:32 PM
  2. How to print words in different colours?
    By veen in forum New To Java
    Replies: 7
    Last Post: 06-10-2010, 05:54 PM
  3. How can i draw a circle on an image uploaded ?
    By motress in forum New To Java
    Replies: 1
    Last Post: 04-15-2010, 05:55 PM
  4. Replies: 0
    Last Post: 01-27-2010, 04:52 PM
  5. PDFBOX change text colours and remove background
    By gstkein in forum Advanced Java
    Replies: 1
    Last Post: 11-20-2009, 12:54 PM

Posting Permissions

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