Please run the code and see the error, it looks very straight forward, but just not working. I like to see what happens when I click, so I can modify it.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
publicclass DrawOval extends JFrame implements MouseListener{
Container con = getContentPane();
ArrayList<Goal> allOvals;
Goal parentOval;
double x, y;
/** Creates a new instance of Main */
public DrawOval() {
super.setTitle("Drawing Goal Hierarchy");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
con.addMouseListener(this);
allOvals = new ArrayList<Goal>();
parentOval = null;
}
publicvoid mouseClicked(MouseEvent e){
x = e.getX();
y = e.getY();
Goal newGoal = new Goal("title", x , y , parentOval);
if(parentOval == null){
parentOval = newGoal;
}
}
publicvoid mouseEntered(MouseEvent e){
}
publicvoid mouseExited(MouseEvent e){
}
publicvoid mousePressed(MouseEvent e){
}
publicvoid mouseReleased(MouseEvent e){
}
public String getToolTipText(MouseEvent e){
return parentOval.getToolTipText();
}
publicvoid paint (Graphics g){
Graphics2D g2 = (Graphics2D)g;
for (Goal goal : allOvals){
goal.drawGoal(g2);
}
}
publicstaticvoid main(String[] args) {
Goal go = new Goal("CG1", x, y, parentOval);
go.setSize(500, 400);
go.setVisible(true);
}
}
Would be easier if you would simply post the error message.
Anyway, I got this error message (and others): D:\Projekte\Cam Interface\src\DrawOval.java:58: non-static variable x cannot be referenced from a static context Goal go = new Goal("CG1", x, y, parentOval);
Is this the error wich causes you trouble?
a "static context" is a method or a variable wich can be called without initializing the class.
The main method is such a method: The Java runtime routine simply calls it without creating an instance of DrawOval.
Therefore the variables x and y don't exist yet.
In the main method you should only create a new DrawOval and show it, nothing else.
Create Goals only when clicking with the mouse.
Just as a question: Why do you store x and y in DrawOval? They are only needed when you click or move the mouse and then you can get them from the MouseEvent.
> haha, > I'm not from a software background, from the Electronics > background. The code was not indented because it was > pasted to a Word Document for correct, it was originally > indented in NetBeans. Then I just copied and posted the > word document version. > > Thanks for understanding my ideas How do the above rantings relate to your capabilities of indenting your code???
Flat View: This topic has 19 replies
on 2 pages
[
«
|
12
]