Ask Question. Asked 9 years, 10 months ago. Active 6 years, 6 months ago. Viewed 9k times. I have 2 class files called PaintMe. Applet; import java. Improve this question. Andrew Thompson k 39 39 gold badges silver badges bronze badges. You shouldn't be calling the paint Graphics g method yourself.
It is called by the Event Dispatch Thread. If you remove ring. Add a comment. Active Oldest Votes. To get it to compile, change ring. Use Swing which offers a JApplet. Don't start an applet from the main String[]. A GUI can be designed in a panel, that is then put in a free-floating application or applet. That is known as a hybrid. But both frame and applet separately add the GUI, which is most often a different class to either. The main as it exists is useless.
Unless the applet is added to a container and made visible, the code will run successfully but end in a few moments without displaying anything. Update Try this. I need to have it started from Starter. Dimension; import javax. The repaint Method The second case, when paint calls are genereted is when the program calls repaint or update. The repaint method is the one invoked by a program to do drawing. Their are 4 versions of this method but the one with no arguments is usually used.
Drawing via repaint most often takes place in response to user input. It schedules a call to an intermediate method, update. Finally, update calls paint unless you override update. The reason for this complexity is Java's support for concurrent programming. It does this using threads. Using repaint can be tricky for at least three reasons. The repaint method is asynchronous. All these potential complications are considered in details below.
However, let's start with a discussion what happens if paint or update are called directly. Using paint and update directly. What is of concern here is what happens if the user, for example, covers all or part of your drawing by another window, and then uncovers your drawing again.
Does it survive? Similarly, what happens if the user resizes the browser window? Not the right way -- RepaintApplet. Java Source When paint is called after init , or after the applet has been covered or iconified, it uses the default paint which just paints the background colour, thereby erasing the image. Note, however, the useful method getGraphics used in this example. Putting drawing in the paint method , calling paint directly -- RepaintApplet1. Java Source A paint method has been added.
This works but the method paint is overly complex. An improvement using update method to redraw the background --RepaintApplet2. For example: Suppose you want to display the current x and y coordinates of the location where the mouse button is clicked in the applet window.
As the applet needs to update information displayed in its window i. To sum up, the repaint method is invoked to refresh the viewing area i.
0コメント