Results 1 to 4 of 4
- 05-01-2011, 11:48 PM #1
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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.
now in my Canvas class I have the following method to get the currently drawn figure and display onscreen: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; }
i've left uncommented the relevant parts of this function here.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(); }
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!
-
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).
- 05-02-2011, 04:09 AM #3
Member
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
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.
-
Similar Threads
-
Insert class file as object in a table & read the object from the blob.
By facemeguru in forum New To JavaReplies: 1Last Post: 02-02-2011, 06:11 PM -
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
How to assign unique id to each object created from same class
By srisar in forum New To JavaReplies: 2Last Post: 02-18-2010, 05:26 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks