Results 1 to 4 of 4
Thread: geting value from paintComponent
- 03-21-2011, 07:24 PM #1
Member
- Join Date
- Feb 2010
- Posts
- 51
- Rep Power
- 0
geting value from paintComponent
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
- 03-21-2011, 07:34 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
one of your classes need a reference to the other class/instance !
-->
Dependency injection - Wikipedia, the free encyclopedia
- 03-21-2011, 07:35 PM #3
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You should NOT be setting the x/y values in the paintComponent() method. This method is called whenever Swing determines the panel needs to be repainted. You need to define a Point() object which contains the x/y values. Then you need a method to "randomize" the x/y values of the Point. Then you can create a method that simply returns this Point object.
- 03-21-2011, 07:56 PM #4
Member
- Join Date
- Feb 2010
- Posts
- 51
- Rep Power
- 0
thanks for the reply,
@camickr could you please elaborate a bit more on how to create point() object and set the values to it and then use it.and later randomize the values of the point.
im quite new to java and it is kind of hard for me to get.
may be you know some similar examples that would demonstrate how it could be done.
thank you very much
Similar Threads
-
geting user input (number)
By kliane in forum New To JavaReplies: 9Last Post: 01-17-2010, 12:25 PM -
geting value from text
By Mekonom in forum New To JavaReplies: 10Last Post: 12-15-2009, 12:22 AM -
geting object from other class
By Mekonom in forum New To JavaReplies: 10Last Post: 11-12-2009, 03:34 PM -
Sending SMS to other mobile geting exception as java.lang.IllegalArgumentException
By maruffaiz in forum CLDC and MIDPReplies: 1Last Post: 08-07-2009, 09:37 PM -
Geting below em while starting the was6.1 server.
By kumarjp in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 04-08-2009, 08:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks