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

    Default Drawing points on a JPanel

    I have created an applet with a Paint Panel application. It is creating the points, but the paint method is not drawing the points on the JPanel. Any Ideas?

    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 SampleApplet 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("1. Click in two locations");
            label1.setBackground(Color.CYAN);
            label1.setOpaque(true);
            label1.setVisible(true);
            label1.setPreferredSize(new Dimension(150,20));
            
            label2 = new JLabel("2. Drag the points");
            label2.setBackground(Color.YELLOW);
            label2.setOpaque(true);
            label2.setVisible(true);
            label2.setPreferredSize(new Dimension(150,20));
    
            p.setLayout(new FlowLayout());  
            p.add(label1);
            p.add(label2);
    
    
            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.ORANGE );
    		
    		// 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
    		
    		// handle frame mouse motion event
    		addMouseMotionListener(
    		
    			new MouseMotionAdapter() // anonymous inner class
    			{  
    				// drag the point and repaint
    				public void mouseDragged( MouseEvent event )
    				{
    					//System.out.println("Mouse dragged: x=" + event.getX() + "  y=" + event.getY() );
    					if ( p0Exists ) 
    					{
    						if ( Math.abs(points[ 0 ].x - event.getX()) < r 
    							&& Math.abs(points[ 0 ].y - event.getY()) < r ) 
    						{
    							// set new coordinates for point0
    							points[ 0 ] = new Point( event.getX(), event.getY() ); 
    							repaint();	
    						}
    					} 
    					if ( p1Exists ) 
    					{
    						if ( Math.abs(points[ 1 ].x - event.getX()) < r 
    							&& Math.abs(points[ 1 ].y - event.getY()) < r ) 
    						{
    							// set new coordinates for point1
    							points[ 1 ] = new Point( event.getX(), event.getY() ); 
    							repaint();	
    						}
    					}					
    				} // end method mouseDragged
    			} // 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 );
    		}
    		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 );
    		}
    	} // end method paint
    }

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

    Default

    Yes, I know exactly what your problem is and what the solution is. but I have deleted the answer until you respond to your previous posts. Why help if you'll likely just ignore this post too?
    Last edited by Fubarable; 02-25-2009 at 04:57 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,192
    Rep Power
    19

    Default

    Thanks for the heads-up.

    db

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

    Default

    I'm Sorry if you feel that I was not taking your advice. It seems like you are jumping to conclusions a little quickly. I also did not intend on posting the same issue twice, that was a mistake on my part. As far as a response to those post, I decided to not use a JFrame so far since it was not a requirement. I was able to get the applet up with labels and my PaintPanel, so I have gotten farther than before.

    I like this forum and do not want to make a bad impression, but it seems like pretty harsh reaction towards a new forum member and a person very new to programming in general.

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

    Default

    Thanks for the response. We're talking basic common courtesy here. If someone takes effort to give you help, especially if they are volunteers, the least you can do is somehow acknowledge that help with a simple note, especially before asking a new question. I don't think that this is being too harsh, and I don't think that it's asking too much.

    To your problem: your paint method is held within your JApplet class not within the inner class that extends JPanel. Thus calls that ask the JPanel to repaint will do nothing.

    Actually you shouldn't be overriding paint here but paintComponent, and this method should be held inside of the inner class, the one that extends JPanel, the one that is being painted. Best of luck.

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

    Default

    I agree it is probably not really that harsh(it was early morning when I read this), I just don't use forums that much, but am starting to do so now, so I will try and be more courteous in the future.

    I did move my paint method inside my inner class and now it works. All I have to do now is draw a line to connect the points. I do appreciate the help. Thanks.

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

    Default

    Glad it helped, but again, override paintComponent, not paint, not unless your painting also includes changes to the painting of component children and borders (yours doesn't, and in fact 95% of these types of methods usually don't).

Similar Threads

  1. Looking for help on drawing stuff in a jPanel
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 08-28-2009, 06:00 PM
  2. Percentage/decimal points?
    By Exhonour in forum New To Java
    Replies: 6
    Last Post: 01-16-2009, 10:35 PM
  3. Replies: 2
    Last Post: 08-24-2008, 12:30 AM
  4. Replies: 0
    Last Post: 06-28-2008, 09:25 PM
  5. X&Y Coordinate Drawing on jPanel
    By BHCluster in forum Java 2D
    Replies: 2
    Last Post: 03-27-2008, 10:47 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
  •