java - Confirming Input with multiple inputs from JFrame and JOptionPane -
i creating simulation of harmonograph, , using abstract class paint method. have made 2 separate jframes , jpanels input window asking 8 numbers each. (my main frame , panel contain main ui). have joptionpane.showconfirmdialog
delay reading of variables in panel, confirmation dialog pops before panel starts.
here code:
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.scanner; import java.lang.math; //@mark coats 5/11/16--jadeja 3 public class harm extends java.applet.applet implements actionlistener,mouselistener { int numy = 0; int numx = 0; int numa2,numf2,nump2,numd2; int numa1,numf1,nump1,numd1; int numa4,numf4,nump4,numd4; int numa3,numf3,nump3,numd3; // add scanner scanner key = new scanner(system.in); // add jframe jframe frame = new jframe("harmonograph sim"); //add jpanel jpanel pnlf = new jpanel(); // add canvas canvas c = new canvas(); // new jtextfield jtextfield uin = new jtextfield(15); //add fonts font btnf = new font("serif",font.bold,12); //setup buttons jbutton setx = new jbutton("set p1"); jbutton sety = new jbutton("set p2"); jbutton run = new jbutton("run"); jbutton clear = new jbutton("clr"); jbutton exit = new jbutton("exit"); //end setup buttons public static void main(string[] args) { harm h = new harm(); h.initbut(); } public void initbut() { //setfont setx.setfont(btnf); sety.setfont(btnf); run.setfont(btnf); clear.setfont(btnf); exit.setfont(btnf); //set jframe frame.setsize(1000,650); frame.setbackground(color.black); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.add(pnlf); frame.pack(); pnlf.setbackground(color.black); frame.setvisible(true); //set panel layout pnlf.setlayout(null); //add button locations setx.addactionlistener(this); setx.setbounds(25,25,1,1); setx.setbackground(color.gray); setx.setsize(75,50); pnlf.add(setx); sety.addactionlistener(this); sety.setbounds(25,130,1,1); sety.setbackground(color.gray); sety.setsize(75,50); pnlf.add(sety); run.addactionlistener(this); run.setbounds(25,210,1,1); run.setbackground(color.green); run.setsize(75,50); pnlf.add(run); clear.addactionlistener(this); clear.setbounds(25,290,1,1); clear.setsize(75,50); clear.setbackground(color.gray); pnlf.add(clear); exit.addactionlistener(this); exit.setbounds(25,370,1,1); exit.setsize(75,50); exit.setbackground(color.red); pnlf.add(exit); //other actions uin.addactionlistener(this); } public void key (keyevent e) { } public void actionperformed(actionevent e)//oh crap...here go { object s = e.getsource(); if (s == setx) { jframe n1 = new jframe("set pendulum 1"); jpanel setp1 = new jpanel(); n1.setsize(200,400); n1.setvisible(true); n1.add(setp1); n1.pack(); repaint(); setp1.setopaque(false); setp1.setbackground(color.white); setp1.setsize(200,400); setp1.setvisible(true); jtextfield seta1 = new jtextfield(5); jtextfield setf1 = new jtextfield(5); jtextfield setpe1 = new jtextfield(5); jtextfield setd1 = new jtextfield(5); jtextfield seta2 = new jtextfield(5); jtextfield setf2 = new jtextfield(5); jtextfield setpe2 = new jtextfield(5); jtextfield setd2 = new jtextfield(5); //add textboxes setp1.add(new jlabel("amplitudex:")); setp1.add(seta1); setp1.add(box.createhorizontalstrut(15));//create space setp1.add(new jlabel("frequencyx:")); setp1.add(setf1); setp1.add(box.createhorizontalstrut(15)); setp1.add(new jlabel("phasex:")); setp1.add(setpe1); setp1.add(box.createhorizontalstrut(15)); setp1.add(new jlabel("dampingx:")); setp1.add(setd1); setp1.add(new jlabel("amplitudey:")); setp1.add(seta2); setp1.add(box.createhorizontalstrut(15));//create space setp1.add(new jlabel("frequencyy:")); setp1.add(setf2); setp1.add(box.createhorizontalstrut(15)); setp1.add(new jlabel("phasey:")); setp1.add(setpe2); setp1.add(box.createhorizontalstrut(15)); setp1.add(new jlabel("dampingy:")); setp1.add(setd2); int ok = joptionpane.showconfirmdialog(n1,"enter requested info","confirm",joptionpane.ok_cancel_option); if (ok==joptionpane.ok_option) { //setnums string stra1; string strf1; string strp1; string strd1; stra1 = seta1.gettext(); strf1 = setf1.gettext(); strp1 = setpe1.gettext(); strd1 = setd1.gettext(); numa1 = integer.parseint(stra1); numf1 = integer.parseint(strf1); nump1 = integer.parseint(strp1); numd1 = integer.parseint(strd1); if (numa1 == 0) { joptionpane.showmessagedialog(n1,"error: \n x missing","error",joptionpane.error_message); } } else { system.exit(0); } /* string strx = joptionpane.showinputdialog("set x:"); numx = integer.parseint(strx); */ setx.setbackground(color.green); repaint(); } else if (s == sety) //___________________________________________________________________________________ { jframe n2 = new jframe("set pendulum 2"); jpanel setp2 = new jpanel(); n2.setsize(200,400); n2.setvisible(true); n2.add(setp2); n2.pack(); repaint(); setp2.setopaque(false); setp2.setbackground(color.white); setp2.setsize(200,400); setp2.setvisible(true); jtextfield seta3 = new jtextfield(5); jtextfield setf3 = new jtextfield(5); jtextfield setpe3 = new jtextfield(5); jtextfield setd3 = new jtextfield(5); jtextfield seta4 = new jtextfield(5); jtextfield setf4= new jtextfield(5); jtextfield setpe4 = new jtextfield(5); jtextfield setd4 = new jtextfield(5); //add textboxes setp2.add(new jlabel("amplitudex:")); setp2.add(seta3); setp2.add(box.createhorizontalstrut(15));//create space setp2.add(new jlabel("frequencyx:")); setp2.add(setf3); setp2.add(box.createhorizontalstrut(15)); setp2.add(new jlabel("phasex")); setp2.add(setpe3); setp2.add(box.createhorizontalstrut(15)); setp2.add(new jlabel("dampingx")); setp2.add(setd3); setp2.add(new jlabel("amplitudey:")); setp2.add(seta4); setp2.add(box.createhorizontalstrut(15));//create space setp2.add(new jlabel("frequencyy:")); setp2.add(setf4); setp2.add(box.createhorizontalstrut(15)); setp2.add(new jlabel("phasey:")); setp2.add(setpe4); setp2.add(box.createhorizontalstrut(15)); setp2.add(new jlabel("dampingy:")); setp2.add(setd4); int ok = joptionpane.showconfirmdialog(n2,"enter requested info","confirm",joptionpane.ok_cancel_option); if (ok==joptionpane.ok_option) { //setnums string stra3; string strf3; string strp3; string strd3; string stra4; string strf4; string strp4; string strd4; stra3 = seta3.gettext(); strf3 = setf3.gettext(); strp3 = setpe3.gettext(); strd3 = setd3.gettext(); numa3 = integer.parseint(stra3); numf3 = integer.parseint(strf3); nump3 = integer.parseint(strp3); numd3= integer.parseint(strd3); stra4 = seta4.gettext(); strf4 = setf4.gettext(); strp4 = setpe4.gettext(); strd4 = setd4.gettext(); numa4 = integer.parseint(stra4); numf4 = integer.parseint(strf4); nump4 = integer.parseint(strp4); numd4= integer.parseint(strd4); } else { system.exit(0); } sety.setbackground(color.green); repaint(); } else if (s == run) //______________________________________________________________________vv { jframe drawf = new jframe("simulator"); jpanel simpanel = new jpanel(); drawf.setvisible(true); drawf.setsize(1000,650); drawf.add(simpanel); drawf.pack(); simpanel.setlayout(null); simpanel.setsize(1000,650); simpanel.setvisible(true); simpanel.setbackground(color.black); //add drawing surface //start drawing simpanel.add(new jcomponent() { public void paint(graphics g) { g.setcolor(color.red); int xlast,ylast; for(int i=0;i < 5000000; i++) // 5 million { double dx = (math.e*math.exp(-numd1*i)*math.sin(i*numf1+nump1))+(math.e*math.exp(-numd2*i)*math.sin(i*numf2*nump2)); double dy = (math.e*math.exp(-numd3*i)*math.sin(i*numf3+nump3))+(math.e*math.exp(-numd4*i)*math.sin(i*numf4*nump4)); int drawx = (int)dx; int drawy = (int)dy; if (i==0) { xlast = 0; ylast = 0; g.drawline(xlast,ylast,drawx,drawy); } else { g.drawline(xlast,ylast,drawx,drawy); } } } }); repaint(); } else if (s == clear)//__________________________________________________________________________^^ { if (numy == 0) { joptionpane.showmessagedialog(null,"x has been cleared.","reset complete",joptionpane.plain_message); setx.setbackground(color.gray); sety.setbackground(color.gray); } else if (numx == 0) { joptionpane.showmessagedialog(null,"y has been cleared.","reset complete",joptionpane.plain_message); setx.setbackground(color.gray); sety.setbackground(color.gray); } else { numy = 00; numx = 00; joptionpane.showmessagedialog(null,"x , y have been cleared.","reset complete",joptionpane.plain_message); setx.setbackground(color.gray); sety.setbackground(color.gray); } repaint(); } else if (s == exit)//__________________________________ { system.exit(0); repaint(); } } public void mousepressed(mouseevent e)//________________________________________________________________________________________________________ { e.getx(); e.gety(); } // ignore public void mousereleased(mouseevent e) { } public void mouseclicked(mouseevent e) { } public void mouseentered(mouseevent e) { int a=e.getx(); int b=e.gety(); } public void mouseexited(mouseevent e) { } //end ignore
}
the problem seems coming here somewhere:
object s = e.getsource(); if (s == setx) { jframe n1 = new jframe("set pendulum 1"); jpanel setp1 = new jpanel(); n1.setsize(200,400); n1.setvisible(true); n1.add(setp1); n1.pack(); repaint(); setp1.setopaque(false); setp1.setbackground(color.white); setp1.setsize(200,400); setp1.setvisible(true); jtextfield seta1 = new jtextfield(5); jtextfield setf1 = new jtextfield(5); jtextfield setpe1 = new jtextfield(5); jtextfield setd1 = new jtextfield(5); jtextfield seta2 = new jtextfield(5); jtextfield setf2 = new jtextfield(5); jtextfield setpe2 = new jtextfield(5); jtextfield setd2 = new jtextfield(5); //add textboxes setp1.add(new jlabel("amplitudex:")); setp1.add(seta1); setp1.add(box.createhorizontalstrut(15));//create space setp1.add(new jlabel("frequencyx:")); setp1.add(setf1); setp1.add(box.createhorizontalstrut(15)); setp1.add(new jlabel("phasex:")); setp1.add(setpe1); setp1.add(box.createhorizontalstrut(15)); setp1.add(new jlabel("dampingx:")); setp1.add(setd1); setp1.add(new jlabel("amplitudey:")); setp1.add(seta2); setp1.add(box.createhorizontalstrut(15));//create space setp1.add(new jlabel("frequencyy:")); setp1.add(setf2); setp1.add(box.createhorizontalstrut(15)); setp1.add(new jlabel("phasey:")); setp1.add(setpe2); setp1.add(box.createhorizontalstrut(15)); setp1.add(new jlabel("dampingy:")); setp1.add(setd2); int ok = joptionpane.showconfirmdialog(n1,"enter requested info","confirm",joptionpane.ok_cancel_option); if (ok==joptionpane.ok_option) { //setnums string stra1; string strf1; string strp1; string strd1; stra1 = seta1.gettext(); strf1 = setf1.gettext(); strp1 = setpe1.gettext(); strd1 = setd1.gettext(); numa1 = integer.parseint(stra1); numf1 = integer.parseint(strf1); nump1 = integer.parseint(strp1); numd1 = integer.parseint(strd1); if (numa1 == 0) { joptionpane.showmessagedialog(n1,"error: \n x missing","error",joptionpane.error_message); } } else { system.exit(0); } /* string strx = joptionpane.showinputdialog("set x:"); numx = integer.parseint(strx); */ setx.setbackground(color.green); repaint();
Comments
Post a Comment