looping the drawLine() method

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 13
Reputation: javanub123 is an unknown quantity at this point 
Solved Threads: 0
javanub123 javanub123 is offline Offline
Newbie Poster

looping the drawLine() method

 
0
  #1
24 Days Ago
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




}
}
}
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: santiagozky is an unknown quantity at this point 
Solved Threads: 3
santiagozky santiagozky is offline Offline
Newbie Poster
 
0
  #2
23 Days Ago
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

  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; 23 Days Ago at 3:50 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #3
23 Days Ago
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; 23 Days Ago at 3:55 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: javanub123 is an unknown quantity at this point 
Solved Threads: 0
javanub123 javanub123 is offline Offline
Newbie Poster
 
0
  #4
23 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 18
Reputation: santiagozky is an unknown quantity at this point 
Solved Threads: 3
santiagozky santiagozky is offline Offline
Newbie Poster
 
0
  #5
23 Days Ago
Originally Posted by javanub123 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,479
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
0
  #6
23 Days Ago
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; 23 Days Ago at 7:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 13
Reputation: javanub123 is an unknown quantity at this point 
Solved Threads: 0
javanub123 javanub123 is offline Offline
Newbie Poster
 
0
  #7
20 Days Ago
Originally Posted by Ezzaral View Post
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC