//****************************************************************************** // Matching.java: Applet // //****************************************************************************** import java.applet.*; import java.awt.*; import java.util.*; import java.net.*; //============================================================================== // Main Class for applet Matching // //============================================================================== public class Matching extends Applet { Card[] cd=new Card[19]; int q; int cur; int cur2; int mul; int score; int n2; int n; int matches; Image back; Image front; Image pic1; Image pic2; Image d; Graphics dg; // Matching Class Constructor //-------------------------------------------------------------------------- public Matching() { // TODO: Add constructor code here } // APPLET INFO SUPPORT: // The getAppletInfo() method returns a string describing the applet's // author, copyright date, or miscellaneous information. //-------------------------------------------------------------------------- public String getAppletInfo() { return "Name: Matching\r\n" + "Author: Kevin O'Brien\r\n" + "Created with Microsoft Visual J++ Version 1.0"; } // The init() method is called by the AWT when an applet is first loaded or // reloaded. Override this method to perform whatever initialization your // applet needs, such as initializing data structures, loading images or // fonts, creating frame windows, setting the layout manager, or adding UI // components. //-------------------------------------------------------------------------- public void init() { // If you use a ResourceWizard-generated "control creator" class to // arrange controls in your applet, you may want to call its // CreateControls() method from within this method. Remove the following // call to resize() before adding the call to CreateControls(); // CreateControls() does its own resizing. //---------------------------------------------------------------------- super.init(); Dimension d1=this.size(); d=createImage(d1.width,d1.height); dg=d.getGraphics(); this.back=getImage(getDocumentBase(),"BACK.jpg"); this.front=getImage(getDocumentBase(),"FRONT.jpg"); resize(640,330); this.q=0; this.score=1; this.mul=2; this.cur=0; this.cur2=0; this.matches=0; for (int num=0;num<19;num++){ this.cd[num]=new Card(); if (!(num==0)) { this.cd[num].y=(num/(6))*66; if (num<=6){ this.cd[num].x=50*(num-1); } else { this.cd[num].x=(50*(num-1))-(50*6); if (num>=12) { this.cd[num].x=(50*(num-1))-(50*12); } }//else }//num!0 this.cd[num].content="IMG"+num+".jpg"; if (num%2==0) { this.cd[num].contentmatch="IMG"+(num-1)+".jpg"; } else { this.cd[num].contentmatch="IMG"+(num+1)+".jpg";} } this.cd[6].y=0; this.cd[12].x=5*50; this.cd[12].y=66; this.cd[18].y=2*66; this.pic1=getImage(getDocumentBase(),"1ST.jpg"); this.pic2=getImage(getDocumentBase(),"2ND.jpg"); for (int y=0;y<100;y++){ this.shuffle(); // TODO: Place additional initialization code here } } // Place additional applet clean up code here. destroy() is called when // when you applet is terminating and being unloaded. //------------------------------------------------------------------------- public void destroy() { // TODO: Place applet cleanup code here } // Matching Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { dg.setColor((new Color(98,174,200))); dg.fillRect(0,0,640,330); dg.setColor((new Color(0,0,0))); boolean done=true; dg.drawImage(this.pic1,(6*50+5),2,this); dg.drawImage(this.pic2,(6*50+5),(3*66+5)-25,this); for (int i=1;i<19;i++) { if (this.cd[i].here){ if (this.cd[i].notselected){ dg.drawImage(this.back,this.cd[i].x,this.cd[i].y,this); } else {dg.drawImage(this.front,this.cd[i].x,this.cd[i].y,this); } done=false; } } if (done){ this.pic1=getImage(getDocumentBase(),"GAME.jpg"); this.pic2=getImage(getDocumentBase(),"OVER.jpg"); dg.drawImage(this.pic1,(6*50+5),2,this); dg.drawImage(this.pic2,(6*50+5),(3*66+5)-25,this); } g.drawImage(d,0,0,this); String j=" "+this.score; g.drawString("Match the related concepts to score points!",310,165); g.drawString(("Score:"+j),55,250); j=" "+this.matches; g.drawString("Matches:"+j,170,250); j=" "+this.mul; g.drawString("Score Multiplier:"+j,105,228); g.drawString("-To start a new game reload the page-",40,290); } // The start() method is called when the page containing the applet // first appears on the screen. The AppletWizard's initial implementation // of this method starts execution of the applet's thread. //-------------------------------------------------------------------------- public void start() { // TODO: Place additional applet start code here repaint(); } // The stop() method is called when the page containing the applet is // no longer on the screen. The AppletWizard's initial implementation of // this method stops execution of the applet's thread. //-------------------------------------------------------------------------- public void stop() { } // MOUSE SUPPORT: // The mouseDown() method is called if the mouse button is pressed // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseDown(Event evt, int x, int y) { // TODO: Place applet mouseDown code here return true; } // MOUSE SUPPORT: // The mouseUp() method is called if the mouse button is released // while the mouse cursor is over the applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseUp(Event evt, int x, int y) { // TODO: Place applet mouseUp code here return true; } // MOUSE SUPPORT: // The mouseDrag() method is called if the mouse cursor moves over the // applet's portion of the screen while the mouse button is being held down. //-------------------------------------------------------------------------- public boolean mouseDrag(Event evt, int x, int y) { // TODO: Place applet mouseDrag code here return true; } // MOUSE SUPPORT: // The mouseMove() method is called if the mouse cursor moves over the // applet's portion of the screen and the mouse button isn't being held down. //-------------------------------------------------------------------------- public boolean mouseMove(Event evt, int x, int y) { // TODO: Place applet mouseMove code here return true; } public void update(Graphics g){ paint(g); } // MOUSE SUPPORT: // The mouseEnter() method is called if the mouse cursor enters the // applet's portion of the screen. //-------------------------------------------------------------------------- public boolean handleEvent(Event e){ if (e.id==Event.MOUSE_DOWN){ if (e instanceof Button){ this.init(); return true; }else{ for (int i=1;i<19;i++){ if (this.cd[i].x<=e.x) { if ((this.cd[i].x+50)>=e.x){ if (this.cd[i].y<=e.y) { if ((this.cd[i].y+66)>=e.y) { if (this.cd[i].notselected){this.cd[i].notselected=false; }else {this.cd[i].notselected=false; //this.q=this.q-1; if (this.q==1) { this.mul=1; this.cd[this.cur2].notselected=true; this.cd[i].notselected=true; this.cd[this.cur].notselected=true; this.cur=0; this.cur2=0; this.q=0; // this.score=this.score-5; this.pic1=getImage(getDocumentBase(),"1ST.jpg"); this.pic2=getImage(getDocumentBase(),"2ND.jpg"); return this.handleEvent(e); } } if (this.cd[i].notselected==false){ this.q=this.q+1; if (this.q==1){if (this.cd[i].here){this.pic1=getImage(getDocumentBase(),this.cd[i].content); this.cur=i;}} if (this.q==2){if (this.cd[i].here){this.pic2=getImage(getDocumentBase(),this.cd[i].content); this.cur2=i; } if (this.cd[cur].content.equals(this.cd[i].contentmatch)) { if (this.score>=0) { this.score=mul*this.score;} else {this.score=this.score/mul;} this.mul=mul+1; this.cd[i].here=false; this.cd[cur].here=false; this.q=0; this.score=this.score+5; this.matches=this.matches+1; this.pic1=getImage(getDocumentBase(),"1ST.jpg"); this.pic2=getImage(getDocumentBase(),"2ND.jpg"); } }if (this.q>2) { this.mul=1; this.cd[this.cur2].notselected=true; this.score=this.score-1; this.cd[i].notselected=true; this.cd[this.cur].notselected=true; this.cur=0; this.cur2=0; this.q=0; this.pic1=getImage(getDocumentBase(),"1ST.jpg"); this.pic2=getImage(getDocumentBase(),"2ND.jpg"); return this.handleEvent(e); } } } } } } }//for repaint(); return true; } } return super.handleEvent(e); } public boolean mouseEnter(Event evt, int x, int y) { // TODO: Place applet mouseEnter code here return true; } // MOUSE SUPPORT: // The mouseExit() method is called if the mouse cursor leaves the // applet's portion of the screen. //-------------------------------------------------------------------------- public boolean mouseExit(Event evt, int x, int y) { // TODO: Place applet mouseExit code here return true; } // TODO: Place additional applet code here void shuffle() { //Random t=new Random(); int n; //n=t.nextInt(); //n2=t.nextInt(); int n2; double d; double d2; d=Math.random()*19; d2=Math.random()*19; Double db=new Double(d); Double db2=new Double(d2); n=db.intValue(); n2=db2.intValue(); this.n=n; this.n2=n2; minishuffle(n,n2); }//end shuffle. public void minishuffle(int n, int n2) { if(!(n==0)){ if (!(n2==0)) { Card temp=new Card(); temp.content=this.cd[n2].content; temp.contentmatch=this.cd[n2].contentmatch; this.cd[n2].content=this.cd[n].content; this.cd[n2].contentmatch=this.cd[n].contentmatch; this.cd[n].content=temp.content; this.cd[n].contentmatch=temp.contentmatch; } } } } class Card { String content; String contentmatch; int x; int y; boolean notselected; boolean here; Card(){ this.notselected=true; this.here=true; this.x=0; this.y=0; this.content="default"; this.contentmatch="default"; } }//end card