java - How to make GUI work in web browser? -
i writing program school final project. wrote whole project frames, find out needs applet. tried converting not work.
source code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.arraylist; import java.applet.applet; public class alteredbattleship extends applet implements actionlistener{ int ro;//holds row input int co;//holds column input int[][] shots=new int[10][10];//holds shots taken boolean displaylose=false;//boolean triggers if game lost boolean displaywin=false;//boolean triggers if game won //the next 6 variables hold how many parts of ships haven't been hit int two=2; int three=3; int four=4; int threetwo=3; int five=5; //boolean triggers if boat sunk boolean twosunk=false; boolean threesunk=false; boolean threetwosunk=false; boolean foursunk=false; boolean fivesunk=false; //holds difficulty boolean b=true; boolean n=false; boolean e=false; arraylist<integer> misses= new arraylist<integer>();//holds missed shots arraylist<integer> hits= new arraylist<integer>();//holds hit shots int shotcount=75;//how many shots player has win int[][] board=new int[10][10];//holds ship positions japplet main =new japplet();//holds graphics textfield input=new textfield(2);//takes input player textfield count=new textfield(2);//displays shots left public void actionperformed(actionevent e) { }; //places 4 long ship public void placefour(){ int fourstartx=(int)(math.random()*5); int fourstarty=(int)(math.random()*9); for(int i=0; i<4; i++){ board[fourstartx+i][fourstarty]=4; } } //places 3 long ship public void placethree(){ int threestartx=(int)(math.random()*9); int threestarty=(int)(math.random()*6); if(board[threestartx][threestarty]==0&& board[threestartx][threestarty+1]==0&&board[threestartx][threestarty+2]==0){ board[threestartx][threestarty]=3; board[threestartx][threestarty+1]=3; board[threestartx][threestarty+2]=3; } else this.placethree(); } //places 2 long ship public void placetwo(){ int twostartx=(int)(math.random()*7); int twostarty=(int)(math.random()*9); if(board[twostartx][twostarty]==0&& board[twostartx+1][twostarty]==0){ board[twostartx][twostarty]=2; board[twostartx+1][twostarty]=2; } else this.placetwo(); } //places second 3 long ship public void placethreetwo(){ int threestartx=(int)(math.random()*6); int threestarty=(int)(math.random()*9); if(board[threestartx][threestarty]==0&& board[threestartx+1][threestarty]==0&&board[threestartx+2][threestarty]==0){ board[threestartx][threestarty]=6; board[threestartx+1][threestarty]=6; board[threestartx+2][threestarty]=6; } else this.placethreetwo(); } //places 5 long ship public void placefive(){ int twostartx=(int)(math.random()*9); int twostarty=(int)(math.random()*4); if(board[twostartx][twostarty]==0&& board[twostartx][twostarty+1]==0&& board[twostartx][twostarty+2]==0&& board[twostartx][twostarty+3]==0&& board[twostartx][twostarty+4]==0){ board[twostartx][twostarty]=5; board[twostartx][twostarty+1]=5; board[twostartx][twostarty+2]=5; board[twostartx][twostarty+3]=5; board[twostartx][twostarty+4]=5; } else this.placefive(); } //this method creates frame , builds interactable parts of gui public void init() { main = new japplet(); drawpanel drawpanel = new drawpanel(); main.add(drawpanel); jpanel holder=new jpanel(); jpanel line=new jpanel(); jpanel space=new jpanel(); space.setmaximumsize(new dimension(50,150)); space.setbackground(color.lightgray); buttongroup level=new buttongroup();//holds difficulty radiobuttons jradiobutton beginner = new jradiobutton();//beginner difficulty button beginner.setselected(true); beginner.setbackground(color.lightgray); beginner.addactionlistener(new actionlistener() { //makes button set difficulty beginner public void actionperformed(actionevent i) { b=true; n=false; e=false; } }); space.add(beginner);//adds button panel jradiobutton normal = new jradiobutton();//normal difficulty button normal.setbackground(color.lightgray); space.add(normal);//adds button panel normal.addactionlistener(new actionlistener() { //makes button set difficulty normal public void actionperformed(actionevent i) { b=false; n=true; e=false; } }); jradiobutton expert = new jradiobutton(); expert.setbackground(color.lightgray); expert.addactionlistener(new actionlistener() {//makes button set difficulty expert public void actionperformed(actionevent i) { b=false; n=false; e=true; } }); space.add(expert);//adds button panel level.add(beginner); level.add(normal); level.add(expert); count.seteditable(false); count.settext(integer.tostring(shotcount)); line.setmaximumsize(new dimension(105,100)); line.setbackground(color.lightgray); line.add(count); holder.setlayout(new boxlayout(holder, boxlayout.y_axis));//creates panel holds other panels holder.setmaximumsize(new dimension(20,50)); jpanel inputhold=new jpanel();//creates panel holds input textfield inputhold.setmaximumsize(new dimension(40,100)); inputhold.add(input);//adds textfield panel //makes input take shot , put through program input.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { string row=input.gettext().substring(0,1);//makes first letter of input row string column=input.gettext().substring(1,input.gettext().length());//makes number portion column ro=row.compareto("a");//makes row number representation co=integer.parseint(column)-1;//makes string int if(row.compareto("a")<10 && integer.parseint(column)<=10&&shots[ro][co]==0){//makes sure shot in bounds , not called if(board[ro][co]==0){//checks miss misses.add(ro); misses.add(co); } else{//makes sure it's hit hits.add(ro); hits.add(co); if(board[ro][co]==2){//checks if hit 2 ship two--; if (two==0)//checks see if 2 sunk twosunk=true; } else if(board[ro][co]==3){//checks if hit 3 ship three--; if (three==0)//checks see if 3 sunk threesunk=true; } else if(board[ro][co]==6){//checks if hit other 3 ship threetwo--; if(threetwo==0)//checks if other 3 ship sunk threetwosunk=true; } else if(board[ro][co]==4){//check if hit 4 ship four--; if (four==0)//check if 4 ship sunk foursunk=true; } else if(board[ro][co]==5){//check if hit 5 ship five--; if(five==0)//check if 5 ship sunk fivesunk=true; } } shotcount--;//takes away shot shots[ro][co]=1;//remembers spot taken shot } if(twosunk&&threesunk&&foursunk&&threetwosunk&&fivesunk){//checks win status displaywin=true; } input.settext("");//resets textfield //frame.repaint();//resets board new shot count.settext(integer.tostring(shotcount));//resets shotcount new number if(shotcount==0){//checks lose status displaylose=true; input.seteditable(false); } } }); jpanel spacer=new jpanel(); jbutton newb=new jbutton("new game");//creates new game button newb.addactionlistener(new actionlistener() { //makes button work public void actionperformed(actionevent i) { //resets variables new game status displaylose=false; displaywin=false; twosunk=false; threesunk=false; threetwosunk=false; foursunk=false; fivesunk=false; two=2; three=3; threetwo=3; four=4; five=5; misses= new arraylist<integer>(); hits= new arraylist<integer>(); if(b) shotcount=75; else if(n) shotcount=65; else if(e) shotcount=50; count.settext(integer.tostring(shotcount)); board=new int[10][10]; shots=new int[10][10]; placefour(); placethree(); placethreetwo(); placefive(); placetwo(); main.repaint(); } }); //adds panels frame spacer.add(newb,borderlayout.north); spacer.setbackground(color.lightgray); inputhold.setbackground(color.lightgray); spacer.setbackground(color.lightgray); spacer.setmaximumsize(new dimension(100,32)); holder.add(space,borderlayout.north); holder.add(line,borderlayout.north); holder.add(inputhold, borderlayout.north); holder.add(spacer,borderlayout.south); holder.setbackground(color.lightgray); main.add(holder,borderlayout.east); main.setvisible(true); //places ships this.placefour(); this.placethree(); this.placetwo(); this.placethreetwo(); this.placefive(); }
and html is: <embed type="application/x-java-applet;version=1.2" code="alteredbattleship.class" width=680 height=400> </embed>
i'm sorry if answer obvious new java , extremely new graphics. thank time
you have made mistake.
public class alteredbattleship extends applet implements actionlistener
should be:
public class alteredbattleship extends japplet implements actionlistener
if applet still doesn't works, please inform.
Comments
Post a Comment