hey,

i got a class called IRPanel this class contains a few methods that handles what is drawn on it.

randomPoints() is a method that generates random values for x and y coordinates for random point. new random point coordinates are generated only when the coordinates from IREvent() method match the random point coordinates

getAvrageSpeed() calculates avrage speed it takes IREvent() generated coordinates to reach random point coordinates.

onIRevent() generates coordinates that are drawn by paint component.

public class IRPanel extends JPanel implements myListener {

	private static int MAX_NB_POINTS = 4;
	private Color color = Color.WHITE;
	private Color backgroundColor = Color.BLUE;
	private Color borderColor = Color.RED;
	private Shape shape;
  
     
	private Image mImage;// image for double buffering]
        private Timer timer;
        private Timer timer2;
	private int[] xCoordinates;
	private int[] yCoordinates;
        private int[] size;
	private int nbPoints = -1;
        private boolean hasColaborated =true;
        private boolean sorted =false;
        private double radians;
        private int degrees;
        private double hypotenuse;
        private double adjacent;
        private double oposite;
        private int distance2;
        private int speed;
        private int xa;// shoulder cordinates
        private int ya;
        private int yb; //arm coordinates
        private int xb;
        private double avrgTime =0;
        private int second =1000;
        int oldRandomX = 0;
        int oldRandomY = 0;
        int randomX = 300;
        int randomY = 100;
        int oldX;
        int oldY;
        int zz=1;
        int avrgSpeed =0;
        int avrg =0;

	public IRPanel() {        
		shape = new Ellipse2D.Double(0, 0, 10, 10);
		initArrays();
                timer2 = new Timer( second, new TimerCatcher2() ) ;
                timer2 . start() ;
               
	}
//array does not exceed its limit otherwise value is decreemented
	private void initArrays() {
		xCoordinates = new int[MAX_NB_POINTS];
		yCoordinates = new int[MAX_NB_POINTS];
                size=new int[MAX_NB_POINTS];
		for (int i = 0; i < MAX_NB_POINTS; i++) {
			xCoordinates[i] = -1;
			yCoordinates[i] = -1;
                        size[i]= -1;
		}
	}

        public void randomPoints(){
             if((randomX <= xb && randomX+15 >= xb)&&(randomY <= yb && randomY+15 >= yb)){
                    randomX = 200 +(int)(Math.random() * ((350 - 200)+1));
                    randomY = 50 + (int)(Math.random()* ((200 - 50)+1));
                    ++zz;

                }           
        }

        public int getAvrageSpeed(){
            distance2 = (int) Math.sqrt(Math.pow(oldRandomX-randomX,2) + Math.pow(oldRandomY-randomY,2));
            speed =  (int) (distance2 / avrgTime);
            
            if((randomX <= xb && randomX+15 >= xb)&&(randomY <= yb && randomY+15 >= yb)){              
                avrg = (speed+avrg);
                avrgSpeed = avrg /zz;
                oldRandomX = randomX;
                oldRandomY =randomY;
                avrgTime=0.0;
            }
            
            return  avrgSpeed;
            

        }

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Dimension d = getSize();
		checkOffScreenImage();
		Graphics offG = mImage.getGraphics();
		offG.setColor(backgroundColor);
		offG.fillRect(0, 0, d.width, d.height);
               
		Graphics2D g2 = (Graphics2D) mImage.getGraphics();
                Graphics2D g1= (Graphics2D) mImage.getGraphics();
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);


                 
/*!!*/          randomPoints();




           
		int i=0;
		while (i < nbPoints) {
			int x = xCoordinates[i];
			int y = yCoordinates[i];
//change x and y coordinates start point 
                        int xx = Math.round((int) getWidth() * x / 1024);
			int yy = getHeight()- Math.round((int) getHeight() * y / 768);
                        g2.translate(xx, yy);

//each point is set to varibles
                         xa =  Math.round((int) getWidth() * xCoordinates[0] / 1024);
                         ya = getHeight()- Math.round((int) getHeight() * yCoordinates[0] / 768);
                         xb =  Math.round((int) getWidth() * xCoordinates[1] / 1024);
                         yb = getHeight()- Math.round((int) getHeight() * yCoordinates[1] / 768);
  
                         g1.drawString("X coord :" +xa+" Y coords :"+ya+" X2 Coords :"+xb+" Y2 coord :"+ yb,50,25);
                         g1.setColor(color.WHITE);
                         g1.drawLine(xa, ya, xb, yb);
                         g1.drawLine(xa,ya,xa,ya+50);                                              
                         g1.drawString(" Shoulder",xa,ya);


                         getAvrageSpeed();
                         g2.setPaint(borderColor);
                         g2.draw(shape);
                         g2.fill(shape);
                         g2.setPaint(color);
                         g2.setTransform(new AffineTransform());
                         g2.drawString("random dis"+distance2, 100,180);
                         g2.drawString(" in "+zz,100,160);
                         g2.drawString("avrgspeed" + avrgSpeed, 100,200);
                         g2.drawString("total : "+avrg,100,220);
                         g2.drawString("speed : "+speed,100,240);
                         g2.fillOval(randomX+5, randomY+5, 10, 10);
                         g2.setColor(Color.RED);
                         g2.drawOval(randomX+5, randomY+5, 10, 10);
                         i++;
		}
		g.drawImage(mImage, 0, 0, null);               
	}




	private void checkOffScreenImage() {
		Dimension d = getSize();
		if (mImage == null || mImage.getWidth(null) != d.width
				|| mImage.getHeight(null) != d.height) {
			mImage = createImage(d.width, d.height);
		}
	}
//this is where all the coordinates are set in to the array
	public void onIrEvent(IREvent arg0) {
		// transfer points
		IRSource[] points = arg0.getIRPoints();
		nbPoints = points.length;
		for (int i = 0; i < points.length; i++) {
			xCoordinates[i] = (int) points[i].getRx();
			yCoordinates[i] = (int) points[i].getRy();
                    
		}
		for (int i = points.length; i < MAX_NB_POINTS; i++) {
			xCoordinates[i] = -1;
			yCoordinates[i] = -1;
		}
		// redraw panel
           
             
		repaint();


	}


	public void onDisconnectionEvent(DisconnectionEvent arg0) {
		// clear previous points
		for (int i = 0; i < MAX_NB_POINTS; i++) {
			xCoordinates[i] = -1;
			yCoordinates[i] = -1;
                        size[i] =-1;
		}
		// redraw panel
		repaint();
	}

	public void clearView() {
           
		initArrays();
		repaint();
	}



    class TimerCatcher2 implements ActionListener
    {
        public void actionPerformed( ActionEvent e )
        {
           avrgTime++;
           
        }
    }

}

what i need to do is get the value returned from getAvrageSpeed() method
i have a different class that also extends JPanel and on the button click i would like to get the average speed value i tried something like this:

IRPanel ir = new IRPanel();
if(getAvrageSpeedButton.isEnabled()){
    label.setText(""+ir.getAvrageSpeed());
}

once i click that button i get nothing if i replace label.setText with System.out.println() it prints me zero

why cant i get that value????


please somebody help tell me what im doing wrong
i been stuck for so long ...


any help will be greatly appreciated

thank you

Recommended Answers

All 2 Replies

Start debugging this function...

public int getAvrageSpeed(){
            distance2 = (int) Math.sqrt(Math.pow(oldRandomX-randomX,2) + Math.pow(oldRandomY-randomY,2));
            speed =  (int) (distance2 / avrgTime);
 
            if((randomX <= xb && randomX+15 >= xb)&&(randomY <= yb && randomY+15 >= yb)){              
                avrg = (speed+avrg);
                avrgSpeed = avrg /zz;
                oldRandomX = randomX;
                oldRandomY =randomY;
                avrgTime=0.0;
            }
 
            return  avrgSpeed;
 
 
        }

Stick some print functions in there to display the variable values. Make sure they make sense. Stick some print statements in the "if" statement to see whether it is entered. Decide how that function SHOULD work and make sure it actually works that way. The function likely either has bad input or it is calculating incorrectly. Displaying the variable values should give you a good clue of where to go next.

well i am sure the getAvrageSpeed method does not have any bugs because i have tested it many times and also the value returned by this method is drawn by paintComponent

the problem occures when i tried calling it in my ViewPanel class. everytime i click the button which calls the getAvrageSpeed() method the output i get is zero even though i can see that the output printed by the paintComponent is a sensible value.

i think this could be a problem because the value returned by getAvrageSpeed() is constantly changing while the program is running

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.