Solved

Painting on/over Components

Posted on 1998-03-16
5
179 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
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Why does the Android Studio logcat show this error when the user presses the button? 2 46
Java Options in Zimbra Server 2 52
servlet filter example 37 61
jboss wildfly 10.1 10 87
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 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