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

    Default 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

  2. #2
    eRaaaa is online now Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    one of your classes need a reference to the other class/instance !
    -->
    Dependency injection - Wikipedia, the free encyclopedia

  3. #3
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    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.

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

    Default

    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

  1. geting user input (number)
    By kliane in forum New To Java
    Replies: 9
    Last Post: 01-17-2010, 12:25 PM
  2. geting value from text
    By Mekonom in forum New To Java
    Replies: 10
    Last Post: 12-15-2009, 12:22 AM
  3. geting object from other class
    By Mekonom in forum New To Java
    Replies: 10
    Last Post: 11-12-2009, 03:34 PM
  4. Replies: 1
    Last Post: 08-07-2009, 09:37 PM
  5. Geting below em while starting the was6.1 server.
    By kumarjp in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 04-08-2009, 08:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •