Need some Help


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Need some Help

  1. #1
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Exclamation Need some Help

    I have to write an application that alows the user to draw a rectangle by simply dragging the mouse. It also has to display the area of the rectangle on the bottom. I have done that, but there seems to be something wrong with it. Sometime the rectangle will show up in the right spot but half of the time it seems to be in the wrong place. Another thing that happens is that if the mouse is clicked and it is released south west of the click point the rectangle also shows up in the wrong spot. Does anyone know what I can do to fix this ?

    Thanks in advance

    Here is my code:

    Code:
    import java.awt.*;
     import javax.swing.*;
     import java.awt.event.*;
    
     class Painter
     {
     	int x1, x2, y1, y2;
    
     	public Painter()
     	{
     		Frame main = new Frame();
     		final JLabel lblArea = new JLabel("Area :");
     		main.setLayout(new BorderLayout());
    
     		main.add(lblArea,BorderLayout.SOUTH);
    
     		final Canvas pad = new Canvas(){
    	   		public void paint(Graphics g){
    
    			g.drawRect(x1,y2,x2,y2);
    			lblArea.setText("Area : "+(x1+x2)*(y1+y2)+ " pixels");
    		    }
    		};
    
    		pad.addMouseListener(new MouseAdapter(){
    	   		public void mousePressed(MouseEvent e)
    	   		{
    				x1 = e.getX(); y1 = e.getY();
    	   		}
    
    	   		public void mouseReleased(MouseEvent e)
    	   		{
    				x2 = e.getX(); y2 = e.getY();
    				pad.repaint();
    	    	}
    		});
    
    		main.addWindowListener(new WindowAdapter(){
    	   		public void windowClosing(WindowEvent e)
    	   		{
    	   			System.exit(0);
    	   		}
    		});
    
    		main.add(pad);
    		main.pack();
    		main.setSize(100,200);
    		main.setVisible(true);
    
     	}
    
    
    
     	public static void main(String args[])
     	{
    		Painter paint = new Painter();
     	}
     }

  2. #2
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    One thing you could do is put some comments in your program describing what its doing.
    For example what are the variables x1 and x2 used for?

    Remember what the parameters for drawRect are: the upper left corner!
    What happens if the mouse moves up or to the left? You code only works for the mouse going down and right!

    Add some println() statements to the code so that you can see what is happening. Display the x,y points where the mouse is pressed and released. The output from this should help you see what is happening.

    Good luck.
    Last edited by Norm; 09-09-2005 at 10:56 AM.

  3. #3
    Join Date
    Oct 2004
    Location
    Chicago
    Posts
    108

    Thumbs up

    Thanks Norm, I wrote this very late at night and had no idea what was going on with the code. Thanks for the tips,

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