java - Typing game not working properly -
i working on typing game in java in worked on moving buttons....i wish when press key , specific button come on specific bounds of frame. not happening. can't understand reason. program not showing error not giving desired output. there should added in this?
import java.awt.event.*; public class screen extends javax.swing.jframe implements keylistener { int x = 0; int y = 500; int x1 = 100; int x2 = 200; int x3 = 300; int x4 = 400; int y1 = 550; int y2 = 600; int y3 = 650; int y4 = 700; int rad = 50; public screen() { initcomponents(); setsize(1500, 800); setvisible(true); } void move1() { if (y < getheight()) y = y - 1; this.jbutton1.setbounds(x, y, rad, rad); if (y == 0) { x = 0; y = 500; this.jbutton1.setbounds(x, y, rad, rad); } if (y1 < getheight()) y1 = y1 - 1; this.jbutton2.setbounds(x1, y1, rad, rad); if (y1 == 0) { x1 = 100; y1 = 550; this.jbutton2.setbounds(x1, y1, rad, rad); } if (y2 < getheight()) y2 = y2 - 1; this.jbutton3.setbounds(x2, y2, rad, rad); if (y2 == 0) { x2 = 200; y2 = 600; this.jbutton3.setbounds(x1, y1, rad, rad); } if (y3 < getheight() - rad) y3 = y3 - 1; this.jbutton4.setbounds(x3, y3, rad, rad); if (y3 == 0) { x3 = 300; y3 = 650; this.jbutton4.setbounds(x1, y1, rad, rad); } if (y4 < getheight() - rad) y4 = y4 - 1; this.jbutton5.setbounds(x4, y4, rad, rad); if (y4 == 0) { x4 = 400; y4 = 700; this.jbutton5.setbounds(x4, y4, rad, rad); } } @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jbutton1 = new javax.swing.jbutton(); jbutton2 = new javax.swing.jbutton(); jbutton3 = new javax.swing.jbutton(); jbutton4 = new javax.swing.jbutton(); jbutton5 = new javax.swing.jbutton(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); getcontentpane().setlayout(null); jbutton1.settext("a"); jbutton1.addkeylistener(new java.awt.event.keyadapter() { public void keyreleased(java.awt.event.keyevent evt) { jbutton1keyreleased(evt); } }); getcontentpane().add(jbutton1); jbutton1.setbounds(20, 163, 60, 50); jbutton2.settext("v"); jbutton2.addkeylistener(new java.awt.event.keyadapter() { public void keyreleased(java.awt.event.keyevent evt) { jbutton2keyreleased(evt); } }); getcontentpane().add(jbutton2); jbutton2.setbounds(130, 180, 39, 23); jbutton3.settext("t"); getcontentpane().add(jbutton3); jbutton3.setbounds(230, 190, 37, 23); jbutton4.settext("k"); getcontentpane().add(jbutton4); jbutton4.setbounds(220, 240, 37, 23); jbutton5.settext("g"); getcontentpane().add(jbutton5); jbutton5.setbounds(380, 250, 39, 23); pack(); }// </editor-fold> private void jbutton1keyreleased(java.awt.event.keyevent evt) { // todo add handling code here: if (evt.getkeychar() == 'a') { // this.jbutton1.setvisible(false); this.jbutton1.setbounds(x, y, rad, rad); } } private void jbutton2keyreleased(java.awt.event.keyevent evt) { // todo add handling code here: // if(evt.getkeychar()=='v'){ // this.jbutton1.setbounds(0, 600, rad, rad); // this.jbutton1.setvisible(false); // } } /** * @param args * command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ // <editor-fold defaultstate="collapsed" desc=" , feel setting // code (optional) "> /* * if nimbus (introduced in java se 6) not available, stay * default , feel. details see * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf. * html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(screen.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(screen.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(screen.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(screen.class.getname()).log(java.util.logging.level.severe, null, ex); } // </editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new screen().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jbutton jbutton1; private javax.swing.jbutton jbutton2; private javax.swing.jbutton jbutton3; private javax.swing.jbutton jbutton4; private javax.swing.jbutton jbutton5; // end of variables declaration @override public void keytyped(keyevent e) { //to change body of generated methods, choose tools | templates. throw new unsupportedoperationexception("not supported yet."); } @override public void keypressed(keyevent e) { // throw new unsupportedoperationexception("not supported yet."); //to // change body of generated methods, choose tools | templates. if (e.getkeychar() == 'a') this.jbutton1.setbounds(x, y, rad, rad); } @override public void keyreleased(keyevent e) { //to change body of generated methods, choose tools | templates. throw new unsupportedoperationexception("not supported yet."); } }
and main method.
public class game { public static void main(string[] args) { screen s = new screen(); while (true) { s.move1(); s.repaint(); try { thread.sleep(6); } catch (exception e) { } } } }
Comments
Post a Comment