help with lines


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: help with lines

  1. #1
    Join Date
    Mar 2003
    Posts
    3

    help with lines

    i am tyring to get a line to re-draw from where the mouse is clicked. the *** in my code is where i am trying to redraw the line but i cant find the right code to make it work. Thanks in advance for any help.
    Code:
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.*;
    import java.awt.geom.Line2D;
    
    public class MouseApplet extends Applet{
    
      public MouseApplet(){
    
        class MousePressListener implements MouseListener{
    
          public void mousePressed(MouseEvent event){
            int x = event.getX();
            int y = event.getY();
            ***
            repaint();
          }
    
          public void mouseReleased(MouseEvent event){}
          public void mouseClicked(MouseEvent event){}
          public void mouseEntered(MouseEvent event){}
          public void mouseExited(MouseEvent event){}
        }
    
        MouseListener listener = new MousePressListener();
        addMouseListener(listener);
      }
    
      public void paint(Graphics g){
        Line2D.Double jimsline = new Line2D.Double(x,y,x2,y2);
        Graphics2D g2 = (Graphics2D)g;
        g2.draw(jimsline);
      }
    
      private Line2D.Double jimsline;
      private static final int x = 100;
      private static final int y = 100;
      private static final int x2 = 20;
      private static final int y2 = 30;
    }
    [ArchAngel added CODE tags and indented code]

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    I'm assuming that you're trying to overwrite the x and y class values in the following code:
    Code:
    public void mousePressed(MouseEvent event){
      int x = event.getX();
      int y = event.getY();
      ...
    However, you're not doing this as you've declared these 'x' and 'y' variables, thereby making them *local* variables. Also you class variables 'x' and 'y' are declared as final:
    Code:
    private static final int x = 100;
    private static final int y = 100;
    Lastly, I would suggest you make 'MousePressListener' extend 'MouseAdapter', that way you don't need to declare all those unused methods:
    Code:
    public void mouseReleased(MouseEvent event){}
    public void mouseClicked(MouseEvent event){}
    public void mouseEntered(MouseEvent event){}
    public void mouseExited(MouseEvent event){}
    ArchAngel.
    O:-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center