• 01-03-2012, 09:22 PM
ramin
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.

http://mt.sh.se/informatik/labwiki/i...ic_circles.png

• 01-03-2012, 09:33 PM
Fubarable
Quote:

Originally Posted by ramin
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.

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.

http://mt.sh.se/informatik/labwiki/i...ic_circles.png

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.
• 01-04-2012, 01:28 AM
doWhile
• 01-04-2012, 01:31 AM
Fubarable
Quote:

Originally Posted by doWhile

Thanks for the warning. I will proceed with caution.
• 01-04-2012, 10:28 AM
DarrylBurke