Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modify code to run as applet

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I am writing a simple game that currently runs in a window on the desktop JDK1.3. What do I need to do to get this running as an applet in a browser?? Is there an easy way?
Here the beginning of the source:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.BufferedInputStream;


// Create a frame window.
class Game extends Frame {
static Rectangle virtualBounds;
static int screenHeight[] = { 480, 600, 768, 1024, 1050, 1200 };
static Game game;
static Screen screen;
Screen1 screen1 = new Screen1();
Screen2 screen2;
static TextField name;
static Choice opponent, gameChoice, shoice, rChoice;
static Button button, backup, begin,
static MyActionCode ma, mb, mc, me, mr;
static MyItemCode mi, ms, mv, mt;


Game() {
addWindowListener(new MyWindowAdapter());
screen = screen1;
}


public void paint(Graphics g) {
screen.display(g);
}

// Create the window
public static void main(String args[]) {
int i, j;

game = new Game();
game.loadImages();
virtualBounds = new Rectangle();
getScreen1();
}

// Header screen
static void getScreen1() {
game.setLayout( new FlowLayout(FlowLayout.LEFT, 0, virtualBounds.height-(160*virtualBounds.height)/1050) );
game.setBackground(Color.green);

Label label1 = new Label(" First Name:");
name = new TextField(12);
game.add(label1);
game.add(name);
ma = new MyActionCode();
game.name.addActionListener(ma);
Label label2 = new Label(" Number of Opponents:");
opponent = new Choice();
game.add(label2);
game.add(opponent);
opponent.add("4");
opponent.add("5");
opponent.add("6");
mi = new MyItemCode();
opponent.addItemListener(mi);
Label label3 = new Label(" ");
game.add(label3);
button = new Button("Begin play");
game.add(button);
mb = new MyActionCode();
button.addActionListener(mb);
game.setVisible(true);
curY = initialY; }

class Screen {
 
I think you are better than me programing in Java so you are going to understand this example perfectly is an easy one. Of course frames do not run into an applet so what you have to is called the frame from another class that is an applet.



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

public class Dragging extends Applet
{
private myFrame f;
private Button btn;
private TextField nickName;

public void init()
{
btn= new Button("Create");
nickName= new TextField();

setLayout(null);
add(btn);
add(nickName);

btn.reshape(15,9,60,26);
nickName.reshape(15,40,80,23);
}

public boolean action(Event e,Object o)
{
String valid;

valid=nickName.getText();

if (e.target instanceof Button)
{
if(e.target==btn)
{
if (f!=null)
{
f.hide();
f.dispose();
}
if (valid.length()>0)
{
f= new myFrame(nickName.getText());
f.resize(400,400);
f.setResizable(false);
f.show();
}
else
showStatus("Please choose a nickName");
}
return true;
}
return false;
}//fin del procedure acction;

}//fin de la clase
class myFrame extends Frame
{
private TextArea txt1;
private TextField edit1;
private Button b;
private int nleft,ntop;
private String nick;

public myFrame(String s)
{
super(s);
nick=s;
txt1= new TextArea("Chatear, para practicar",10,20);
edit1= new TextField(15);
b= new Button("SEND");
nleft=20;


txt1.setEditable(false);

setBackground(Color.white);
setForeground(Color.black);
txt1.setBackground(Color.white);
setLayout(null);

add(edit1);
add(txt1);
add(b);

b.reshape(160+nleft,278,60,26);
edit1.reshape(nleft,280,120,22);
txt1.reshape(nleft,60,250,200);
// X,Y,Xlen,YLen

resize(200,200);
show();

}

public boolean handleEvent(Event e)
{
if (e.id==Event.WINDOW_DESTROY)
{
hide();
dispose();
return true;
}
return super.handleEvent(e);
}

public boolean action(Event e,Object o)
{
if (e.target instanceof Button)
{
if(e.target==b)
{
txt1.appendText("\n"+nick+" dice: "+edit1.getText());
return true;
}
return false;
}
if (e.target instanceof TextField)
{
if (e.target==edit1)
{
txt1.appendText("\n"+nick+" dice: "+edit1.getText());
edit1.setText("");
return true;
}
return false;
}
return false;
}//fin del procedure acction;

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top