Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Unhappy get the min X and min Y of an image drawn on the window

    (in Java)using drawLine function i drew an image in black on (600,600) window.
    now i wish to know the min value of X and Y co-ordinate of the image i drew and display it on the same window as soon i release my mouse button..
    plzzz help me...m using net beans..
    Last edited by bizzareshishir; 01-22-2011 at 11:55 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    How are you doing the drawing to begin with? Are you creating and filling an ArrayList of Point objects and then iterating through this list in a JPanel's paintComponent method? Or are you drawing in a BufferedImage object that is displayed? Regardless, you get x and y points in the MouseListener, so keep track of them and save the lowest values.

  3. #3
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Post

    this is how i m drwaing...
    .
    .
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    
    class anyshapekeborderpeovla extends Frame implements MouseListener,MouseMotionListener
    { 
      BasicStroke b=new BasicStroke(4);
      int x,y,oldx,oldy;
      
      Color c=Color.BLACK;
      anyshapekeborderpeovla()
      {
       addMouseListener(this);
       addMouseMotionListener(this);
       setSize(600,600);
       setVisible(true);
      }
    
      
      public  void mousePressed(MouseEvent e)
       {
        oldx= e.getX();
        oldy= e.getY();
       }
      public void mouseDragged(MouseEvent e)
       {
        int x= e.getX();
        int y= e.getY();
        Graphics g= getGraphics();
    	
        Graphics2D g2=(Graphics2D)g;
        g.setColor(c);
        g2.setStroke(b);
        g.drawLine(oldx,oldy,x,y);
        oldx= x;
        oldy= y;
    	//System.out.println(oldx+":"+oldy);
    	
    	
       }
       public void mouseMoved(MouseEvent e)
       {
       
       }
       public void mouseClicked(MouseEvent e)
       {}
       public void mouseReleased(MouseEvent e)
       {
         
    	 
    	 
    	 
    	 
         	 
         
       
       }
       public void mouseExited(MouseEvent e)
       {}
       public void mouseEntered(MouseEvent e)
       {}
      
          public static void main(String arg[])
           {
             new anyshapekeborderpeovla();
           }
    
     }
    Moderator edit: code tags added
    Last edited by Fubarable; 01-22-2011 at 01:00 PM. Reason: Moderator edit: code tags added

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't know how to draw in AWT, so I can't advise you on the drawing itself, but as I noted above, you are getting your x and y values in your MouseListener:

    Java Code:
      public void mouseDragged(MouseEvent e)
       {
        int x= e.getX();
        int y= e.getY();

    So it should be trivial to give the class two int values, minX and minY and check if the current x and y are lower than the current minX and minY respectively, and if so, reset the min values. I'm sure you've done this sort of program prior to doing GUI programming and here it's no different.

    Also, I've added code tags to your post to help make the code more readable. To learn how to do this yourself, please read the link in my signature below.

    Luck.

Similar Threads

  1. How to clear Rectangle drawn using GC
    By Tyler in forum SWT / JFace
    Replies: 0
    Last Post: 06-24-2010, 10:51 AM
  2. piechart not being drawn on a panel
    By dataprofiling in forum Java 2D
    Replies: 1
    Last Post: 03-27-2010, 11:33 AM
  3. [newbie] Rectangle2D not drawn within applet
    By jon80 in forum New To Java
    Replies: 7
    Last Post: 06-06-2009, 09:43 PM
  4. Canvas not being drawn
    By tacosc in forum Java Applets
    Replies: 4
    Last Post: 03-29-2009, 10:11 AM
  5. How do erase image drawn
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 05:26 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
  •