When writing applet do you start out as an application or applet?

J

jmDesktop

If you are creating an application that is going to be run in an
applet, do you start out creating a regular application an moving it
to an applet or just creating it as an applet, beginning to end. Size
of project is moderat to large. Thanks.
 
K

Knute Johnson

jmDesktop said:
If you are creating an application that is going to be run in an
applet, do you start out creating a regular application an moving it
to an applet or just creating it as an applet, beginning to end. Size
of project is moderat to large. Thanks.

It depends on a lot of things. If my program is only going to be an
Applet, I create it as an Applet. Often if my program can be contained
in a Panel or drawn on a Canvas, I extend that component and can either
add it to an Applet or an application. See the simple example below of
test.java which extends Canvas and draws a blue oval in a yellow
background. The test1.java is an Applet and will display the test
class. test2.java is an application and will also display the test class.

import java.awt.*;

public class test extends Canvas {
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(Color.YELLOW);
g.fillRect(0,0,w,h);
g.setColor(Color.BLUE);
g.fillOval(w/4,h/4,w/2,h/2);
}
}


import java.awt.*;
import java.applet.*;

public class test1 extends Applet {
public void init() {
test t = new test();
t.setPreferredSize(new Dimension(400,300));
add(t);
}
}

<html>
<head>
</head>
<body>
<applet code="test1.class" width="640" height="480">
</applet>
</body>
</html>

import java.awt.*;
import java.awt.event.*;

public class test2 {
public static void main(String[] args) {
final Frame f = new Frame("test2");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
f.dispose();
}
});

test t = new test();
t.setPreferredSize(new Dimension(400,300));

f.add(t,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}

Sometimes you can make a hybrid that is both an Applet and an
application. The following code can be run as an Applet or as an
application. The Applet is a handy component, it extends Container and
Panel and can be used for anything that those components can be used for.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class test3 extends Applet {
public void paint(Graphics g) {
int w = getWidth();
int h = getHeight();
g.setColor(Color.BLUE);
g.fillRect(0,0,w,h);
g.setColor(Color.YELLOW);
g.fillOval(w/4,h/4,w/2,h/2);
}

public static void main(String[] args) {
final Frame f = new Frame("test3");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
f.dispose();
}
});

test3 t3 = new test3();
t3.setPreferredSize(new Dimension(400,300));
f.add(t3,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
}
 
A

Arne Vajhøj

jmDesktop said:
If you are creating an application that is going to be run in an
applet, do you start out creating a regular application an moving it
to an applet or just creating it as an applet, beginning to end. Size
of project is moderat to large.

For size > small I would find it very tempting to develop and
test classes in app context and then move it into applet context
later.

Arne
 
R

Roedy Green

For size > small I would find it very tempting to develop and
test classes in app context and then move it into applet context
later.

It is easier to debug an application than an Applet. I debug my
hybrids as applications. However, the structure is Applet plus a main
method to let them also behave as applications.
 
A

Andrew Thompson

It depends on a lot of things.  

I only had time to glance at what Knute said,
but I think I basically agree with his take
on it.

For trivial things - direct to applet, for
anything beyond that, hybrid.

This project sounds like it should be an hybrid.

Is there a particular reason the app. is not
being launched using webstart? The maintenance
costs of a JWS app. will be lower in the long
run.
 
J

jmDesktop

I only had time to glance at what Knute said,
but I think I basically agree with his take
on it.

For trivial things - direct to applet, for
anything beyond that, hybrid.

This project sounds like it should be an hybrid.

Is there a particular reason the app. is not
being launched using webstart?  The maintenance
costs of a JWS app. will be lower in the long
run.

Well, I hadn't even heard of webstart until now. My main problem is,
after I figure this part out, is how do I connect my applet (or
webstart?) to the server. it's an internet app, but I need more than
just a servlet, jsp. It's for a game. Thanks.
 
A

Arne Vajhøj

jmDesktop said:
Well, I hadn't even heard of webstart until now. My main problem is,
after I figure this part out, is how do I connect my applet (or
webstart?) to the server. it's an internet app, but I need more than
just a servlet, jsp. It's for a game. Thanks.

Your applet will be allowed to connect to the same server it was
fetched from.

HTTP or plain socket as you want.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top