Results 1 to 10 of 10
- 04-22-2009, 07:33 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
- 04-22-2009, 10:16 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
You have changed the signature of paintComponent() so that it no longer overrides that of JPanel. The method you have written will not be called as part of the painting process.
Consider making the buildings array an instance variable so that the painting code has access to it. This makes sense because a reference to the collections is a fair part of what makes up the "state" of a CityPanel2 instance.
And drop the instance variable building. This plays a role briefly while an instance is being created, but is no part of the "state" (=="the things which make an instance what it is").
- 04-23-2009, 01:07 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
To elaborate (I hope) buildings should be declared where you declared building, which would allow the
paintComponent method to access the ArrayList. As well, building can be removed as
buildings.add(new Building2(parameters)), should be sufficient. Finally, I find it easier to use a Canvas
and the public void paint(Graphics g) method, as this is called immediately after the constructor.If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 04-23-2009, 01:37 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
- 04-23-2009, 01:48 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
paint is also used by the java.awt.Canvas class, and a Canvas may be placed within a JPanel or JFrame. However, as the paint method must be overridden for anything to be painted, the class using the method must extend Canvas
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
-
I hate to disagree boyo, but I think that it would be a very bad thing to place a Canvas object within a JPanel or a JFrame unless you have a very good reason to do so (I've never had to have done this), and you know exactly what and why you are doing this. Much much better is to place Swing components within Swing components and not to mix AWT and Swing components.
- 04-23-2009, 06:03 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 12
- Rep Power
- 0
- 04-23-2009, 10:22 PM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
You're welcome.
- 04-24-2009, 03:17 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 15
- Rep Power
- 0
waa...
the expert user help u...
for the expert user...
plz help me in my topic...
"COMBINATION WORDS" - in new java
- 04-25-2009, 01:07 AM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
sorry about the idea of mixing AWT and Swing... I have never found the Swing equivalent of a Canvas, but have always used Swing graphics with the exception of the Canvas, and it has never caused any major problems for me. However, Fubarable was probably right. If you have a good idea of how to use AWT graphics, that would most likely be a better idea.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
what wrong
By pro85 in forum New To JavaReplies: 3Last Post: 02-09-2009, 01:07 PM -
What did i do wrong
By jpnym15 in forum New To JavaReplies: 8Last Post: 11-17-2008, 10:07 AM -
So, what am I doing wrong?
By Charles_Smith in forum New To JavaReplies: 0Last Post: 10-29-2008, 02:50 PM -
Can someone tell me what I did wrong??
By booter4429 in forum New To JavaReplies: 7Last Post: 08-13-2008, 08:35 PM -
Java Project Trouble: Searching one ArrayList with another ArrayList
By BC2210 in forum New To JavaReplies: 2Last Post: 04-21-2008, 11:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks