Solved

Painting on/over Components

Posted on 1998-03-16
5
177 Views
Last Modified: 2010-04-16
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
Comment
Question by:dirku
  • 3
5 Comments
 
LVL 6

Expert Comment

by:gadio
Comment Utility
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
 
LVL 6

Accepted Solution

by:
jpk041897 earned 50 total points
Comment Utility
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
 
LVL 6

Expert Comment

by:jpk041897
Comment Utility
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
 

Author Comment

by:dirku
Comment Utility
I tried this with MS IE 3.01 gold but it doesn't work, however with MS IE 4.0 it does.
Comments?

0
 
LVL 6

Expert Comment

by:jpk041897
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now