Looping in Applet

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 38
Reputation: cproud21 has a little shameless behaviour in the past 
Solved Threads: 0
cproud21 cproud21 is offline Offline
Light Poster

Looping in Applet

 
0
  #1
Dec 8th, 2008
I am to design an applet that produces a six sided polygon in which the coordinates are set according to the location of the mouse when it is clicked. I am struggling setting each of the clicks to the correct point. I have tried many things, however have not been able to come up with a working idea. I have set up a while loop in my mouseListener, and am not sure what I am missing. If you can help, I would appreciate it.

I thank you in advance for your help, here is my code...

package hw14;

import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseListener;

public class Main extends JApplet
{
    private int x1, x2, x3, x4, x5, x6;
    private int y1, y2, y3, y4, y5, y6;
    int [] xC = {x1,x2,x3,x4,x5,x6};
      int [] yC = {y1,y2,y3,y4,y5,y6};
    
    boolean mouseClicked = false;
        
   public void init()
   {
      getContentPane().setBackground(Color.WHITE);  
      addMouseListener(new MyMouseListener());
   }
   
   public void paint(Graphics g)
   {
       
      
      
      super.paint(g);
      g.setColor(Color.BLACK);
      g.drawPolygon(xC,yC,6);
      
      
      if(mouseClicked== true)
      {
          
      g.setColor(Color.BLACK);
      g.drawPolygon(xC,yC,6);
      
      }
   
   
   }
   
   
   private class MyMouseListener implements MouseListener
   {
        
       public void mousePressed(MouseEvent e)
       {
           
       }

     
        public void mouseClicked(MouseEvent e) {
           
        
        int x = e.getX();
        int y = e.getY();
        
        int i= 0;
        int j =0;
        int r = 0;
        
        while (i <= 5)
        {
            xC[r] = x;
            yC[j] = y;
           
            
            r++;
            j++;
            i++;
            
           
        }

        
        mouseClicked = true;
        repaint();
        
        }

        public void mouseReleased(MouseEvent e) {
           
        }

        public void mouseEntered(MouseEvent e) {
           
        }

        public void mouseExited(MouseEvent e) {
            
        }
   }
   
   }
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,488
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 517
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Looping in Applet

 
1
  #2
Dec 8th, 2008
You don't want a while() loop in the mouseListener. You want to increment a counter each time a point is collected until you have recorded the required number of points for your polygon. You may only want to draw the points themselves until you have the entire polygon specified. You'll also need some way to reset it. Perhaps a seventh click or a right-click should clear the points and reset the counter.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC