0
我正在Java中实现Line DDA算法。代码看起来很好。Applet调整DDA算法在Java中的实现
我正面临的问题是调整applet大小。小程序输出很小,我希望它能够以640-by-480的小程序窗口大小工作。
对于我已经使用resize(640,480)
;在paint()
方法的开始,但它不起作用。它的作用是在小窗口(我认为是350×200)上给出正确的输出,然后将其自身扩大到640×480,程序不会终止(我必须强制退出程序) 。任何帮助,将不胜感激。我的实现代码是:
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 the value of x1 : ");
x1 = sc.nextInt();
System.out.println("Enter the value of y1 : ");
y1 = sc.nextInt();
System.out.println("Enter the value of x2 : ");
x2 = sc.nextInt();
System.out.println("Enter the 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) {
}
}
}
}