appletviewr - getParameter returns null - java

I compile this code and use applet viewer for testing. But I see string "value: null" instead of "value: VALUE".
1) What did I do wrong?
/* <applet code="Demo" width="100" height="100">
<param name="name1" value="VALUE">
</applet>
*/
import java.applet.*;
import java.awt.*;
public class Demo extends Applet
{
String str=null;
public void init()
{
str=getParameter("name1");
}
public void paint(Graphics g)
{
g.drawString("value: "+str,100,50);
}
}
But if I open HTML file which is located in same folder with Demo.class
<html>
<body>
<applet code=Demo.class width="200" height="200" >
<param name="name1" value="VALUE">
</applet>
</body>
</html>
I get desired output "value: VALUE". (However for this result I should kill process java.exe, otherwise I get non-updated applet although Demo.class was updated).
2) Why won't the applet update until I will the java.exe?

I am getting value: VALUE in the applet viewer here. But then that is after increasing the width of the applet element from 100 to 200 in the comment at the top of the source. Thinner than that, and the text becomes truncated.

Related

Not able to run this applet code in browser

In my Java course, I am learning applet. And I have to do it with awt. In order to run the applet, I first have written the code below and compiled it to get MyApplet.class file and also have placed the RunApplet.html file in the same directory of MyApplet.class file.
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet{
public int x,y,w,h;
public void init(){
x=Integer.parseInt(getParameter("xValue"));
y=Integer.parseInt(getParameter("yValue"));
w=Integer.parseInt(getParameter("wValue"));
h=Integer.parseInt(getParameter("hValue"));
}
public void paint(Graphics g){
g.drawRect(x,y,w,h);
}
}
and the RunApplet.html file is:
<html>
<body>
<applet code="MyApplet.class" width="150" hight="100">
<param name = "xValue" value = "20">
<param name = "yValue" value = "40">
<param name = "wValue" value = "100">
<param name = "hValue" value = "50">
</applet>
</body>
</html>
But, when I am running the html file with a browser, it's neither showing any applet nor any error message. Can anyone help me please?

Can't start applet's html in browser

I'm just begining to learn applet tehnology in universitaty. Need an advice or help. I can't ru applet's html in browser. On starting it I have only a message "ClassNoFoundExeption". Work with Firefox+Eclipse+Xubuntu. I guess I installed all what I need in browser.
My simple code:
Pack/Recs.java
package Pack;
import java.applet.*;
import java.awt.*;
public class Recs extends Applet {
private static final long serialVersionUID = 1L;
int a;
int b;
String tempStr;
public void start()
{
a = 50;
b = 50;
tempStr = "Hallo!";
}
public void paint(Graphics g)
{
g.drawString(tempStr, 10, 10);
g.drawOval(20, 20, a, b);
}
}
My html file:
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="content-type">
<title>Java Applet</title>
</head>
<body>
<h3>Java Applet</h3>
<applet code="Pack.Recs.class" height="400" width="500">
<param name=width_a value=100>
<param name=height_a value=200>
Your browser does not support the <code>applet</code> tag.
</applet>
</body>
</html>
I found a solution! I moved *.java file from pack and leaved it in /src root.
In html I actualized path to class and runned html file from /bin folder.

Java applet will not execute in Safari

Beginner Java applet user. I'm not sure as to why my applet will not display in a page. I'm almost positive I have all necessary information. Is it possible that it will not show up due to using Safari? Will not show up in Chrome, either. Do you see what I'm missing? Thanks
EDIT: This >> Java Applet sandboxed in Safari? doesn't help, either.
Java program
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;
public class JNumberOne extends JApplet implements ActionListener
{
Font numberOneFont = new Font("Teen", Font.BOLD, 24);
JButton numberOneButton = new JButton("Who's Number One?");
JLabel numberOneMessage = new JLabel(" ");
Container con = getContentPane();
public void init()
{
numberOneMessage.setFont(numberOneFont);
con.add(numberOneButton);
con.setLayout(new FlowLayout());
con.setBackground(Color.WHITE);
numberOneButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
con.remove(numberOneButton);
numberOneMessage.setText("The 1992 Texas Rangers");
con.add(numberOneMessage);
con.setBackground(Color.YELLOW);
validate();
}
}
HTML for Applet
<!DOCTYPE html>
<html>
<head>
<style>
body{text-align:center; background:#00008B;opacity:.5;}
</style>
</head>
<body>
<object code = "JNumberOne.class" width=450 height=100></object>
</body>
</html>
Remove the quotes from the class name.
Replace
<body>
<object code = "JNumberOne.class" width=450 height=100></object>
</body>
With
<body>
<object code = JNumberOne.class width=450 height=100></object>
</body>

chrome crash with method call of java in javascript

I am trying to use a java application in html5 and javascript, but Chrome always crashes when I try the following:
In the html5 document I have this:
<FORM>
<INPUT type="button" value="call JAVA" onClick = "test()">
</FORM>
<object name="application" type="application/x-java-applet" height="300" width="550" >
<param name="code" value="loader"/>
<param name="java_arguments" value="-Djnlp.packEnabled=true"/>
</object>
In the javascript file I use this function(the onclick test()-function)
function test() {
var app = document.getElementById("applet_test");
alert("Screen Dimension\r\n width:" +
app.getScreenWidth() + " height:" +
app.getScreenHeight() );
}
And finally this is the applet_test.java file:
import java.applet.*;
import java.awt.*;
public class applet_test extends Applet
{ public int getScreenWidth() {
return Toolkit.getDefaultToolkit().getScreenSize().width;
}
public int getScreenHeight() {
return Toolkit.getDefaultToolkit().getScreenSize().height;
}
public void main(String [] args) {
}
}
Does anybody know why this won't work? Eclipse is also complaining about the use of an applet in an html5-document:
Element (applet) is obsolete. Its use is discouraged in HTML5
Could this cause the troubles and if so, how can you get the same result with an element?
This works for me.
<!DOCTYPE html>
<html lang="en"><FORM>
<head>
<script>
function test() {
var app = document.applet_test;
alert("Screen Dimension\r\n width:" + app.getScreenWidth()
+ " height:" + app.getScreenHeight());
}
</script>
<body>
<INPUT type="button" value="call JAVA"
onClick = "test()">
</FORM>
<object name="applet_test" type="application/x-java-applet" height="300" width="550" >
<param name="code" value="loader.class"/>
</object>
</body>
</html>
Change var app = document.getElementById("applet_test"); to
var app = document.applet_test;
Eclipse is also complaining about the use of an applet in an html5-document.
Probably because the applet tag is deprecated. Look at bottom of
https://eyeasme.com/Shayne/HTML5_APPLETS/

Creating and running a Java applet

I thought I would try my hand at applets - I made an applet using Eclipse. It runs fine using the Run As -> Java Applet.
I read a bit about running it outside Eclipse, so I did the following:
Made a folder.
Created New -> Java Project [applet_test].
Inside the project, I created New -> Other -> Visual Swing Class -> Applet [Number1] - that created Number1.class.
Added code and ran it as a Java applet - it ran fine.
Exported the project as a JAR file (not a runnable JAR file).
Wrote HTML using TextEdit (Mac's version of Windows' Notepad). The HTML follows, below...
I put the JAR file, HTML and .class file in the folder.
In Terminal (Mac's version of Windows command prompt window), I ran Appletviewer applet_testX2.html (that's the name of my HTML).
I could see a brief flash of the application name at the top of the screen (as would any other running application).
However, the application (which should display a Jpanel with a label and a button) did NOT appear. I also tried running it from Firefox and Safari. Only the HTML code appeared.
So, what am I doing wrong? And, more importantly, how do I do it correctly?
Code follows without imports statements:
<html>
<body>
<applet code="Number1.class" archive="applet_test.jar"
width=300
height=300>
</applet>
</body>
</html>
The Java code:
public class Number1 extends JApplet {
public Number1() {
}
private static final long serialVersionUID = 1L;
#Override
public void init() {
try {
EventQueue.invokeAndWait(new Runnable() {
#Override
public void run() {
initComponents();
}
});
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void initComponents() {
setSize(320, 240);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
JLabel lblAppletTest = new JLabel("Applet test 1");
panel.add(lblAppletTest);
JButton btnPushIt = new JButton("Push it");
panel.add(btnPushIt);
}
}
Firefox source view:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<meta name="Author" content="BT">
<meta name="Generator" content="Cocoa HTML Writer">
<meta name="CocoaVersion" content="1038.35">
<style type="text/css">
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica}
p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Helvetica; min-height: 19.0px}
</style>
</head>
<body>
<p class="p1"><html></p>
<p class="p1"><span class="Apple-converted-space"> </span><body></p>
<p class="p1"><span class="Apple-converted-space"> </span><applet code="Number1.class" archive="applet_test.jar"</p>
<p class="p2"><br></p>
<p class="p1"><span class="Apple-converted-space"> </span>width=300</p>
<p class="p1"><span class="Apple-converted-space"> </span>height=300></p>
<p class="p1"></applet></p>
<p class="p1"></body></p>
<p class="p1"></html></p>
</body>
</html>
My guess is that here:
<applet code="Number1.class" archive="applet_test.jar"
you're not taking packages into consideration. For instance, if the package is myPackage.vol3 then the line should read
<applet code="myPackage.vol3.Number1.class" archive="applet_test.jar"
But if this doesn't help, you'll want to extract any error messages that the browser gives you and edit your original post to show us what they are.
Using Appletviewer
------------------
Write code of Applet.
If you installed tomcat in D:
code
-
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class MyApplet extends Applet
{
public void init()
{
System.out.println("init intilize");
GridLayout g=new GridLayout(4,6,0,0);
setLayout(g);
MyListener m=new MyListener();
for(int i=1;i<=12;i++)
{
Button b=new Button("ok"+i);
add(b);
b.addActionListener(m);
}
}//end of init
public void start()
{
System.out.println("applet started");
}//end of start
public void stop()
{
System.out.println("applet stop");
}//end of
public void paint(Graphics g)
{
g.drawString("Naveed",200,25);
g.drawOval(20,30,30,20);
System.out.println("applet paint");
}//end of start
public void destroy()
{
System.out.println("applet destroy");
}//end of start
}
class MyListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println("button clicked");
}//end of actionPerformed
}
Now save this code in D:, not in sub folder.
First Compile it.
open the cmd
cd D:
type
`javac MyApplet.java -d classpath D:\Tomcat\common\lib\servlet.jar`
This will make a MyApplet.class file
Now make a html file.
<html>
<body>
<applet code="Number1.class" width=30 height=300 > </applet>
</body>
</html>
Save with the name of you want let's say app.html
run the html file now.
In the cmd window
appletviewer app.html
Output will be in front of you.

Categories