Solved

Drawing a Graphics object in a panel...

Posted on 1998-02-05
5
232 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 50 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:
This video teaches viewers about errors in exception handling.

746 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

9 Experts available now in Live!

Get 1:1 Help Now