できれば一度に破線のセグメントで線をプロットしたいと思います。2 つの終点の座標が与えられると、直線の方程式を見つけることができますよね? したがって、この線上にあるすべてのポイント(ピクセル)を推測しますか? しかし、一度にセグメントをプロットし、最終的に 2 番目のエンドポイントに到達して前のセグメントを消去するのに問題があります。(0,0) から (5,5) に向かってプロットを開始し、その過程でセグメント (0,0)->(1,1) を表示し、これを非表示にしてセグメント (1, 1)->(2,2) などで、最終的に (5,5) のピクセルのみを表示します
public class pracDraw extends JFrame {
private Color red=Color.red;
public int i;
private Color white = Color.white;
JPanel pr=new JPanel();
JTextField t=new JTextField();
JTextField t1=new JTextField();
JTextField t2=new JTextField();
JTextArea ta=new JTextArea();
pracDraw(){
setTitle("Practise");
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(t);
add(t1);
add(pr);
}
public void paint(Graphics g){
g.setColor(Color.BLACK);
g.drawLine(100, 100, 200, 200);
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.BLUE);
g.drawLine(200, 200, 350, 375);
t.setBounds(390, 370, 10, 10);
t.setBackground(Color.RED);
t.setEnabled(false);
Thread t5=new Thread(){
public void run()
{
for(;;){
int x,y,x1=200,y1=200,x2=400,y2=400,t_x,t_y,slope;
slope=(y2-y1)/(x2-x1); //slope=1
int c=(y1-(slope*x1)); //c=0
for(i=200;i<375;i+=20){
if(i==(slope*i+c)){
t1.setBounds(i,i, 10, 10);
t1.setBackground(Color.RED);
t1.setEnabled(false);
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
};
t5.start();
if(i>375){
t5.stop();
t1.setBackground(Color.GREEN);
}
}
public static void main(String args[]){
pracDraw p=new pracDraw();
}
}