Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-24-2007, 04:01 AM   #1
Registered: Oct 2007
Posts: 66

Rep: Reputation: 15
Thumbs up 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()
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
<APPLET CODE="applet1.class" WIDTH=500 HEIGHT=500>

i compiled like this
class file is created

to run i used like this

i used
>appletviewer sample.html

i dont know the problem.

thanks in advance.
Old 10-24-2007, 05:52 AM   #2
Mega Man X
LQ Guru
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
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!
Old 10-24-2007, 06:45 AM   #3
Registered: Oct 2004
Location: UK
Distribution: Ubuntu/Kubuntu
Posts: 249

Rep: Reputation: 30
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?
Old 10-24-2007, 03:31 PM   #4
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
The original poster replied:
firstof all thanks.
i tried urs.i set the color before drawString.i changed the position to 10, also its not working.
actually i m using mozilla .first i tried to open that html file said some plug in missing.then only i changed 2 appletviewer.
Old 10-26-2007, 06:36 AM   #5
Registered: Oct 2004
Location: UK
Distribution: Ubuntu/Kubuntu
Posts: 249

Rep: Reputation: 30
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??


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
paint-mono ( for Mono) Installer? caa1980 Linux - Software 0 10-03-2007 10:52 AM
java; how do I make a subclass extend a library function? fatblueduck Programming 2 04-28-2006 01:40 PM
java, call function in calling object exodist Programming 9 06-13-2004 11:49 PM
Web start java not working (java works fine) powadha Debian 5 06-05-2004 12:57 PM
Java Applet paint() Jose Muņiz Programming 0 10-26-2003 06:45 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:25 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration