[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 185
  • Last Modified:

Painting on/over Components

I intend to do the following:
After an application (or an applet in my case) is already displayed on the screen I want to draw a String on a Button in a different color when this Button is clicked.
The text on the Button shall be displayed in green when the use of the Button is OK and it should be appear in red when the applet is busy and is doing this action right now.

The main question is:
How can I use painting methods (Graphics methods) outside the paint()-method.

I tried the following several times:

//class members:
Button button = new Button("Show");
anyMethod()
{
   Graphics gc = button.getGraphics();  // this works well
   FontMetrics fm = gc.getFontMetrics(gc.getFont());  // OK
   gc.setColor(Color.red);
   gc.drawString("Show",button.size().width/2-
                          fm.stringWidth("Show")/2,
                          top+5 /*any y-point*/);
}// end anyMethod

To solve this problem more generally:

How can I paint (lines, text, shapes, ...) without invoking the paint method?
I mean, sometimes you want to draw something after you have displayed something else without redrawing this because that doesn't change!
Of course, what is already displayed shall be displayed without changing!

I am still currently using JDK 1.0, however I am interested in the solution for JDK 1.1, too, if it differs from the "1.0-solution".
0
dirku
Asked:
dirku
  • 3
1 Solution
 
gadioCommented:
An option for you would be to try and invoke the paint method for the button only. You have to override the paint method of the button so that it will do what you want and then use button.repaint() . This would redraw only the button.
0
 
jpk041897Commented:
You will need to extend the button component since the standard AWT button only handles black text.

Read the article at:

http://www.javaworld.com/javaworld/jw-03-1997/jw-03-imagebutton.html

for information on how to create your own button class.

The information you will find ther will probably be overkill for what you want to do :-) but it will solve your problem and teach you some other usefull techniques.
0
 
jpk041897Commented:
Sorry, I missed a part of your question. In 1.02 the only way you can draw something is using paint(), in 1.1 you can use lightweigt components.
0
 
dirkuAuthor Commented:
I tried this with MS IE 3.01 gold but it doesn't work, however with MS IE 4.0 it does.
Comments?

0
 
jpk041897Commented:
Tried what? light weight components (they only work in 1.1) or overriding the AWT?

In either case, I would have to see some of your code to be able to help you out better.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now