Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: paintComponent is not reading my private variables

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default paintComponent is not reading my private variables

    Just to simplify, I'll leave out the details to my code:

    public class Theclass{
     
    private short a,b;
     
    public Theclass(short a,short b){
    }
     
    public void paintComponent(Graphics g){
    super.paintComponent(g)
    System.out.println("a: "+a+" and b: "+b);//outputs: "a: 0 and b: 0"
    }
     
    public static void main(String[]args){
    a=10;
    b=5;
    Theclass tc=new TheClass(a,b);
    }
    }

    So why isn't my paintComponent reading these 2 variables? It draws other things. I'm guessing it's being called before main is called. How do i get it to run when i want it to, aka after the required variables and methods have been run so it knows what to do?


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: paintComponent is not reading my private variables

    You left out so many details that your code is uncompilable.

    What's the point of a constructor that takes two arguments and doesn't do anything with them? Recommended reading: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    db

  3. #3
    Junior Member
    Join Date
    Jun 2010
    Posts
    26
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: paintComponent is not reading my private variables

    sorry i didn't think the code would be enough, but I figured it out.

Similar Threads

  1. Accessability/Visability Private
    By EmSaint in forum Object Oriented Programming
    Replies: 1
    Last Post: March 15th, 2010, 06:50 PM
  2. Private Constructor
    By Ganezan in forum Object Oriented Programming
    Replies: 4
    Last Post: November 7th, 2009, 04:02 PM
  3. Private or public variables??
    By igniteflow in forum Java Theory & Questions
    Replies: 2
    Last Post: September 17th, 2009, 08:07 AM
  4. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM