Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
problem with arrays and paint
ed jones
Greenhorn
Posts: 11
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
I would like to make 5 rows each with 10 blocks of Rectangles
When I run this I only see the last row, and I can't figure out why?
Also is there a better way to approach this ? / ie: creating Rectangle objects.
thanks for your time
package Testing; import java.applet.Applet; import java.awt.* ; public class FirstClass extends Applet{ private static final long serialVersionUID = 1L; int startX = 10; int startY = 10; Rectangle[] array; public void init() { array = new Rectangle[100]; makeBlocks(); } public void makeBlocks() { for(int j=0; j<5; j++) { for(int i=0; i<10; i++) { array[i] = new Rectangle(startX, startY, 30, 10); startX += 35; //System.out.println(array[i]); } startX = 10; startY += 20; } } public void paint( Graphics g ) { g.setColor(Color.red); for(int j=0; j<5; j++) { for(int i =0; i <10; i++) { g.fillRect(array[i].x, array[i].y, array[i].width, array[i].height); //System.out.println(array[i]); } startX = 10; startY += 20; } } } }
Campbell Ritchie
Marshal
Posts: 79151
377
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Your problem is with the for loops; read carefully what you are entering into the array and what you are retrieving.
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Thread.sleep question
Array overwritten
Array of colors in paintComponent method.
How to make rectangle drawn to be vissible when a new rectangle is drawn
Applet not running
More...