swing - Java: Draw a circular spiral using drawArc -
i'm working on java programming exercise have draw circular spiral using drawarc method result looks similar this:
i've been working on while , have far:
import java.awt.graphics; import javax.swing.jpanel; import javax.swing.jframe; public class circspiral extends jpanel { public void paintcomponent(graphics g) { int x = 100; int y = 120; int width = 40; int height = 60; int startangle = 20; int arcangle = 80; (int = 0; < 5; i++) { g.drawarc(x, y, width, height, startangle, arcangle); g.drawarc(x + 10, y + 10, width, height, startangle + 10, arcangle); x = x + 5; y = y + 5; startangle = startangle - 10; arcangle = arcangle + 10; } } public static void main(string[] args) { circspiral panel = new circspiral(); jframe application = new jframe(); application.setdefaultcloseoperation(jframe.exit_on_close); application.add(panel); application.setsize(300, 300); application.setvisible(true); } }
my code gives me result:
i know problem lies in arguments drawarc method because numbers aren't right, don't know how go making numbers go in circular manner. appreciated. thank you!
your idea right. did modifications. need inverse angle draw other side of spiral , use fixed point startangle
.
import java.awt.graphics; import javax.swing.jpanel; import javax.swing.jframe; public class circspiral extends jpanel { public void paintcomponent(graphics g) { int x = getsize().width / 2 - 10; int y = getsize().height/ 2 - 10; int width = 20; int height = 20; int startangle = 0; int arcangle = 180; int depth = 10; (int = 0; < 10; i++) { if (i % 2 == 0) { // g.drawarc(x + 10, y + 10, width, height, startangle + 10, -arcangle); // x = x - 5; y = y - depth; width = width + 2 * depth; height = height + 2 * depth; g.drawarc(x, y, width, height, startangle, -arcangle); } else { // g.drawarc(x + 10, y + 10, width, height, startangle + 10, arcangle); x = x - 2 * depth; y = y - depth; width = width + 2 * depth; height = height + 2 * depth; g.drawarc(x, y, width, height, startangle, arcangle); } } } public static void main(string[] args) { circspiral panel = new circspiral(); jframe application = new jframe(); application.setdefaultcloseoperation(jframe.exit_on_close); application.add(panel); application.setsize(300, 300); application.setvisible(true); } }
Comments
Post a Comment