paint function in java not working
Hai all
i m using fc6.i wrote 1 simple applet program.code is given below.drawString function is not displaying anything in the applet window. applet is initiated & started.i set background color in init function.its properly set.its entering into paint function.but not displaying the drawString. import java.awt.Graphics; import java.applet.*; import java.awt.*; public class applet1 extends Applet { public void init() { setBackground(Color.red); } public void paint(Graphics g) { System.out.println("inside paint function"); g.drawString("This is a simple java applet!", 100, 200); } } this is my html code (sample.html) <html> <body> <APPLET CODE="applet1.class" WIDTH=500 HEIGHT=500> </APPLET> </body> </html> i compiled like this >javac applet1.java class file is created to run i used like this i used >appletviewer sample.html i dont know the problem. thanks in advance. |
I can't see anything wrong with your code. I've tested in Eclipse and it is working. What happens if you open the html file with a browser? It could also be that, for some odd reason, the String is being draw outside the applet. Try to set the position to 10, 10 instead 100, 200 and see if that helps. Try also setting different colours in the paint method before drawing the String (g.setColor(Color.white), for example).
If that still does not help, I don't know what the problem is. A little on style though: Try to name all classes you create in Java, with the first letter in uppercase (in this case, Applet1). It won't affect your code at all, but it is encouraged to do so in order to keep consistence with the rest of Java classes. Good luck! |
I can't see a problem either, I did exactly as you described, used javac and appletviewer and it works for me.
What Java version are you using? |
The original poster replied:
Quote:
|
Can you post the output of running java -version in a console/command prompt please?
Do any of the other Graphics class methods work, i.e. drawLine, drawRect, etc?? |
All times are GMT -5. The time now is 09:42 PM. |