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.
The moose likes Beginning Java and the fly likes How to fill in color? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to fill in color?" Watch "How to fill in color?" New topic

How to fill in color?

may tan

Joined: Jul 22, 2005
Posts: 22

Just wanna know how to fill in color to 3 rectangle when the user enter 3 diff color..

For eg: user input into text field & click compute buttton:
1st color: "red",
2nd color: "orange" &
3rd color: "gray".

Then the 1st rectangle would appear "red",
2nd rectangle would appear "orange"
3rd rectangle would appear "gray"

Can someone help me out with simple coding of this?
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Hi -

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.

Here is an example:

'Hope that helps .. PSM

Paul M. Santa Maria, SCJP
may tan

Joined: Jul 22, 2005
Posts: 22
Hmm.. But i need my 3 rectangle to change color at the same time according to user input.

Is there a more specific example?
Do I need to use looping method? eg: do-while

sorry i kinda not that gd in java.
Philip Heller
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
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
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
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, it looks
like Philip's book might be a good fit for you. Please consider
getting it.

'Hope that helps .. PSM
may tan

Joined: Jul 22, 2005
Posts: 22
ok.. thanks pple.. i got it alrdy
I agree. Here's the link:
subject: How to fill in color?
It's not a secret anymore!