因此,我拥有的是一个绘制相位轨迹的程序。目前,起点都是随机的,但是我要添加的是程序从单击的点开始轨迹的一种方法。我已经摆弄了好几个小时,尝试了我所知道的一切,下面是代码:
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(r, t);
g.lastElement().setColor(Color.blue);
它告诉我的是找不到r和t。我意识到,如果没有完整的代码,可能很难提供帮助,但这是大量的代码,如果您真的愿意提供帮助,我可以通过电子邮件将其发送给某人。但是在其他情况下,有人知道我能做什么?
最佳答案
1)r
和t
不在您的getGraphs()
方法的范围内。
2)您似乎没有在任何地方将鼠标适配器注册为MouseListener
3)不清楚click()
方法如何被调用
您需要捕获来自窗口组件的鼠标单击,假设它是您正在使用的JPanel。
然后您的代码将如下所示:
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();
}
});
}
}