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


}
}
}

Recommended Answers

All 6 Replies

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

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.

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.

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.