Web Hosting Talk







View Full Version : How to draw random color lines in Java?


vip2k
02-15-2004, 06:46 PM
How to draw random color lines in Java in an applet? thx

idologic_aw
02-16-2004, 12:06 PM
import java.awt.* // for Graphics,Color
import java.util.* // for Random, Date

Graphics g;
int r,g,b; // color components
int x1,y1,x2,y2; // line coordinates

d = Date();
random = Random(d.getTime());
r = random.getInt(256); // gets number between 0,255
random = Random(d.getTime());
g = random.getInt(256);
random = Random(d.getTime());
b = random.getInt(256);

g.setColor(Color(r,g,b));
x1 = 10;
y1 = 10;
x2 = 20;
y2 = 20;
g.drawLine(x1,y1,x2,y2);

Hope that helps you get started.

vip2k
02-16-2004, 03:46 PM
oic, thanks yo..

vip2k
02-16-2004, 04:29 PM
ops.. how come this line gives a error?

g.setColor(Color(r,g,b));
^

"cannot resolve symbol
symbol : method Color (int,int,int)"

Thx

idologic_aw
02-16-2004, 04:33 PM
try this instead

c = Color(r,g,b)
g.setColor(c)

vip2k
02-16-2004, 05:28 PM
okie, wat's the variable type for c ? thx

idologic_aw
02-16-2004, 05:45 PM
Um, its a Color object.
c = Color(r,g,b); instantiates a new variable
c which is a Color object which is the parameter
type you need for g.setColor()

vip2k
02-16-2004, 06:15 PM
o, i got it.. thx lotz.

Color c = new Color(r,g,b);

lines.setColor(c);

lines.drawLine(x1, y1, x2, y2);

idologic_aw
02-17-2004, 10:34 AM
Great!
I would, for maintenance reasons, change the name
of lines to graphics so that anyone looking over
your code does not get confused.