| | |
looping the drawLine() method
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 13
Reputation:
Solved Threads: 0
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
}
}
}
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
}
}
}
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 3
0
#2 24 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
That is asuming I understood what you try to accomplish.
Java Syntax (Toggle Plain Text)
int x1=0; //top left int y1=0 int x2=0; //botom left int y2=100; for(int i=0;i<100;i++){ g.drawLine(x1,y1,x2,y2); //x1 does not change y1=y1-1; //we start the next line a bit lower x2=x2+1 //and finish it a bit to the right //y2 stays the same. }
That is asuming I understood what you try to accomplish.
Last edited by santiagozky; 24 Days Ago at 3:50 pm.
0
#3 24 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
http://java.sun.com/docs/books/tutor...ng/closer.html
Last edited by Ezzaral; 24 Days Ago at 3:55 pm.
•
•
Join Date: Sep 2009
Posts: 13
Reputation:
Solved Threads: 0
0
#4 24 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.
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.
•
•
Join Date: Oct 2009
Posts: 18
Reputation:
Solved Threads: 3
0
#5 24 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.
•
•
Join Date: Sep 2009
Posts: 13
Reputation:
Solved Threads: 0
0
#7 21 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.
haha thanks everyone. got it all done yesterday in class. went very well. thanks for your help =), couldnt have figured it out without ya
![]() |
Similar Threads
- stop method from executing (C#)
- Simple Paint Program concepts (Java)
- counting how many text boxes have been modified (VB.NET)
- hiii (Java)
- How to draw a graph in Java applet (Java)
- Looping through arrays (PHP)
- Is it possible to start this special thread in paintComponent() method ? (Java)
Other Threads in the Java Forum
- Previous Thread: JAVA With NETBEANS and Sorting a Set
- Next Thread: generating squares in random points with a random sizes
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database defaultmethod development dice dragging ebook eclipse educational error event exception formatingtextintooltipjava fractal game givemetehcodez graphics gui hql html ide image infinite ingres input integer invokingapacheantprogrammatically j2me java javaprojects jni jpanel jtextarea julia linux list loop looping map method methods mobile mysql netbeans newbie openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads tree websites windows






