actionlistener - How to make a section of code start first-Java Small error/last little step -
so task i've been set make animation of lamp. there buttons added different actions such change colour of sphere etc.
code: sphere class
public class sphere extends jpanel { private boolean flashinglights = false; private int x = 168; private int y = 75; private color[] colors = new color[] {color.orange, color.light_gray }; private int colorindex = 0; public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; graphics2d g3 = (graphics2d) g; if (!flashinglights) { rectangle box0 = new rectangle(x+16, y+50,14, 50); g3.setcolor(color.black); g3.draw(box0); g3.fill(box0); rectangle box1 = new rectangle(x+16, y+90,14, 50); g3.setcolor(color.white); g3.draw(box1); g3.fill(box1); rectangle box2 = new rectangle(x+16, y+130,14, 50); g3.setcolor(color.black); g3.draw(box2); g3.fill(box2); rectangle box3 = new rectangle(x+16, y+170,14, 50); g3.setcolor(color.white); g3.draw(box3); g3.fill(box3); rectangle box4 = new rectangle(x+16, y+210,14, 50); g3.setcolor(color.black); g3.draw(box4); g3.fill(box4); rectangle box5 = new rectangle(x+16, y+250,14, 50); g3.setcolor(color.white); g3.draw(box5); g3.fill(box5); rectangle box6 = new rectangle(x+16, y+290,14, 50); g3.setcolor(color.black); g3.draw(box6); g3.fill(box6); rectangle box7 = new rectangle(x+16, y+330,14, 50); g3.setcolor(color.white); g3.draw(box7); g3.fill(box7); rectangle box8 = new rectangle(x+16, y+370,14, 50); g3.setcolor(color.black); g3.draw(box8); g3.fill(box8); rectangle box9 = new rectangle(x+16, y+410,14, 50); g3.setcolor(color.white); g3.draw(box9); g3.fill(box9); rectangle box10 = new rectangle(x+16, y+450,14, 50); g3.setcolor(color.black); g3.draw(box10); g3.fill(box10); g2.setcolor(color.orange); ellipse2d.double ball = new ellipse2d.double(x, y, 50, 50); g2.draw(ball); g2.fill(ball); } else { if(colorindex > colors.length - 1) colorindex = 0; rectangle box0 = new rectangle(x+16, y+50,14, 50); g3.setcolor(color.black); g3.draw(box0); g3.fill(box0); rectangle box1 = new rectangle(x+16, y+90,14, 50); g3.setcolor(color.white); g3.draw(box1); g3.fill(box1); rectangle box2 = new rectangle(x+16, y+130,14, 50); g3.setcolor(color.black); g3.draw(box2); g3.fill(box2); rectangle box3 = new rectangle(x+16, y+170,14, 50); g3.setcolor(color.white); g3.draw(box3); g3.fill(box3); rectangle box4 = new rectangle(x+16, y+210,14, 50); g3.setcolor(color.black); g3.draw(box4); g3.fill(box4); rectangle box5 = new rectangle(x+16, y+250,14, 50); g3.setcolor(color.white); g3.draw(box5); g3.fill(box5); rectangle box6 = new rectangle(x+16, y+290,14, 50); g3.setcolor(color.black); g3.draw(box6); g3.fill(box6); rectangle box7 = new rectangle(x+16, y+330,14, 50); g3.setcolor(color.white); g3.draw(box7); g3.fill(box7); rectangle box8 = new rectangle(x+16, y+370,14, 50); g3.setcolor(color.black); g3.draw(box8); g3.fill(box8); rectangle box9 = new rectangle(x+16, y+410,14, 50); g3.setcolor(color.white); g3.draw(box9); g3.fill(box9); rectangle box10 = new rectangle(x+16, y+450,14, 50); g3.setcolor(color.black); g3.draw(box10); g3.fill(box10); g2.setcolor(colors[colorindex++]); ellipse2d.double ball = new ellipse2d.double(x, y, 50, 50); g2.draw(ball); g2.fill(ball); } } public void chooseflashinglights(){ flashinglights = true; } public void choosesteady(){ flashinglights = false; } public static void main(string[] args) { jframe scframe = new animationviewer(); scframe.settitle("belisha beacon"); scframe.setsize(400, 500); scframe.setdefaultcloseoperation((jframe.exit_on_close)); scframe.setvisible(true); }
}
code: animation viewer class
public class animationviewer extends jframe { jbutton jbtflash = new jbutton("flash"); jbutton jbtsteady = new jbutton("steady"); jpanel bpanel = new jpanel(); sphere sphpanel = new sphere(); timer timer; public animationviewer() { timer = new timer(500, new timerlistener()); this.add(bpanel, borderlayout.south); bpanel.add(jbtflash); bpanel.setlayout(new gridlayout(1, 1)); bpanel.add(jbtsteady); this.add(sphpanel, borderlayout.center); jbtflash.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { sphpanel.chooseflashinglights(); timer.start(); } }); jbtsteady.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { sphpanel.choosesteady(); timer.stop(); sphpanel.repaint(); } }); } class timerlistener implements actionlistener { public void actionperformed(actionevent e) { sphpanel.repaint(); } }
}
the main 2 buttons 2 different things. 1 makes sphere stay solid orange colour (steady) , other makes sphere alternate orange grey. (flashing)
now problem: when start program sphere starts of in steady mode colour solid orange.
i want program start in flashing mode. when click run sphere should straight in flashing stage alternating orange grey straight away.
so how can make can start piece of code first goes straight flashing lights mode?
extract code used switch mode flashing in method, avoid code duplication:
private void flash() { sphpanel.chooseflashinglights(); timer.start(); }
call method action listener:
jbtflash.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { flash(); } });
and call in constructor:
public animationviewer() { // existing code omitted flash(); }
Comments
Post a Comment