RandomWalk 프로그램을 만들고 있습니다. 대부분의 프로그램은 정상적으로 작동하지만 중요한 문제가 하나 있습니다.PolyLine이 원점 (0,0)에서 그리기를 계속하는 이유는 무엇입니까?
PolyLine을 그릴 때 마지막 점이 있던 지점 대신 원점 (0,0)으로 되돌아갑니다. 나는 내가 무엇을 놓치고 있는지/잘못하고 있는지 보려고 노력했지만 문제를 찾을 수는 없습니다.
도움을 주시면 감사하겠습니다. 더 많은 정보가 필요하면 그냥 물어보십시오. 감사.
메인 클래스
import javax.swing.*;
import java.awt.*;
public class RandomWalk {
public static void main (String[] args) {
// Creating main frame
JFrame main = new JFrame("RandomWalk - Version 1.0");
main.setSize(800, 800);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main.setResizable(false);
main.setLocationRelativeTo(null);
// Creating content/container panel
JPanel container = new JPanel();
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
main.setContentPane(container);
// Creating scene/canvas
Draw canvas = new Draw();
canvas.setAlignmentX(Component.CENTER_ALIGNMENT);
container.add(canvas);
main.toFront();
main.setVisible(true);
}
}
그리기 클래스
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Draw extends JPanel {
// Starting value for i
public static int i = 1;
// Increment for line length
public static int inc = 10;
// Choose amount of lines/moves
public static int a = 10000;
// Arrays for polyline points
public static int[] xPoints = new int[a];
public static int[] yPoints = new int[a];
public Timer timer = new Timer(5, new ActionListener() {
public void actionPerformed(ActionEvent e) {
xPoints[0] = 400;
yPoints[0] = 400;
if (i < a) {
double r = Math.random();
if (r < 0.25) {
xPoints[i] = xPoints[i - 1] - inc;
yPoints[i] = yPoints[i - 1] - 0;
i++;
} else if (r < 0.50) {
xPoints[i] = xPoints[i - 1] + inc;
yPoints[i] = yPoints[i - 1] + 0;
i++;
} else if (r < 0.75) {
yPoints[i] = yPoints[i - 1] - inc;
xPoints[i] = xPoints[i - 1] - 0;
i++;
} else if (r < 1.00) {
yPoints[i] = yPoints[i - 1] + inc;
xPoints[i] = xPoints[i - 1] + 0;
i++;
}
repaint();
}
}
});
public void paintComponent(Graphics g) {
timer.start();
g.drawPolyline(xPoints, yPoints, xPoints.length);
}
}
예, 작동하지만 원하는대로하지 않습니다. 매번 그려지는 선을보고 싶습니다. 그래서 모든 움직임은 그것을 끌어냅니다. 그래서 타이머에 넣었습니다. – jxshu
와우. 나는 총을 분명히 뛰어 탔다. 미안하다. 닫힌 투표를 철회했습니다. :) – Jashaszun
'... (..., ..., xPoints.length)'대신'g.drawPolyline (xPoints, yPoints, i)'를 사용하겠습니까? – Jashaszun