Drawing line, wave etc... According to equation?

Hello friends,

I was playing with graphics 2d and i require some help.

how to draw onto the pixel.?

I tried

g2.draw((Shape)new Point2D.Float(x, y));

here x ,y changes according to an equation.

but i got exception.

how can we draw on to a specific pixel.

g2.draw(new Line2D.Float(x, y, x, y));

gave me the expected result. but not Satisfactory...

as equation give only specific points and not all the points on the track.

and if i go by precision values say by 0.001 , it takes too much time to render.

i want to draw a sine wave and every point on the wave should be render. How can i achieve it.

thanks.

[757 byte] By [Avinash.Ka] at [2007-11-27 8:24:12]
# 1

> I tried

> > g2.draw((Shape)new Point2D.Float(x, y));

>

OMG !

Look at javadoc. Point2D.Float is NOT a Shape. New to OO programming ?

>

> but i got exception.

>

yep.

> how can we draw on to a specific pixel.

Look at javadoc

> Satisfactory...

> as equation give only specific points and not all the

> points on the track.

> and if i go by precision values say by 0.001 , it

> takes too much time to render.

You cannot have 0.001 precision on screen since pixel coordinates are integer values. New to Math studies ?

Compute each Y for each integer X => you have a points

> i want to draw a sine wave and every point on the

> wave should be render. How can i achieve it.

New to Algorithm ?

Loop X from 0 to w where w is the desired width in pixels.

Compute Y for each X ; Draw a line between last 2 points.

You know what ? it works not only for sin !

> thanks.

Your welcome.

Kilwcha at 2007-7-12 20:13:10 > top of Java-index,Security,Cryptography...
# 2
g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
crwooda at 2007-7-12 20:13:11 > top of Java-index,Security,Cryptography...
# 3

public void paintComponent(Graphics g) {

Graphics2D g2 = (Graphics2D) g;

Path2D.Double path = new Path2D.Double();

double scale = 100; //choose the appropriate scale;

double offsetX = 100; // choose the appropriate offset;

double offsetY = 100;

path.moveTo(offsetX, offsetY);

for (double t = 0.1; t < Math.PI; t += 0.1) {

path.lineTo(offsetX + scale * t, offsetY -scale * Math.sin(t));

}

g2.draw(path);

}

James_Vagabonda at 2007-7-12 20:13:11 > top of Java-index,Security,Cryptography...
# 4

hi Kilwch

thanks for ur reply.

> > I tried

> > > > g2.draw((Shape)new Point2D.Float(x, y));

> >

ya i admit it was my mistake or weirdness to do that.

>

> > Satisfactory...

> > as equation give only specific points and not all

> the

> > points on the track.

> > and if i go by precision values say by 0.001 , it

> > takes too much time to render.

> You cannot have 0.001 precision on screen since pixel

> coordinates are integer values. New to Math studies

> ?

here u misinterpreted me. here i was not saying of co-ordinates but the angle for sine wave.

see my code

double y=0, x=0;

for(int i=0;i<50000;i++)

{

y = 10*Math.sin(x);

System.out.println("cordinates ( "+ i+" ' "+y+" )");

g2.draw(newLine2D.Double(i/1000+150,y+200,i/1000+150,y+200));

x=x+.001;

}

now u would understand..

> Compute each Y for each integer X => you have a

> points

> > i want to draw a sine wave and every point on the

> > wave should be render. How can i achieve it.

> New to Algorithm ?

> Loop X from 0 to w where w is the desired width in

> pixels.

> Compute Y for each X ; Draw a line between last 2

> points.

> You know what ? it works not only for sin !

yes I know that. But that does not give smooth curve.

for eg. say for equation x = y*y;

we get points

0,0

1,1

2,4

3,9

4,16

......

and if we join them we don't get a smooth curve...

Avinash.Ka at 2007-7-12 20:13:11 > top of Java-index,Security,Cryptography...