Solved

Painting on/over Components

Posted on 1998-03-16
5
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 6

Expert Comment

by:gadio
ID: 1233585
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
ID: 1233586
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
ID: 1233587
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
ID: 1233588
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
ID: 1233589
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 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