سؤال

I've been working through The Art & Science of Java text and SEE CS106A course. Everything has been going smoothly until interactive graphics programs were introduced. The following code, taken directly from the text, will not compile:

/*
 * File: DrawLines.java
 * -----------------------
 * This program allows a user to draw lines to the canvas.
 */

import acm.graphics.*;
import acm.program.*;
import java.awt.event.*;

public class DrawLines extends GraphicsProgram {

    public void run() {
        addMouseListeners();
    }

    /** Called on mouse press to record the coordinates of the click */
    public void mousePressed(MouseEvent e) {
        double x = e.getX();
        double y = e.getY();
        line = new GLine(x, y, x, y);
        add(line);
    }

    /** Called on mouse drag to reposition the object */
    public void mouseDragged(MouseEvent e) {
        double x = e.getX();
        double y = e.getY();
        line.setEndPoint(x, y);
    }

    private GLine line;

}

It fails on line 14 with a cannot find symbol: method addMouseListeners() error. ACM ConsolePrograms and GraphicsPrograms without that method call work fine. As far as I can tell this method should be valid.

Am I doing something wrong here? Are the ACM documentation and textbook outdated? How do I add a mouse listener here?

هل كانت مفيدة؟

المحلول

It turns out that karel.jar, a library used in the first assignment of CS106A, interferes with the addMouseListeners() method. Removing karel.jar from source resolves the issue.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top