Results 1 to 3 of 3
  1. #1
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default cant return value from a method

    hey,

    i got a class called viewPanel extends JPanel it has two methods and paint component method.
    paint component method draws two points on to the screen. points coordinates are received from an array and the drawn.

    first method randomPoints() generates random coordinates and paint component draws those coordinates on to the screen

    avrage speed method calculates avrage speed that takes from my array coordinates to colide with the random coordinates

    ViewPanel extends JPanel
    Java Code:
     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);
     
     
    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];
     
     
                         getAvrageSpeed();
                         xb =  Math.round((int) getWidth() * xCoordinates[1] / 1024);
                         yb = getHeight()- Math.round((int) getHeight() * yCoordinates[1] / 768);
                         g2.drawOval(xb,yb,10,10);
                         g2.drawString("avrgspeed" + avrgSpeed, 100,200);
                         i++;
    		}
    		g.drawImage(mImage, 0, 0, null);

    now i have a different JPanel called ControlsPanel extends JPanel which has a button and once the button is clicked i would like to catch the avrage speed from getAvrageSpeed() method in viewPanel and display it as a label

    Java Code:
     private void avrageSpeedMousePressed(MouseEvent evt) {   
    ViewPanel vp = new ViewPanel();                                         
                    if(avrageSpeedButton.isEnabled()){
                       labelAvrageSpeed.setText(""+vp.getAvrageSpeed);
                         
                    }

    once i do this it just gives me zero what ever i do even tho i know it is not zero

    please somebody help this is driving me mad.

    any help will help me :)

    thank you

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,782
    Rep Power
    7

    Default

    Why are you creating a new ViewPanel every time the button is clicked? I think you should have a ViewPanel that has been created elsewhere and it is this panel that has the actual data in it and you should be calling the get method on that panel.

  3. #3
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default

    thanks for your reply i tried creating viewPanel as a global variable but once the button is clicked i still get nothing.
    i found some sites that had similar problems they were talking about multi-threading im not sure is that the case but if it is i am not familiar with multi-threading and i wouldn't know how to implement it.

    p.s i have to call that method on the controlsPanle because later on that data will be stored in database with the other data i cant call it on viewPanel


    so would you have more suggestions how to solve this issue please
    thanks you

Similar Threads

  1. how to return a value from runnable method
    By udinulis in forum AWT / Swing
    Replies: 10
    Last Post: 07-20-2010, 05:06 AM
  2. Not able to return the method value
    By dmakshay2002 in forum Advanced Java
    Replies: 11
    Last Post: 05-28-2010, 02:07 PM
  3. Method won't return value
    By footyvino in forum New To Java
    Replies: 2
    Last Post: 03-26-2010, 10:49 AM
  4. Replies: 3
    Last Post: 10-15-2009, 01:02 PM
  5. Return value of method
    By cachi in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 08:23 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
  •