Results 1 to 7 of 7
  1. #1
    B Green is offline Member
    Join Date
    Jan 2017
    Posts
    3
    Rep Power
    0

    Question Simple graphics interactivity

    I am trying to write a GraphicsProgram that allows the user to draw lines on the canvas. Pressing the mouse button sets the starting point for the line. Dragging the mouse moves the other endpoint around as the drag proceeds. Releasing the mouse fixes the line in its current position and gets ready to start a new line.

    Can someone explain why when I run the code the lines are not able to be displayed, and what about the correct code, which I have also attached, makes it preferable.

    My
    Java Code:
    import acm.program.*;
    import java.awt.event.MouseEvent;
    import acm.graphics.*;
    
    public class DrawLines extends GraphicsProgram{
    
    public void init(){
        addMouseListeners();
        line=new GLine(x1,y1,x2,y2);
    }
    
    public void mousePressed(MouseEvent e){
        x1=e.getX();
        y1=e.getY();
    }
    public void mouseDragged(MouseEvent e){
        x2=e.getX();
        y2=e.getY();
        add(line);
    }
    
    private GLine line;
    private int x1;
    private int y1;
    private int x2;
    private int y2; 
    
    }
    Correct
    Java Code:
    import acm.graphics.*; 
    import acm.program.*; 
    import java.awt.event.*; 
    
    /** This class allows users to drag lines on the canvas */ 
    public class RubberBanding extends GraphicsProgram { 
       public void run() { 
          addMouseListeners(); 
       } 
    
    /** Called on mouse press to create a new line */ 
       public void mousePressed(MouseEvent e) { 
          double x = e.getX(); 
          double y = e.getY(); 
          line = new GLine(x, y, x, y); 
          add(line); 
       } 
    /** Called on mouse drag to reset the endpoint */ 
       public void mouseDragged(MouseEvent e) { 
          double x = e.getX(); 
          double y = e.getY(); 
          line.setEndPoint(x, y); 
       } 
    
    /* Private instance variables */ 
       private GLine line; 
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Simple graphics interactivity

    Not everyone is familiar with the acm packages classes.
    Is there a website the provides support for the acm packages users?

    why when I run the code the lines are not able to be displayed
    Have you tried debugging the code by adding lots of print statements that show where the code is executing and the values of the variables that are used?


    when I run the code the lines are not able to be displayed,
    Look at the code that works and compare the methods it uses to control the drawing of lines to your code.
    Last edited by Norm; 01-14-2017 at 07:48 PM.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    B Green is offline Member
    Join Date
    Jan 2017
    Posts
    3
    Rep Power
    0

    Default Re: Simple graphics interactivity

    Hi Norm,

    acm.program.* supports the GraphicsProgram screen
    import java.awt.event.MouseEvent supports the user interactivity
    import acm.graphics.* supports the creation of a GObject line which is created on the GraphicsProgram screem

    Does this answer your question?

    Have you tried debugging the code by adding lots of print statements
    I have tried debugging. My program seems to be storing all the correct values of the coordinates, but does not display the line.
    Last edited by B Green; 01-14-2017 at 08:40 PM.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Simple graphics interactivity

    but does not display the line.
    Where is the line given its location to be displayed?
    Remember that a constructor copies the values of the variables when it is called. If the variables' values are changed later, the constructor will not see the new values.

    Did you do this>
    Look at the code that works and compare the methods it uses to control the drawing of lines to your code.
    What is different?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Simple graphics interactivity

    Imho, you would be better off just using the Java API to draw the graphics and forget about the acm package. It would actually make your program more portable.

    However, you add lines to something as they are created. But where or how are those lines supposed to be redrawn? Normally, in standards Java you would have to iterate over the "list" of lines to redraw them within a graphics context.

    Regards,
    Jim
    Last edited by jim829; 01-14-2017 at 08:16 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    B Green is offline Member
    Join Date
    Jan 2017
    Posts
    3
    Rep Power
    0

    Default Re: Simple graphics interactivity

    Remember that a constructor copies the values of the variables when it is called. If the variables' values are changed later, the constructor will not see the new values.
    This was the problem. Thanks!

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Simple graphics interactivity

    Yes, that was part of it. The working code shows you what needs to be done and where to do it.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Simple Graphics
    By shodai in forum New To Java
    Replies: 6
    Last Post: 02-07-2014, 12:51 AM
  2. Simple Graphics program not working.
    By forms in forum New To Java
    Replies: 7
    Last Post: 03-11-2012, 03:44 PM
  3. Drawing simple graphics in NetBeans
    By arifin in forum AWT / Swing
    Replies: 1
    Last Post: 11-12-2010, 10:01 AM
  4. Simple Graphics
    By Freakzoyd in forum New To Java
    Replies: 6
    Last Post: 10-05-2010, 02:47 AM
  5. interactivity????/
    By aamiritsu in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 10-29-2007, 02:40 PM

Tags for this Thread

Posting Permissions

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