Results 1 to 7 of 7
  1. #1
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default 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.

    notes:
    createCompoundEdge and finishedCompoundEdge are just booleans which decide when to start and finish the compound edge.

    right now I have two array lists:
    ArrayList<Edge> compound;
    ArrayList<ArrayList<Edge>> edges;

    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.

    Java Code:
    public void mouseClicked(MouseEvent event)
             {
                 mouseX = event.getX();
                 mouseY = event.getY();
                 
                 if (createCompoundEdge && event.getButton() == event.BUTTON1)
                {   
                    line = new Edge();
                    line.setStartPoint(mouseX, mouseY);
                    x2 = event.getX();
                    y2 = event.getY();
                    line.setEndPoint(x2, y2);
                    compound.add(line);
                    while (!finishedCompoundEdge)
                    {
                        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));
                        
                }
                edges.add(compound);
                createCompoundEdge = false;
                finishedCompoundEdge = false;
            }
                repaint();
                }
    Edge class is pretty much a line.2D object.
    Java Code:
    import java.awt.geom.*;
    import java.awt.Graphics2D;
    
    public class Edge extends GraphElement
    {
      private double xEndPos, yEndPos;
      private Line2D.Double displayLine;
      private static final double THRESHOLD_DIST =  3.0;  
      
      Edge()
      {
        super();
        displayLine = new Line2D.Double();
        displayLine.x1 = displayLine.y1 = displayLine.x2 = displayLine.y2 = 0; 
      }
      
      Edge(double xs, double ys, double xe, double ye)
      {
        moveTo(xs,ys);
        setEndPoint(xe,ye);
      }
      
      public void setStartPoint(double xStart, double yStart)
      {
        moveTo(xStart, 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()
      {
        return false;
      }
      
      public boolean applyImage()
      {
        return false;
      }
        
      public double getX1Point()
      {
          return displayLine.x1;
      }
        
      public double getY1Point()
      {
          return displayLine.y1;
        }
        
      public double getX2Point()
      {
          return xEndPos;
      }
      
      public double getY2Point()
      {
          return yEndPos;
        }
        
      boolean isSelected(double x, double y)
      {
        if (displayLine == null)
          return false;
        double dist = displayLine.ptSegDist(x,y);
        if (dist < THRESHOLD_DIST)
          return true;
        return false;  
      }
      
      public void draw(Graphics2D g2)
      {
        if (displayLine != null)  
          g2.draw(displayLine);
      }
    }
    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.

  2. #2
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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?
    Last edited by paul pasciak; 04-05-2009 at 11:32 PM. Reason: update less accurate request

  3. #3
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default

    Quote Originally Posted by paul pasciak View Post
    I can't judge this code too well.
    It seems ok.

    How is the interface for the
    array lists implemented?
    um not sure what you mean by interface, are you asking where the array lists are declared?

  4. #4
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default 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.
    Last edited by paul pasciak; 04-06-2009 at 12:02 AM. Reason: added the word 'already' to the last line

  5. #5
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default True: Interface has more than one meaning in java.

    By 'interface', in this case, I mean
    the entire route your actions and
    data take.

    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.
    Last edited by paul pasciak; 04-05-2009 at 11:48 PM.

  6. #6
    busdude is offline Member
    Join Date
    Oct 2008
    Posts
    25
    Rep Power
    0

    Default

    I looked at the line number and it appears the error occurs in the while loop, or near it. i think it might be in the line where i add a new line using the endpoint of previous one, something isn't right.

  7. #7
    paul pasciak is offline Senior Member
    Join Date
    Jul 2008
    Posts
    125
    Rep Power
    0

    Default ArrayList<ArrayList<Edge>> edges; does look redundant

    ArrayList<ArrayList<Edge>> edges;

    looks redundant.

    ArrayList<Edge> compound

    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

    ArrayList<ArrayList<Edge>> edges;

    for now, and just concentrate on
    producing and rendering the
    "compound" data.

Similar Threads

  1. Replies: 0
    Last Post: 12-22-2008, 07:16 PM
  2. how to deserialize multiple objects in a file
    By xcallmejudasx in forum Advanced Java
    Replies: 11
    Last Post: 12-16-2008, 06:29 PM
  3. Replies: 2
    Last Post: 07-18-2008, 02:13 PM
  4. Can I store multiple objects in an array
    By lareauk in forum New To Java
    Replies: 9
    Last Post: 05-29-2008, 04:57 AM
  5. Can I use vectors to store multiple types of objects
    By Nathand in forum Advanced Java
    Replies: 6
    Last Post: 04-28-2008, 08:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •