Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Drawing a Graphics object in a panel...

Posted on 1998-02-05
5
Medium Priority
?
248 Views
Last Modified: 2010-04-16
I am trying to use Graphics.drawString() in a java.awt.Panel and I am having trouble.  I have a Panel on my applet, and when the user's mouse enters the Panel, I want to draw a String of text in the panel.  How do I do this?
0
Comment
Question by:bjg
  • 3
  • 2
5 Comments
 
LVL 4

Expert Comment

by:russgold
ID: 1232881
Are you using JDK 1.02 or JDK 1.1?
0
 

Author Comment

by:bjg
ID: 1232882
JDK 1.1
0
 
LVL 4

Expert Comment

by:russgold
ID: 1232883
What kind of problems are you having? Are you using a MouseEventListener to detect the mouse moving into the Panel?
0
 

Author Comment

by:bjg
ID: 1232884
Yes, it detects when the mouse moves into the panel successfully, that I checked.  But when I make a call to g.drawString(), nothing happens...
0
 
LVL 4

Accepted Solution

by:
russgold earned 100 total points
ID: 1232885
When doing GUI programming, direct updates to graphics are generally not recommended.  The problem is that the kernel will, at certain times, insist on refreshing the screen by calling update().

Now, the default implementation of update() clears the graphics region to the background and then invokes paint().  The default paint() method for Panel simply updates its child components.

This means that your text could be erased immediately!

A better approach might be to store the desired text and have it drawn on your Panel (once defined) in the paint method.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

916 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