Results 1 to 9 of 9
Thread: Any help?
 08082012, 05:59 PM #1Member
 Join Date
 Aug 2012
 Posts
 3
 Rep Power
 0
Any help?
Hi guys! first of all, awesome forum, ive read amazing thing that you did with java, just amazing.
Well, my name is Matt, im from Argentina and i just got "Java how to program 9th edition", great book, but im really stuck in the chapter 4, control statements part 1.
My question is if any of you could help me through a case study in which i have to make, some kind of fan using only while statemenst and the drawline method.
This is the image of what i have to make:
This is what i have so far:
Java Code:package drawpaneltest; import java.awt.Graphics; import javax.swing.JPanel; class DrawPanel extends JPanel{ public void paintComponent( Graphics g ) { int n=1; int widht=getWidth(); int height=getHeight(); while(n<15){ contador++; g.drawLine(0, 0, height/15, widht/15); } g.drawLine(0,height,widht,0); } }
Regards.
Matt.Last edited by Fubarable; 08082012 at 06:05 PM. Reason: code tags added

Re: Any help?
Moderator Edit: code tags ([code] [/code]) added to your original post so that your posted code will retain its formatting and be easier to read.
One problem: Your while loop will go on forever since it is checking the value of n, but you never change n from within the while loop and instead change a completely different variable, contador. Another problem, is that you don't use the loops index variable, n or contador, when figuring out which line to draw.
I suggest that you do the drawing on paper and figure out the logic of how the lines go where, and then use this knowledge in constructing your code algorithm.

Re: Any help?
Also, welcome to the javaforums.org.
Some suggestions that may help us help you:
 Use [code] [/code] around your code when posting code to this forum. This allows your code to retain its formatting, making it much easier to read.
 Please use an informative title for your question. "any help" doesn't tell us much about your underlying problem other than you need help. But of course you need help, otherwise you wouldn't be here. Instead use the question title kind of like the headline of a newspaper article  a very brief statement that summarizes your problem in an informative way. Perhaps a better title for your question would be something like: "Using a while loop to draw lines on a JPanel", or something similar. This would help attract the forum volunteers who best understand Swing graphics to look at your question.
Again welcome to our forum, and please post below if you have any questions about the recommendations above, or in my previous post to this thread.
 08082012, 07:18 PM #4Member
 Join Date
 Aug 2012
 Posts
 3
 Rep Power
 0
Re: Any help?
Thank you very much!!!!! ill keep that in mind for next postings, well...
I still stuck, maybe is a lack of practice, but i cant figure out yet the solution in paper...
I do know how to use while statements, but my issue comes when i try to, decrement the lines...
Ive been trying to solve this for a whole week and still cant figure out :(.
Again, sorry for the post error, wont happen again.

Re: Any help?
first you need to draw an arc, which is a quarter segment of a circle
from that arc at regular intervals (6 degrees) you need to calculate a point on that arc.
the end point of the lines from those points would be a point on another intangible arc which has a radius 200 pixels larger than the visible arc, rotated + or  60 degrees.
to do this i'd use 4 arrays:
int[] degrees = new int[60];
float[] radians = new float[60];
Point[] points1 = new Point[60];
Point[] points2 = new Point[60];
use a while loop with a counter that starts at 1 to populate those arrays:
degrees = counter * 6
radians = PI * degrees / 180
point1.x = width + Math.sin(radians) * width
point1.y = 0  Math.cos(radians) * height
point2.x = width + Math.sin(radians) * (width + 200)
point2.y = 0  Math.cos(radians) * (height + 200)
 08102012, 01:38 AM #6Member
 Join Date
 Aug 2012
 Posts
 3
 Rep Power
 0
 08102012, 04:03 AM #7

 08102012, 09:11 AM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,457
 Blog Entries
 7
 Rep Power
 20
Re: Any help?
Do your math: asssuming a square JComponent with sizes SxS and a coordinate system with the origin (0,0) in the upper left corner you have straight lines between the points (x, S) and (0, x); iterate of the values of x.
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
Bookmarks