Need help with drawing multiple objects with mouseClicked method.
I'm trying to get my program to draw multiple connected lines by clicking on different places on a JFrame. Need to have it so the user selects the option of creating a compound edge (consisting of multiple connected straight lines).
They click two different points with their mouse, and the program draws a line there , and they click somewhere else and a new line is drawn, which takes the endpoint of the previous line and uses it as its start point and the new point as its endpoint, etc. until the user selects an option to finish the compound edge.
createCompoundEdge and finishedCompoundEdge are just booleans which decide when to start and finish the compound edge.
right now I have two array lists:
the first one is a single connected group of lines, and the second is the collection of all groups of connected lines, but I'm wondering if there's a better way then to have an array list of array lists.
Edge class is pretty much a line.2D object.
public void mouseClicked(MouseEvent event)
mouseX = event.getX();
mouseY = event.getY();
if (createCompoundEdge && event.getButton() == event.BUTTON1)
line = new Edge();
x2 = event.getX();
y2 = event.getY();
int x = event.getX();
int y = event.getY();
compound.add(new Edge((compound.get(compound.size()-1)).getX2Point() , (compound.get(compound.size()-1)).getY2Point() , x, y));
createCompoundEdge = false;
finishedCompoundEdge = false;
I need help, I probably need to change the code inside the mouseClicked method, right now I am getting a Null Pointer exception error, and the lines don't draw (at all). This is obviously not the entire program, but the only part I think is relevant to this problem.
public class Edge extends GraphElement
private double xEndPos, yEndPos;
private Line2D.Double displayLine;
private static final double THRESHOLD_DIST = 3.0;
displayLine = new Line2D.Double();
displayLine.x1 = displayLine.y1 = displayLine.x2 = displayLine.y2 = 0;
Edge(double xs, double ys, double xe, double ye)
public void setStartPoint(double xStart, double yStart)
displayLine.x1 = xStart;
displayLine.y1 = yStart;
public void setEndPoint(double xEnd, double yEnd)
xEndPos = xEnd;
yEndPos = yEnd;
displayLine.x2 = xEnd;
displayLine.y2 = yEnd;
public boolean applyLabel()
public boolean applyImage()
public double getX1Point()
public double getY1Point()
public double getX2Point()
public double getY2Point()
boolean isSelected(double x, double y)
if (displayLine == null)
double dist = displayLine.ptSegDist(x,y);
if (dist < THRESHOLD_DIST)
public void draw(Graphics2D g2)
if (displayLine != null)
I want to see the line segment data structure.
I can't judge this code too well.
It seems ok.
How is the interface for the
array lists implemented?
What line does the nullPointer exception refer to?
The error states what line in
your code that the error occurs.
Copying the error alone will
not help us to help you.
Find that chunk of code also.
It may or may not be in the
code you already provided.
True: Interface has more than one meaning in java.
By 'interface', in this case, I mean
the entire route your actions and
Your mouseClicked action is the major
part of it, and your array list makes it
practically complete, but I'm left with
the suspicion that the array list were
never "instanciated" (created and
assigned a reference).
And you are calling me on some details
I must admit, I have been sloppy at
wording, I'll try to be a bit more careful.
ArrayList<ArrayList<Edge>> edges; does look redundant
seems good enough, UNLESS you
expect to create families of
compound edges, which would
represent many objects on the display.
If that's the case, I would ditch
any work on the
for now, and just concentrate on
producing and rendering the