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").
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.
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
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.
the expert user help u...
for the expert user...
plz help me in my topic...
"COMBINATION WORDS" - in new java
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.