hey,
i have got a class that extends JPanel called viewPanel and in there, there is paint method.
in the paint method there is a oval point drawn on it. the coordinates x and y for the point are generated by using math.random so when the program executes the point is randomly placed on the screen and after small time interval new random coordinates are set for x and y of the point.

i have a different class which also extends JPanel called controlsPanel what i want to get is the x and y values of the point and set is as a label.

i tried creating method in the viewPanel that returns x and y but once i create an object of the viewPanel in controlsPanel and i call the method it just returns me zero.
i have also tried creating object of the controlsPanel in the viewPanel and set the x and y values as the parameters of one of the methods in the controlsPanel but i received an "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException "

it is so frustrating that i cant solve this i hope you will be able to help me.

any suggestions will be greatly appreciated thank you

Recommended Answers

All 13 Replies

thank you for your reply,
the thread you have showed seems "long-winded" and very unclear do you knoe any other examples that would help me to sort my problem
thanks

how many posts you've already sent here ..., anything about your topic is only on an academical level

i am independent learner and if i have issue i ask people for help. i guess thats what these forums are for. if everybody would know how to do everything websites like this would go obsolete

huuuh,

your topic is written with vague style, any line from your code can generated NPE,

that's nothing against your person, just talking about NPE without code is freeStyleDiscusion same as about Space Constant

ok here is the code i have so far

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);               
	}

what i really want is to get my averagespeed() and be able to print it out on my other JPanel class

for exmple in my conrolls panel class once the button is clicked i want something like this

private void getSpeedMousePressed(java.awt.event.MouseEvent evt) {  
ViewPanel vp = new ViewPanel();                                          
                if(recordShoulder.isEnabled()){
                   label.setText("avrage speed is"+ vp.getAvrageSpeed());
        }
    }

i hope this makes it clearer what im trying to do

what is howed you in the code above keeps on printing the value zero but i am sure the value is greater than zero.


if you have more questions about the program please ask.


thank you for all the help

please to check above link again, (AnimationBackground)

untill replace

label.setText("avrage speed is"+ vp.getAvrageSpeed());
with
SwingUtilities.invokeLater(new Runnable() {
   public void run() {
     label.setText("avrage speed is"+ vp.getAvrageSpeed());
   }
});

what libraries are you importing to make this code work? because i am currently getting some errors

sorry if I missunderstood

these code examples is in runnable forms inc. requird Libraries Imports,

you can (directly copy & paste) run that directly from IDE or compile by using Command Line

hmmm NOTE each code is separate class, don't put that alltogether, split out ...

thanks for all the help,
i have tried everything now but it keeps on giving me zero.
i know the value is not zero but the value returned does not change just returns me zero..
please help , this is driving me mad

thank you very much

this is totally irrelevant to me. the problem is not about gui the problem is that the values of variables are not passes when i call the class.

the problem is that when i call the method it keeps on returning me the same zero that i have initialized the variable with even though the value has changed of that variable. i just need to know why the value sets it self back to zero when i call the method when the value should be different from zero

dont worry any more just solved it only thing i had to do is to make the variable static.

thank you for all the help

kind regards

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.