java - How can I switch between jpanels? -
i'm still new java programming, please me correct mistakes might have overlooked or give tips on how improve program.
okay, lot of problems have been solved, , have cardlayout, still have questions how should make pipes show inside it.
when tried add in refresh rate timer , speed timer, have problems how need declare , initialize boolean variables.
also, when compile , run game, files such game$1.class
. there way me clean up, , explain why happens? these have affect on finished product? (when game compiled , packaged jar.)
i want set playerisready true when play button clicked. , there, when if statement true, switch panel displays pipes, , start moving pipe across screen. preferably 3 instances of pipe, each starting @ different times, whatever can fine.
some of code needs work, have commented parts out , left notes.
my other questions game can found here.
this current code
game.java
import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; import javax.swing.border.emptyborder; import javax.swing.swingutilities; public class game { public static void main(string[] args) { runnable r = new runnable() { @override public void run() { // gui seen user (without frame) final cardlayout cl = new cardlayout(); final jpanel gui = new jpanel(cl); // remove if no border needed gui.setborder(new emptyborder(10,10,10,10)); jpanel menu = new jpanel(new gridbaglayout()); jbutton playgame = new jbutton("play!"); actionlistener playgamelistener = new actionlistener() { @override public void actionperformed(actionevent e) { cl.show(gui, "game"); } }; playgame.addactionlistener(playgamelistener); insets margin = new insets(20, 50, 20, 50); playgame.setmargin(margin); menu.add(playgame); gui.add(menu); cl.addlayoutcomponent(menu, "menu"); final jpanel pipes = new pipes(); gui.add(pipes); cl.addlayoutcomponent(pipes, "game"); jframe f = new jframe("pipes game"); f.add(gui); // ensures jvm closes after frame(s) closed , // non-daemon threads finished f.setdefaultcloseoperation(jframe.dispose_on_close); // see https://stackoverflow.com/a/7143398/418556 demo. f.setlocationbyplatform(true); // ensures frame minimum size needs // in order display components within f.pack(); // should done last, avoid flickering, moving, // resizing artifacts. f.setvisible(true); /*if (playerisready) { timer speed = new timer(10, new actionlistener() { //pipe speed @override public void actionperformed(actionevent e) { pipes.move(); } }); speed.start(); timer refresh = new timer(30, new actionlistener() { //refresh rate @override public void actionperformed(actionevent e) { pipes.repaint(); } }); refresh.start(); }*/ } }; // swing guis should created , updated on edt // http://docs.oracle.com/javase/tutorial/uiswing/concurrency swingutilities.invokelater(r); } }
pipes.java
// import(s) need arraylist? public class pipes { list<pipe> pipes = new arraylist<pipe>(); public pipes() { pipes.add(new pipe(50, 100)); pipes.add(new pipe(150, 100)); pipes.add(new pipe(250, 100)); } protected void paintcomponent(graphics g) { super.paintcomponent(g); ( pipe pipe : pipes ){ pipe.drawpipe(g); } } }
pipeobject.java
import java.awt.graphics; public class pipeobject { //declare , initialiaze variables int x1 = 754; //xval start int x2 = 75; //pipe width //total width 83 int y1 = -1; //yval start int y2 = setheightval(); //pipe height int gap = 130; //gap height public void drawpipe(graphics g) { g.clearrect(0,0,750,500); //clear screen g.drawrect(x1,y1,x2,y2); //draw part 1 g.drawrect(x1-3,y2-1,x2+6,25); //draw part 2 g.drawrect(x1-3,y2+25+gap,x2+6,25); //draw part 3 g.drawrect(x1,y2+25+gap+25,x2,500-y2-49-gap); //draw part 4 } public void move() { x1--; } public int getmyx() { //to determine pipe horizontally return x1-3; } public int getmyy() { //to determine pipe vertically return y2+25; } public int setheightval() { //get random number , select preset height int num = (int)(9*math.random() + 1); int val = 0; if (num == 9) { val = 295; } else if (num == 8) { val = 246; } else if (num == 7) { val = 216; } else if (num == 6) { val = 185; } else if (num == 5) { val = 156; } else if (num == 4) { val = 125; } else if (num == 3) { val = 96; } else if (num == 2) { val = 66; } else { val = 25; } return val; } }
the best way approach using cardlayout
.
notes
- a button
actionlistener
far bettermouselistener
on rectangle.- the button show focus when mouse pointed @ it, or component tabbed via keyboard.
- the button keyboard accessible.
- the button has facility support multiple icons built in (e.g. 'initial look', focused, pressed etc.)
- white space in gui provided around menu panel , game adding
emptyborder
- the button made larger setting margin.
- adjust margins, borders , preferred size according need. these sizes set me not make screenshots large.
- see more tips in code comments.
code
here mctare (minimal complete tested , readable example) produced above screenshots.
import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.*; import javax.swing.border.emptyborder; public class pipesgame { public static void main(string[] args) { runnable r = new runnable() { @override public void run() { // gui seen user (without frame) final cardlayout cl = new cardlayout(); final jpanel gui = new jpanel(cl); // remove if no border needed gui.setborder(new emptyborder(10,10,10,10)); jpanel menu = new jpanel(new gridbaglayout()); jbutton playgame = new jbutton("play!"); actionlistener playgamelistener = new actionlistener() { @override public void actionperformed(actionevent e) { cl.show(gui, "game"); } }; playgame.addactionlistener(playgamelistener); insets margin = new insets(20, 50, 20, 50); playgame.setmargin(margin); menu.add(playgame); gui.add(menu); cl.addlayoutcomponent(menu, "menu"); jpanel pipes = new pipes(); gui.add(pipes); cl.addlayoutcomponent(pipes, "game"); jframe f = new jframe("pipes game"); f.add(gui); // ensures jvm closes after frame(s) closed , // non-daemon threads finished f.setdefaultcloseoperation(jframe.dispose_on_close); // see https://stackoverflow.com/a/7143398/418556 demo. f.setlocationbyplatform(true); // ensures frame minimum size needs // in order display components within f.pack(); // should done last, avoid flickering, moving, // resizing artifacts. f.setvisible(true); } }; // swing guis should created , updated on edt // http://docs.oracle.com/javase/tutorial/uiswing/concurrency swingutilities.invokelater(r); } } class pipes extends jpanel { pipes() { setbackground(color.black); setforeground(color.white); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); g.drawstring("pipes game appears here..", 170, 80); } @override public dimension getpreferredsize() { // adjust need return new dimension(500,150); } }
Comments
Post a Comment