aspose file tools*
The moose likes Beginning Java and the fly likes Array of objects/ graphics painting help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array of objects/ graphics painting help" Watch "Array of objects/ graphics painting help" New topic
Author

Array of objects/ graphics painting help

Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 354

Hello all! I've been browsing here for awhile, but I'm new here so I hope to meet a lot of nice people and learn a lot!

this is my initiation.



My code is:



Originally the paint routine just used f. instead of f1[a]. but it wouldn't print any rectangles, because of the obvious, but if I switched everything to just f. I'm not able to get anything in the display, which is to display the properties of my objects.

Basically with this code I get this error when run.


How many rectangles do you want to draw?
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Rect.paint(Rect.java:91)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Rect.paint(Rect.java:91)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Rect.paint(Rect.java:91)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Rect.paint(Rect.java:91)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Rect.paint(Rect.java:91)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:713)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:693)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)



How can I fix this so I can be able to do everything I need to?


EDIT: I fixed it but I now have a new issue.



It would print the rectangles, however if I resized the screen, only the last one would appear. How can I fix it so they are all displayed all the time.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Konrad Zuse wrote:...my question is how do I get this data of faceName x y width and height?

When I try to print the position of the new array object:

I get :

Face@1ca318a...

f1[1] references an instance of Face. When you provide this reference as a parameter to println, it will call the object's toString() method. This method is inherited from the Object class. And because this Face class does not override toString(), the implementation from Object is invoked (which basically gives you the class name, followed by the '@' character, followed by a hex representation of the object's hash code). This is why you're getting that output.

If you want to output the values of instance variables, simply use the dot operator. For example, to output the value of x...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 354

marc weber wrote:
Konrad Zuse wrote:...my question is how do I get this data of faceName x y width and height?

When I try to print the position of the new array object:

I get :

Face@1ca318a...

f1[1] references an instance of Face. When you provide this reference as a parameter to println, it will call the object's toString() method. This method is inherited from the Object class. And because this Face class does not override toString(), the implementation from Object is invoked (which basically gives you the class name, followed by the '@' character, followed by a hex representation of the object's hash code). This is why you're getting that output.

If you want to output the values of instance variables, simply use the dot operator. For example, to output the value of x...



Yeah I was able to fix it, for some reason when I first did it all it would show me was the normal stuff like .wait or .clone, etc. I edited a new issue I've been having, thanks for the help!
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Instead of editing an earlier post, it's much better to post a reply in the thread, or better still start a new topic for an unrelated problem. This time round, lets complete the discussion here.

1. Your code doesn't indicate whether you're performing custom painting in an AWT or Swing component, in an application or an applet. In future, to get better help sooner, post a SSCCE that clearly demonstrates your problem.

2. When doing any custom painting that doesn't cover the entire are of a component, it's necessary to invoke the super implementation to clear the background.

3. Your custom painting only ever does paint a single rectangle. If you want to paint a number of rectangles, you need to maintain a java.util.List<Rectangle> or an array of type Rectangle[] and iterate over the list/array, painting each rectangle.

Further reading: Performing Custom Painting and the API for ArrayList


luck, db
There are no new questions, but there may be new answers.
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 354

Darryl Burke wrote:Instead of editing an earlier post, it's much better to post a reply in the thread, or better still start a new topic for an unrelated problem. This time round, lets complete the discussion here.

1. Your code doesn't indicate whether you're performing custom painting in an AWT or Swing component, in an application or an applet. In future, to get better help sooner, post a SSCCE that clearly demonstrates your problem.

2. When doing any custom painting that doesn't cover the entire are of a component, it's necessary to invoke the super implementation to clear the background.

3. Your custom painting only ever does paint a single rectangle. If you want to paint a number of rectangles, you need to maintain a java.util.List<Rectangle> or an array of type Rectangle[] and iterate over the list/array, painting each rectangle.

Further reading: Performing Custom Painting and the API for ArrayList


I'm sorry, here is my complete code. I also didn't think it was appropriate to make multiple threads about the same kind of issues, so I guess I will in the future.

I'm pretty new to the whole graphics implementation as well.


Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

I've already given you some tips and pointed you to a tutorial. It's pointless to post code that ignores the recommendations and clearly show you haven't gone through the tutorial.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Don't forget to call super.paint(g) at the start of your paint method. This won't solve the NullPointerException but it will solve some other painting issues you will otherwise get.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jay Orsaw
Ranch Hand

Joined: Jun 14, 2011
Posts: 354

Rob Spoor wrote:Don't forget to call super.paint(g) at the start of your paint method. This won't solve the NullPointerException but it will solve some other painting issues you will otherwise get.


I originally had that in my constructor, but it would give me some weird issue so I took it out. Right now all I'm trying to do it get all the rectangles to stay on the screen. They will if I don't resize it, but once I do they all will disappear, besides the last one. The other user said use the util rectangle, or an array of rectangles, but I believe that the Face[] acts as my array passing the parims, and should be all I need. Something is obviously not saving in the background when I'm repainting based on the resize. Or should I just put all the code in the pain routine and loop through it? Which I don't think will work, because when I tried something like that I got the error above.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Konrad Zuse wrote:
Rob Spoor wrote:Don't forget to call super.paint(g) at the start of your paint method. This won't solve the NullPointerException but it will solve some other painting issues you will otherwise get.


I originally had that in my constructor, but it would give me some weird issue so I took it out.

You shouldn't put the super.paint(g) call in the constructor, but in your own paint method.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of objects/ graphics painting help
 
Similar Threads
Null pointer exception.....
Exception when using Nimbus LaF
Validate JTable edit
Turn Based Strategy Games
updating database cause error