944,082 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 827
  • Java RSS
Nov 9th, 2009
0

looping the drawLine() method

Expand Post »
im trying to make my program draw lines from the top right corner of the JFrame which is 400 x 400, to the bottom of it making what looks like a curve but really is just lines. i think i know the basic idea of how to do this which would be to subtract 1 from x and add 1 to y and loop that in a for loop. i just cant get my syntax right or sumthin (im a first year java student) so far i have (and i dont really understand these code tag things so forgive me)


import javax.swing.*;
import java.awt.*;

public class DrawingColor{
public static void main(String[] args) {
DrawingColor d = new DrawingColor();
}

public DrawingColor(){
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MyComponent());
frame.setSize(400,400);
frame.setVisible(true);
}

public class MyComponent extends JComponent{
public void paint(Graphics g){
int height = 200;
int width = 120;

// and here is where my loop would start




}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javanub123 is offline Offline
18 posts
since Sep 2009
Nov 10th, 2009
0
Re: looping the drawLine() method
I haven't write anything to draw things in a long time but what you basically need to do is declare your coordinates in variables (x1,y1,x2,y2 for example). Then initialise them in whenever place you wish and in every loop iteration substract an add as needed

Java Syntax (Toggle Plain Text)
  1. int x1=0; //top left
  2. int y1=0
  3. int x2=0; //botom left
  4. int y2=100;
  5.  
  6. for(int i=0;i<100;i++){
  7. g.drawLine(x1,y1,x2,y2);
  8. //x1 does not change
  9. y1=y1-1; //we start the next line a bit lower
  10. x2=x2+1 //and finish it a bit to the right
  11. //y2 stays the same.
  12.  
  13. }

That is asuming I understood what you try to accomplish.
Last edited by santiagozky; Nov 10th, 2009 at 3:50 pm.
Reputation Points: 11
Solved Threads: 3
Newbie Poster
santiagozky is offline Offline
18 posts
since Oct 2009
Nov 10th, 2009
0
Re: looping the drawLine() method
Just a note: it's preferable to override paintComponent() in most cases instead of paint(), since paint() is also responsible to painting children and the border.
http://java.sun.com/docs/books/tutor...ng/closer.html
Last edited by Ezzaral; Nov 10th, 2009 at 3:55 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 10th, 2009
0
Re: looping the drawLine() method
yeah this is what i would like to do.
i started out fixing 2 of the coordinates since they dont need to change
then just looped adding or subtracting from the two remaining coords. when i did this all i got was a big black triangle where the lines and the curve should be. i dont know if the way i wanted to do it just wont work or my syntax is wrong.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javanub123 is offline Offline
18 posts
since Sep 2009
Nov 10th, 2009
0
Re: looping the drawLine() method
Click to Expand / Collapse  Quote originally posted by javanub123 ...
yeah this is what i would like to do.
i started out fixing 2 of the coordinates since they dont need to change
then just looped adding or subtracting from the two remaining coords. when i did this all i got was a big black triangle where the lines and the curve should be. i dont know if the way i wanted to do it just wont work or my syntax is wrong.
A picture might help but I think that you should place more space between lines. Instead os y1=y1+1 try y1=y1+5 or 10. Play with the values until you get what you want.
Reputation Points: 11
Solved Threads: 3
Newbie Poster
santiagozky is offline Offline
18 posts
since Oct 2009
Nov 10th, 2009
0
Re: looping the drawLine() method
Sounds like you just need to tweak your coordinate math a little. If you got a solid triangle you are on the right track.

Edit: Oops, cross posted with santiagozky. I would agree that you should add more than if you don't want solids.
Last edited by Ezzaral; Nov 10th, 2009 at 7:14 pm.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 13th, 2009
0
Re: looping the drawLine() method
Click to Expand / Collapse  Quote originally posted by Ezzaral ...
Sounds like you just need to tweak your coordinate math a little. If you got a solid triangle you are on the right track.

Edit: Oops, cross posted with santiagozky. I would agree that you should add more than if you don't want solids.

haha thanks everyone. got it all done yesterday in class. went very well. thanks for your help =), couldnt have figured it out without ya
Reputation Points: 10
Solved Threads: 0
Newbie Poster
javanub123 is offline Offline
18 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: JAVA With NETBEANS and Sorting a Set
Next Thread in Java Forum Timeline: generating squares in random points with a random sizes





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC