Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default drawing points with mouse

    I need to be able to draw a point with the mouse every time a user clicks in the applet window. It needs to display the Point X (1, 2, 3) and the coordinates. I can't figure how to how to do that. Here is my code, it is currently setup to draw 2 points and connect a line between them. It displays all the information, I just can't figure out how to do it an unlimited amount of times. Thanks

    Java Code:
    import java.awt.*;
    import java.applet.*;
    import javax.swing.JFrame;
    import javax.swing.JApplet;
    import javax.swing.JLabel;
    import java.awt.BorderLayout;
    import javax.swing.JPanel;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseMotionAdapter;
    
    public class polygon extends JApplet {
    	
    	private int pointCount = 0; // count number of points
    
       	// array of 2 java.awt.Point references
       	private Point points[] = new Point[ 2 ];  
       
      	private boolean p0Exists = false; 
      	private boolean p1Exists = false; 
      	private int r = 8;
    	JLabel label1;
    	JLabel label2;
    	
    	public void init() {
    		
            JPanel p = new JPanel();
            
            label1 = new JLabel("Click several times to create a polygon. Finish by clicking on Point 0.");
            label1.setBackground(Color.CYAN);
            label1.setOpaque(true);
            label1.setVisible(true);
            label1.setPreferredSize(new Dimension(400,20));
    
            p.setLayout(new FlowLayout());  
            p.add(label1);
            
            Container content = getContentPane();
            content.setLayout(new BorderLayout()); // Used to center the panel
            content.add(p, BorderLayout.SOUTH);
            
    		PaintPanel paintPanel = new PaintPanel();
    		content.add(paintPanel, BorderLayout.CENTER);
    		
    		
    	}
    
    	public class PaintPanel extends JPanel
    	{
    
    	// set up GUI and register mouse event handler
    	public PaintPanel()
    	{
    		super();
    		this.setBackground( Color.LIGHT_GRAY );
    		
    		// handle frame mouse  event
    		addMouseListener(
    		
    			new MouseAdapter() // anonymous inner class
    			{  
    				// define a point and repaint
    				public void mousePressed( MouseEvent event )
    				{
    					if ( !p0Exists ) 
    					{
    						// create point0
    						points[ 0 ] = new Point( event.getX(), event.getY() ); 
    						System.out.println("Point 0 created: " + points[ 0 ] );
    						p0Exists = true;
    						repaint();	
    					} else if ( !p1Exists ) 
    					{
    						// create point1
    						points[ 1 ] = new Point( event.getX(), event.getY() ); 
    						System.out.println("Point 1 created: " + points[ 1 ] );
    						p1Exists = true;
    						repaint();	
    					}
    				
    				} // end method mousePressed
    				
    			} // end anonymous inner class
    		); // end call to addMouseMotionListener
    	} // end PaintPanel constructor
    	
    		public void paint( Graphics g )
    		{
    		super.paint( g ); // clears drawing area
    		if ( p0Exists ) 
    		{
    			g.drawOval( points[ 0 ].x - r/2, points[ 0 ].y - r/2, r, r );
    			g.drawString("Point0", points[ 0 ].x, points[ 0 ].y - 3*r );
    			g.drawString("x=" + points[ 0 ].x + " y=" + points[ 0 ].y, 
    										points[ 0 ].x, points[ 0 ].y - r );
    			//g.drawLine(points[ 0 ].x - r/2, points[ 0 ].y - r/2, r, r);
    		}
    		if ( p1Exists ) 
    		{
    			g.drawOval( points[ 1 ].x - r/2, points[ 1 ].y - r/2, r, r );
    			g.drawString("Point1", points[ 1 ].x, points[ 1 ].y - 3*r );
    			g.drawString("x=" + points[ 1 ].x + " y=" + points[ 1 ].y, 
    										points[ 1 ].x, points[ 1 ].y - r );
    		}
    		if ( p0Exists && p1Exists)
    		{
    			g.drawLine(points[ 0 ].x, points[ 0 ].y, points[ 1 ].x, points[ 1 ].y);
    		}
    	} // end method paint
    }
    
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The code you posted is similar (down to the r=8) to code on teh internet and in D&D etc. Do you understand it? I don't mean to be rude, but if you don't then that's where to start: by figuring out how this code works. Then you can start writing your own.

    You want to draw a polygon, so why do you have

    Java Code:
    private Point points[] = new Point[ 2 ];
    which initialises a points array capable of describing at most two points?

  3. #3
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    Yes, I understand it. I know what it does. The points array has since been removed as part of the requirements. I am using two arrays of 100 to get the x and y values. I just need to know how to create a point every time the mouse is pressed.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The array thing was - of course - just an example. Your code (or at least the code you posted, as you say your code is now something else) also has the p0/1Exists variables which determine what the painting does.

    Again having a pair of such variables might make sense for some other problem, but seems beside the point in drawing a polygon.

    Anyway, I'm glad you understood the code you posted. The point is now to write your own and post questions about it if they arise.

  5. #5
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default

    I'm not sure why your not willing to help. maybe you need to talk to my instructor because he is the one to told us to take the sample code and modify it. BTW, I have used this same sample code to make 2 other programs, that is just the way the class is. Anyone else willing to help?

Similar Threads

  1. Drawing points on a JPanel
    By josephdcoleman in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 04:47 PM
  2. Percentage/decimal points?
    By Exhonour in forum New To Java
    Replies: 6
    Last Post: 01-16-2009, 11:35 PM
  3. Replies: 2
    Last Post: 08-24-2008, 01:30 AM
  4. Replies: 0
    Last Post: 06-28-2008, 10:25 PM
  5. Replies: 6
    Last Post: 06-12-2008, 10:14 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
  •