This site requires JavaScript, please enable it in your browser!
Greenfoot back
wlau1010
wlau1010 wrote ...

2016/12/9

Help drawing a line

wlau1010 wlau1010

2016/12/9

#
Here is my code,
1
getBackground().drawLine(Mouse_follower.p1x, Mouse_follower.p1y, Line_point_2.p2x, Line_point_2.p2y);
I am trying to make a line in-between 2 objects ignore the names as they don't mean anyhting as I changed what I was doing halfway through and don't know how to change the name. The 2 points move randomly. I have imported java.awt.Color so I don't know the problem. *EDIT I figured it out it was drawing a white line on a white background. I fixed it but would like to know how change the color of the line though. It would be nice if I was able to change the color of the line as it runs so it changes from white to green to black etc.
danpost danpost

2016/12/9

#
Having a single line with multiple colors takes a bit of coding to accomplish. To create a line of a any single color is a breeze in comparison. It is just a matter of setting the drawing color of the background image and drawing the line. Here is for any random color of clear visibility on a white background:
1
2
3
4
5
int r = Greenfoot.getRandomNumber(128);
int g = Greenfoot.getRandomNumber(128);
int b = Greenfoot.getRandomNumber(128);
getBackground().setColor(new Color(r, g, b));
getBackground().drawLine( // etc.
wlau1010 wlau1010

2016/12/12

#
Thanks so much, I am new to coding but this makes sense with the rgb values.
You need to login to post a reply.