Reputation: 115
I've made a map over Copenhagen in Java Swing, but I would like to be able to draw new lines to the map, and then afterwards add them to the existing line, that I get in from my buffered reader. I have tried adding the new drawn lines to an arraylist of temporary lines, but that does not seem to work - I am simply not able to draw on the canvas anymore. Can anyone help out?
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());
}
}
Upvotes: 0
Views: 94
Reputation: 127
to be honest, I don't understand very well the code because I don't see the paint method, so I don't see what you do with the arrays. I suppouse that you add lines in the Lists and return them to the paint method using the iterator method.
I suppouse that you add a DOT, not a line, when the mouse button is pressed here:
model.addToTmpLines(new Line2D.Double(
canvas.toModelCoords(lastMousePosition),
canvas.toModelCoords(lastMousePosition)));
A DOT because the coordinates are the same. Then, when the mouse is dragged you remove the last line (DOT) and add a new line from the starting coordinate of the dot to the actual mouse position. Is it right? But, when do you pass the line from the tmp list to the real list? Wouldn't you need a mouseReleased method to pass the lines from the tmp list to the real list? And the iterator method returns only the List lines, so I suppouse that the last line, the line being dragged, isn't painted because the actual new line is in the tmpLines list and is not returned.
But I think tht I'm not seeing the whole picture, so my comment could be unuseful...
Upvotes: 1