Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    gedas is offline Member
    Join Date
    Feb 2010
    Posts
    51
    Rep Power
    0

    Default value returned is zero when it is not

    hey,
    i have one class with few methods, two of the methods creates a random number and returns it

    here is the example
    Java Code:
    public class Random extends JPanel{
    private int randomX=0;
    private int randomY=0;
    
    public Random(){
    
    }
    
    public int getRandomX(){
               randomX = 200 +(int)(Math.random() * ((350 - 200)+1));
               return randomX;
    }
    
    public int getRandomY(){
                randomY = 200 +(int)(Math.random() * ((350 - 200)+1));
                return randomY;
    }
                 
    public paintComponent(Graphics g){
    
    //some code.........
    
             g.drawOval(getRandomY(),getRandomY(),10,10);
    
    }
    
    }

    i have one more class where i try to get radomX and randomY value but it returns me zero even though i know its not zero.


    Java Code:
    public class ReturnValues extends JPanel{
    Random rd = new Random();
        private void getRandomMousePressed(MouseEvent evt) {                                            
                    if(getRandomButton.isEnabled()){
                          labelX.setText("x :"+rd.getRandomX());
                          labelY.setText("y :"+rd.getRandomY());
                    }
    
    }

    so once i press the button it just keeps on returning me zero but i know its not zero because i can see the point drawn on to my other panel which has a coordinates set to a random value


    any help will be greatly appreciated

    Thank you

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    remove these two from the top.... you don't need them:
    Java Code:
    private int randomX=0;
    private int randomY=0;

    just declare the 'int randomX' and 'int randomY' in the relevent methods

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    g.drawOval(getRandomY(),getRandomY(),10,10);

    should that be ...getRandomX(), getRandomY()... ?

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

    Default

    sorry i forgot to mention that there is one more line in both methods

    Java Code:
    public int getRandomY(){
    
           if(conditionIsTrue){//i forgot to add condition statment
                randomY = 200 +(int)(Math.random() * ((350 - 200)+1));
           }
           return randomY;
    }
    (i am not showing all the code because it is quite a huge program )
    thanks for your reply though :)
    i have removed the decleration of randomX and randomY to 0 in the top.

    but now i have to add it in the method


    Java Code:
    public int getRandomY(){
           randomX =0; //here 
           if(true){
                randomY = 200 +(int)(Math.random() * ((350 - 200)+1));
           }
           return randomY;
    }
    otherwise i get randomX may not have been initialized


    and still i get zero all the time when i call this method


    thank you

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

    Default

    @ozzyman
    hould that be ...getRandomX(), getRandomY()
    yeah there should be getRandomX() and getRandomY(), i wrote this by hand cos i didint want to copy and paste all the program it is quite large and confusing i just left the bits that are cosing me problems

  6. #6
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    it seems like your condition is never coming true.... what is the condition in the IF statement before randomX is set to a random number?

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

    Default

    the condition is simple when my mouse cursor coordinates reaches the random point coordinates new random point is drawn. which works but once i click the button to get the coordinates of my random point i still get zero

    thank you

  8. #8
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    okay but its very difficult to know what went wrong without looking at the code. it may be simple but there may also be an error popping out of it somewhere.

    - why does clicking the button stop you from getting correct random coordinates? theres something obviously wrong with the method used for the button.
    - if you take out the condition, do you get 0's still?

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

    Default

    without the condition it gives me the desired value, but i cant create new random point without the condition being true (i need the condition)

    thank you

  10. #10
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    so my point is, your condition is wrong, because it is failing to return TRUE when it should do, and thats why you are getting 0's when you are not supposed to.

    i can't help you further than that without seeing the condition

    condition FALSE --> randomX = 0
    condition TRUE --> randomX = desired value

    i'm guessing that clicking the button changes a value (possibly a counter) and the CONDITION depends on that value
    Last edited by ozzyman; 03-26-2011 at 07:02 PM.

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

    Default

    this is the condition

    Java Code:
    if((randomX <= xb && randomX+15 >= xb)&&(randomY <= yb && randomY+15 >= yb)){
    xb is my mouse coordinates
    i increased extra 15 pixels to the range so collision would be easier to establish

    so if my random point is equals to my mouse cursor new random point is created


    and in my other class once the button is clicked it should give me those coordinates that were randomly generated. so if my mouse overlaps the random point new point is generated and if i click the button it would show new point coordinates


    thanks a lot for all the help

  12. #12
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    and in my other class once the button is clicked it should give me those coordinates that were randomly generated.

    how does it get the coordinates?
    knowing that i made you take out the class variables at the top, you can't be storing the randomly generated numbers.

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

    Default

    well i said it should, but it doesnt get it now after class variables have been removed

  14. #14
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gedas View Post
    well i said it should, but it doesnt get it now after class variables have been removed
    Well, stick them back in again; I bet that your code doesn't even compile now. This thread wouldn't have been so long as it is if you'd mentioned the code for your condition test from the start.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    well everything compiles and i can make everything work like it did but my issue still persist and i am still confused how to solve it and i think i have given enough information in this thread to solve the issue im having.
    if i make those two variables as a class variables i still receive value zero
    i only receive value different from zero when the condition is removed but without the condition my program is not operational
    thank you

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gedas View Post
    well everything compiles and i can make everything work like it did but my issue still persist and i am still confused how to solve it and i think i have given enough information in this thread to solve the issue im having.
    if i make those two variables as a class variables i still receive value zero
    i only receive value different from zero when the condition is removed but without the condition my program is not operational
    thank you
    Well, we can take your word for it which would turn us into religeous beings or you could post all of your relevant code so there would be no need for anyone to guess what might be the cause.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

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

    Default

    ok here is the code for the entire class

    Java Code:
    package wiiusej.panels;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Line2D;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.QuadCurve2D;
    import javax.swing.*;
    import javax.swing.Timer;
    import java.lang.Math.*;
    import wiiusej.WiiUseApiManager;
    import wiiusej.values.IRSource;
    import wiiusej.wiiusejevents.physicalevents.IREvent;
    import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
    import wiiusej.wiiusejevents.utils.WiimoteListener;
    import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
    import java.lang.Object.*;
    import java.util.Vector;
    import wiiusej.Wiimote;
    import wiiusej.panels.graphs.IRSinusoids;
    import wiiusej.values.IRSpeed;
    import wiiusej.wiiusejevents.wiiuseapievents.WiimoteEvent;
    
    
    
    
    public class IRPanel extends JPanel implements WiimoteListener {
            private Wiimote wiimote;
            private Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(2, true);
            private WiiUseApiManager n= new WiiUseApiManager();
            private MotionSensingEvent motionSensingEvent = null;
            private IREvent infraredEvent = null;
    	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;
            int avrg =0;
            private JButton zoom;
    
    	public IRPanel() {        
    		shape = new Ellipse2D.Double(0, 0, 10, 10);
    		initArrays();
                    timer2 = new Timer( second, new TimerCatcher2() ) ;
                    timer2 . start() ;
    
                    zoom = new JButton();
    
                    GroupLayout mainPanelLayout= new GroupLayout(this);
                    this.setLayout(mainPanelLayout);
                    mainPanelLayout.setHorizontalGroup(
                    mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addGroup(GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addContainerGap(317, Short.MAX_VALUE)
                        .addComponent(zoom)
                        .addContainerGap()));
                    mainPanelLayout.setVerticalGroup(
                        mainPanelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                        .addContainerGap(245, Short.MAX_VALUE)
                        .addComponent(zoom)
                        .addContainerGap())
                        );
    
                    zoom.setText("Click To Enlarge");
                    zoom.addMouseListener(new MouseAdapter() {
                    public void mousePressed(MouseEvent evt) {
                        zoomButtonPressed(evt);
                    }
                });
                   
    	}
            public IRPanel(boolean button) {
    		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 getRandomX(){
                    if((randomX <= xb && randomX+15 >= xb)&&(randomY <= yb && randomY+15 >= yb)){
                        randomX = 200 +(int)(Math.random() * ((350 - 200)+1));
                }
                    return randomX;
            }
            
             public int getRandomY(){
                    if((randomX <= xb && randomX+15 >= xb)&&(randomY <= yb && randomY+15 >= yb)){
                        randomY = 200 +(int)(Math.random() * ((350 - 200)+1));
                }
                    return randomY;
            }
    
    
            public int getAvrageSpeed(){
                distance2 = (int) Math.sqrt(Math.pow(oldRandomX-randomX,2) + Math.pow(oldRandomY-randomY,2));
                speed =  (int) (distance2 / avrgTime);
              //int avrgSpeed=0;
                if((randomX <= xb && randomX+15 >= xb)&&(randomY <= yb && randomY+15 >= yb)){              
                    avrg = (speed+avrg);
                    avrgSpeed = avrg /zz;
                    oldRandomX = randomX;
                    oldRandomY =randomY;
                    avrgTime=0.0;
                    IRMotionRecordPanel ir= new IRMotionRecordPanel();
                    if(ir == null){
                        System.out.println("was empty");
                    }else{                   
                        ir.setSpeed(avrgSpeed);            
                    }
    
                }
               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);
    
    
                    Stroke drawingStroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);
                    Line2D horizontalLine = new Line2D.Double(20, d.height /2, d.width, d.height /2);
                    Line2D verticalLine = new Line2D.Double(d.width/2, d.height, d.width/2, 0);                 
    /*!!*/          randomPoints();
    
            
    //draw center lines-------------------------------------------------------------
                    g2.setColor(color.gray);
                    g2.setStroke(drawingStroke);
                    g2.draw(horizontalLine);
                    g2.draw(verticalLine);               
    
                  
    //numbering---------------------------------------------------------------------
                    int number =0;
                    int num =0;
                    int index =1;
                    for (int i=0; i<d.height;i++){
                       if(num == number)
                        {
                             number=number+10;
                             g2.drawString(""+index++, 6, number);
                        }
                        ++num;
                   }
                    g2.drawString(""+d.height, 0, d.height);
                    g2.drawString(""+d.width, d.width-20, 10);
                    g2.setColor(Color.RED);
     
    //points and the body-----------------------------------------------------------
    
               
    		int i=0;
    		while (i < nbPoints) {
    			int x = xCoordinates[i];
    			int y = yCoordinates[i];
    //all points resolution----------------------------------------------------------------
                            int xx = Math.round((int) getWidth() * x / 1024);
    			int yy = getHeight()- Math.round((int) getHeight() * y / 768);
                            g2.translate(xx, yy);
    
    //each point--------------------------------------------------------------------------------------------------
                             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);
    
    
                             hypotenuse  = Math.sqrt(Math.pow(xb-xa,2) + Math.pow(yb-ya,2));
                             adjacent    = Math.sqrt(Math.pow(xa-xa,2) + Math.pow(ya+50-ya,2));
                             oposite     = Math.sqrt(Math.pow(xb-xa,2) + Math.pow(yb-(ya+50),2));
                             radians = Math.acos((Math.pow(adjacent,2) + Math.pow(hypotenuse,2)- Math.pow(oposite,2)) /
                                                                    (2*adjacent*hypotenuse));
                             degrees = (int)  Math.toDegrees(radians);
                             g1.drawArc(xa, ya, 20, 20 , 270, degrees);
                             g1.drawString("Angel :"+degrees,20,d.height /2);
    
            
                             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" + getAvrageSpeed(), 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);
    		}
    	}
    
    	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;
    		}
    		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();
    	}
    
            public void zoomButtonPressed(MouseEvent evt){
                 if(zoom.isEnabled()){
                    if (wiimotes.length > 0) {
                        new EnlargeView(wiimotes[0],false,true,false);
    		} else {
                        new EnlargeView(false,true,false);
    		}
                }
                
            }
    
        public void onMotionSensingEvent(MotionSensingEvent e) {
          
        }
    
    
    
    
        class TimerCatcher2 implements ActionListener
        {
            public void actionPerformed( ActionEvent e )
            {
               avrgTime++;
               
            }
        }
    
    }





    only method you need to worry about is getRandomX() and getRandomY()

    and how to return their value from other classes
    i hope you will be able to sort it out for me



    thank you very much

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

    Default

    i just solved it!! all i had to do is make that variable static :))

    thanks to everyone for all the help.

    regards

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

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

    Default

    randomx and random y i change those two in to a static variables and now when their value changes i am able to retrieve it from other classes

Page 1 of 2 12 LastLast

Similar Threads

  1. get url returned by search
    By avizana in forum Networking
    Replies: 2
    Last Post: 03-14-2011, 07:43 PM
  2. 2 Items returned on itemStateChanged() Test???
    By AcousticBruce in forum New To Java
    Replies: 1
    Last Post: 01-08-2011, 04:10 PM
  3. SplashScreen.getSplashScreen() returned null
    By kmm1977 in forum AWT / Swing
    Replies: 4
    Last Post: 06-21-2010, 11:04 AM
  4. SplashScreen.getSplashScreen() returned null
    By ibrahimyoussof in forum AWT / Swing
    Replies: 2
    Last Post: 04-17-2010, 02:51 PM
  5. Cant we have two values returned from a method in a class
    By jaiminparikh in forum Advanced Java
    Replies: 7
    Last Post: 03-21-2009, 03:20 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
  •