This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Basic answer: override "Paint()" and draw your rectangles (filled or unfilled) there.
More detailed answer: You would have some drawable object (for simple "hello world" type programs, your might just subclass "JFrame"), you would override the "Paint()" method, and then simply use the "Graphics" object passed into "Paint()" to draw the filled rectangles.
This is a simple question with a complicated answer. If you want the screen's appearance to change in response to user activity, you need to understand Java'a event thread mechanism. Too complicated to explain here ... good introductory books take a couple of chapters to explain it. Pardon my self-promotion, but check out "Ground-Up Java", Chapter 16.
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Paul Santa Maria
Joined: Feb 24, 2004
Hi, May -
1. Sorry: I thought the gist of your question was "How do I draw a colored rectangle?". You're actually trying to do several things at once:
a) Draw multiple rectangles b) Read the data that tells you how to draw the rectangles c) Get the notification that tells you to read the data in the first place
2. Strong suggestion: first write small, simple, programs to do any *one* of these things. For Example:
a) Write a program with an edit box and a push button. Respond to the pushbutton click by reading what's in the edit button.
b) Write another program that draws a colored rectangle in a window.
c) Etc etc
3. Based on the (very favorable!) reviews on Amazon.com, it looks like Philip's book might be a good fit for you. Please consider getting it.