LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   paint function in java not working (https://www.linuxquestions.org/questions/linux-newbie-8/paint-function-in-java-not-working-594138/)

mathimca05 10-24-2007 04:01 AM

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.

Mega Man X 10-24-2007 05:52 AM

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!

mickyg 10-24-2007 06:45 AM

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?

Mara 10-24-2007 03:31 PM

The original poster replied:
Quote:

hai
firstof all thanks.
i tried urs.i set the color before drawString.i changed the position to 10,10.now also its not working.
actually i m using mozilla .first i tried to open that html file only.it said some plug in missing.then only i changed 2 appletviewer.

mickyg 10-26-2007 06:36 AM

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.