Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using applets in html

 
tod murphey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I'm trying to practice some java every day, to eventually do some actual work in it! In running applets, one option to test them is to place them in a basic html page and run appletviewer, or load the page from a browser (I assume IE can process applets to the same degree as Netscape??). However, I can't get this one running:
import java.awt.*;
import java.applet.Applet;
public class SimplePaint extends Applet {
public void paint(Graphics g) {
g.setColor(Color.black);
g.fillRect(0,0,300,300);
g.setColor(Color.white);
g.fillOval(30,30,50,50);
}
}
I know the html code is fine, but I'll post it anyway...
<HTML>
<HEAD>
<TITLE> A Simple Program </TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="SimplePaint.class" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
When I run it either way, it doesn't produce what's expected- a white oval on black background. Can you help me out?
thnx!!
 
Suneel Setlur
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I don't see any problem with your program. It works fine.
It does give a black background and a white oval.
May be you can simply try increasing the height parameter in your html.
Regards
Suneel

[This message has been edited by Suneel Setlur (edited January 14, 2001).]
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Your program is ok but Problem arised in your applet. You gave
your height=25 that is the problem. You drew your fillOval
method x=30 and y=30. But you gave 25 height in your applet.
Look your html applet code height and your java fillOval
method's height's i.e. y-axis, both of those are related matter. So increase your applet height point like 100 instead of 50 (
you gave before in height field).
<HTML>
<HEAD>
<TITLE> A Simple Program </TITLE>
</HEAD>
<BODY>
<APPLET CODE="SimplePaint.class" WIDTH=150 HEIGHT=100>
</APPLET>
</BODY>
</HTML>
Then you will find the solution.
Thanks,
Newaz
 
tod murphey
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it again, modifying the height tag up a bit, and I can now see the applet plain as day... Thanks!
 
Golam Newaz
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kindly paste this code into notepad and compile it in jdk1.2 ver.
And give the same html code to run it. Everything should be ok.

import java.awt.*;
import java.applet.*;
public class SimplePaint extends Applet {
public void paint(Graphics g) {
g.setColor(Color.red);
g.fillRect(0,0,300,100);
g.setColor(Color.white);
g.fillOval(25,25,50,50);

}
}
Thanks,
Newaz
 
vineeta pillai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem with your code is that you are
first setting the colour to black and then setting it again to
white again before drawing the oval.
I Hope this will work.
import java.awt.*;
import java.applet.*;
public class SimplePaint extends Applet
{
public void paint(Graphics g)
{
g.drawRect(0,0,25,100);
g.setColor(Color.black);
g.fillOval(5,5,10,10,20,10);
g.setColor(Color.white);
}
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic