开发者

Graph Click Interface

So, what I have is a program that plots Phase trajectories. At the moment the starting points are all random, but what I am trying to add is a way for the program to start a trajectory from the point I click on. I've been fiddling with it for hours, trying everything I know, well here's the code:

public static void click(final double r, final double t) {
    MouseListener mus = new MouseAdapter() {
        public void mouseClicked(MouseEvent e) {
            double r = e.getX();
            double t = e.getY();
        }
    };
}

public Vector<Graph> getGraphs() {
    // ... bunch of code that draws the graph...
    g.add(new Graph.Line());
    g.lastElement().add(开发者_Go百科r, t);
    g.lastElement().setColor(Color.blue);

And what it tells me is that r and t can't be found. I realize that it might be hard to help without the whole code, but it is loads of code, I can email it to someone if you are really willing to help. But in any other case, anyone got an idea what I can do?


1) r and t are not in scope for your getGraphs() method.

2) You don't seem to have registered your mouse adapter as a MouseListener anywhere

3) It is not clear how the click() method gets called


You need to capture the mouse clicks from a window component, let's say it is a JPanel that you are using.

Then your code would look something like this:

public class MyApplication {
    private JFrame myWindow = new JFrame("My Application");
    private JPanel myPanelYouCanClick = new JPanel();

    public MyApplication() {
        myWindow.setContantPane(myPanelYouCanClick);
        myPanelYouCanClick.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                double r = e.getX();
                double t = e.getY();
                // Code to create your new trajectory called from here, pass
                // in the values of r and t if required. Remember you are
                // running on the event dispatcher thread!
            }
        });
        myWindow.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MyApplication app = new MyApplication();
            }
        });
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜