Solved

Painting on/over Components

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 3 50
JAVA part two 5 58
JList custom Cell Renderer refresh 15 53
How  Can  I  resolve   this  Error "java.sql.SQLException: Invalid column index" 4 25
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

896 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

12 Experts available now in Live!

Get 1:1 Help Now