Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to fill in color?

 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello..

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
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
http://www.faqs.org/docs/javap/c3/s7.html
<= SEARCH FOR "CLASS STATICRECTS"
THIS EXAMPLE HAPPENS TO USE AN APPLET INSTEAD OF A
JFRAME, BUT IT wORKS THE SAME IN EITHER CASE...

'Hope that helps .. PSM
 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

'Hope that helps .. PSM
 
may tan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.. thanks pple.. i got it alrdy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic