gpt4 book ai didi

Java Swing - 如何在 Canvas 中绘制新线并将其添加到现有线中?

转载 作者:行者123 更新时间:2023-12-02 11:37:59 24 4
gpt4 key购买 nike

我已经在 J​​ava Swing 中制作了哥本哈根 map ,但我希望能够在 map 上绘制新线,然后将它们添加到我从缓冲阅读器中获取的现有线中。我尝试将新绘制的线条添加到临时线条的数组列表中,但这似乎不起作用 - 我根本无法再在 Canvas 上绘制。有人可以帮忙吗?

public class Model extends Observable implements Iterable<Line2D>, Serializable {
private List<Line2D> lines;
private List<Line2D> tmpLines;

public Model() {
lines = new ArrayList<>();
tmpLines = new ArrayList<>();
}

public Model(String filename) {
readFromFile(filename);
}

public void add(Line2D line) {
lines.add(line);
dirty();
}

public void dirty() {
setChanged();
notifyObservers();
}

public void readFromFile(String filename) {
lines = new ArrayList<>();
tmpLines = new ArrayList<>();
try {
BufferedReader b = new BufferedReader(new FileReader(filename));
for (String line = b.readLine(); line != null; line = b.readLine() ) {
String[] tokens = line.split(" ");
if (tokens[0].equals("LINE")) {
double x1 = Double.parseDouble(tokens[1]);
double y1 = Double.parseDouble(tokens[2]);
double x2 = Double.parseDouble(tokens[3]);
double y2 = Double.parseDouble(tokens[4]);
lines.add(new Line2D.Double(x1, y1, x2, y2));
}

}

}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}

/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
public Iterator<Line2D> iterator() {
return lines.iterator();
}

public Line2D removeLastFromTmpLines() {
Line2D line = tmpLines.remove(tmpLines.size() - 1);
dirty();
return line;
}

public Line2D removeLastFromLines() {
Line2D line = lines.remove(lines.size() - 1);
dirty();
return line;
}

public void addTmpLinesToLines() {
lines.addAll(tmpLines);
dirty();
}

public void addToTmpLines(Line2D line2D) {
tmpLines.add(line2D);
dirty();
}

public List<Line2D> getTmpLines() {
return tmpLines;
}
}
public class MouseController extends MouseAdapter {
private Model model;
private CanvasView canvas;
private Point2D lastMousePosition;

public MouseController(CanvasView c, Model m) {
canvas = c;
model = m;

canvas.addMouseListener(this);
canvas.addMouseWheelListener(this);
canvas.addMouseMotionListener(this);
}

@Override
public void mouseDragged(MouseEvent e) {
Point2D currentMousePosition = e.getPoint();
if (canvas.inDrawingMode()) {
Point2D origin = model.removeLastFromTmpLines().getP1();
model.addToTmpLines(new Line2D.Double(origin, canvas.toModelCoords(currentMousePosition)));
}
else {
double dx = currentMousePosition.getX() - lastMousePosition.getX();
double dy = currentMousePosition.getY() - lastMousePosition.getY();
canvas.pan(dx, dy);
}
lastMousePosition = currentMousePosition;
}

@Override
public void mousePressed(MouseEvent e) {
lastMousePosition = e.getPoint();
if (canvas.inDrawingMode()) {
model.addToTmpLines(new Line2D.Double(
canvas.toModelCoords(lastMousePosition),
canvas.toModelCoords(lastMousePosition)));
}
}

public void mouseMoved(MouseEvent e) {
Point2D modelCoords = canvas.toModelCoords(e.getPoint());
//System.out.println("Screen: [" + e.getX() + ", " + e.getY() + "], "+"Model: [" + modelCoords.getX() + ", " + modelCoords.getY() + "]");
}


@Override
public void mouseWheelMoved(MouseWheelEvent e) {
double factor = pow(1.1, e.getWheelRotation());
canvas.zoom(factor, -e.getX(), -e.getY());
}
}

最佳答案

说实话,我不太理解代码,因为我没有看到paint方法,所以我不明白你对数组做了什么。我假设您在列表中添加行并使用迭代器方法将它们返回到绘制方法。

我假设您在此处按下鼠标按钮时添加一个点,而不是一条线:

model.addToTmpLines(new Line2D.Double(
canvas.toModelCoords(lastMousePosition),
canvas.toModelCoords(lastMousePosition)));

一个点,因为坐标相同。然后,当拖动鼠标时,您将删除最后一条线(点)并从点的起始坐标到实际鼠标位置添加一条新线。这样对吗?但是,什么时候将行从 tmp 列表传递到实际列表呢?您是否不需要 mouseReleased 方法将行从 tmp 列表传递到实际列表?并且迭代器方法仅返回列表行,因此我假设最后一行(被拖动的行)未绘制,因为实际的新行位于 tmpLines 列表中并且未返回。

但我认为我没有看到全貌,所以我的评论可能没有用......

关于Java Swing - 如何在 Canvas 中绘制新线并将其添加到现有线中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48784775/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com