graphics - Applet Resize in DDA algorithm implementation in Java -
i implementing line dda algorithm in java. code seems fine.
the problem facing resizing applet size. applet output small , want work applet window size of 640-by-480.
have used resize(640,480)
; in beginning of paint()
method not work. is, gives output correct on small window (i think 350-by-200) , enlarges 640-by-480 , program not terminate (i have forcefully exit program). appreciated. implementation code is:
package line; import java.applet.applet; import java.awt.color; import java.awt.graphics; import java.util.scanner; public class ddaline extends applet { @override public void paint(graphics g) { //resize(640,480); g.setcolor(color.red); float x,y,x1,y1,x2,y2,dx,dy,steps,incrx,incry; int i; scanner sc = new scanner(system.in); system.out.println("enter value of x1 : "); x1 = sc.nextint(); system.out.println("enter value of y1 : "); y1 = sc.nextint(); system.out.println("enter value of x2 : "); x2 = sc.nextint(); system.out.println("enter value of y1 : "); y2 = sc.nextint(); dx = math.abs(x2-x1); dy = math.abs(y2-y1); if(dx>=dy) steps=dx; else steps=dy; incrx=dx/steps; incry=dy/steps; x=x1; y=y1; i=1; while(i<=steps) { g.drawline(math.round(x),math.round(y),math.round(x),math.round(y)); x=x+incrx; y=y+incry; i=i+1; try { thread.sleep(100); } catch (interruptedexception ex) { } } } }
solved putting setsize() method in init() method. code is:
import java.applet.applet; import java.awt.color; import java.awt.graphics; import java.util.scanner; public class ddaline extends applet { @override public void init(){ setsize(800, 600); } @override public void paint(graphics g) { g.setcolor(color.red); float x,y,x1,y1,x2,y2,dx,dy,steps,incrx,incry; int i; scanner sc = new scanner(system.in); system.out.println("enter value of x1 : "); x1 = sc.nextint(); system.out.println("enter value of y1 : "); y1 = sc.nextint(); system.out.println("enter value of x2 : "); x2 = sc.nextint(); system.out.println("enter value of y1 : "); y2 = sc.nextint(); dx = math.abs(x2-x1); dy = math.abs(y2-y1); if(dx>=dy) steps=dx; else steps=dy; incrx=dx/steps; incry=dy/steps; x=x1; y=y1; i=1; while(i<=steps) { g.drawline(math.round(x), math.round(y), math.round(x), math.round(y)); x=x+incrx; y=y+incry; i=i+1; try { thread.sleep(100); } catch (exception ex) { } } }
}
Comments
Post a Comment