Results 1 to 4 of 4
  1. #1
    Hornfreak is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Drawing an object in my canvas class, the object is created in a separate class

    Hello everyone, I'm quite a noob at Java and this is my first post but I don't know where else to ask and I can't find problem specific solutions to this, I'm sure the answer is something really simple but like I said i'm rather new at Java.

    basically Im making a program which prints figures onscreen after I press a button. the figures are branched from a class called JFigure I created. so basically this is what my code to achieve this looks like:

    First up I have a class called StackClass which among other things declares a vector of JFigures and has the following function. JRect is another function with JFigure inheritance which draws a rectangle.

    Java Code:
    public boolean AddE(){
            int n =0;
    
            if(jTextField1 == null)
                return false;
            else{
                
                stackFig[n]= new JRect();
                n++;
                valor = Integer.parseInt(jTextField1.getText());
                stack.push(valor);
                System.out.println("Added "+ value+" to the stack");
                System.out.println(Figure's height: "+(int) stackFig[0].getDim().getHeight() + ".");
                
                return true;
    
            }
    now in my Canvas class I have the following method to get the currently drawn figure and display onscreen:

    Java Code:
    public void paint (Graphics g){
            s1= new StackClass();
            figures= s1.getFig();
            g.setColor(Color.blue);
           // g.drawRect(2, 2, (int) dimP.getWidth( ) - 4, (int) dimP.getHeight( ) - 4);
           // g.drawRect(4, 4, (int) dimP.getWidth( ) - 8, (int) dimP.getHeight( ) - 8);
            figures.print(g);
            repaint();
            
        }
    i've left uncommented the relevant parts of this function here.
    so basically the error I get after I try to run this is:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    which if I suspect right, comes from the fact that i'm trying to print the figure before it actually exists, however I'm not sure as to how to prevent this from happening, I'd appreciate any help! thanks!

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

    Default

    There are a lot of problems in your code. You seem to be working with Canvas objects in a Swing program which you shouldn't be doing. Rather draw in the paintComponent method of a JPanel or JComponent. You're calling repaint from within a paint method -- that makes no sense. You shouldn't be creating a new StackClass(); from within a paint method but should create it in your constructor and then use it in the paint method (or better as noted above, the paintComponent method of a JPanel or JComponent).

  3. #3
    Hornfreak is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Alright thanks! I fixed it now, just one question now though, is there a particular reason why my Tabbed pane components are not appearing correctly?
    like. I have to Hover over the buttons and textfields for them to appear.

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

    Default

    Quote Originally Posted by Hornfreak View Post
    Alright thanks! I fixed it now, just one question now though, is there a particular reason why my Tabbed pane components are not appearing correctly?
    like. I have to Hover over the buttons and textfields for them to appear.
    Hm, I'm scratching my head trying to figure out how you would assume we'd know the answer to this without seeing your code.

Similar Threads

  1. Replies: 1
    Last Post: 02-02-2011, 06:11 PM
  2. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  3. Replies: 2
    Last Post: 02-18-2010, 05:26 PM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 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
  •