java - ReboundPanel Inheritance -
the happy face im using----)this project wants me modify rebound program chapter such when mouse button clicked animation stops, , when clicked again animation resumes.
when click on screen moving smiley face, doesnt stop when click nor start again because couldnt stop smiley face moving doing wrong? here problem area.------) |
private class reboundmouselistener implements mouselistener { public void mouseclicked(mouseevent event) { if (timer.isrunning()) timer.stop(); else timer.start(); } } public void mouseentered(mouseevent event) {} public void mouseexited(mouseevent event) {} public void mousepressed(mouseevent event) {} public void mousereleased(mouseevent event) {} }
here rest of code:
public class reboundpanel extends jpanel { private final int width =300, height= 100; private final int delay= 20, image_size=35; private imageicon image; private timer timer; private int x, y, movex, movey; //--------------------------------------------------------- // sets panel,including timer animation. //--------------------------------------------------------- public reboundpanel(){ timer= new timer(delay, new reboundlistener()); image= new imageicon("happyface.gif"); x=0; y=40; movex=movey=3; setpreferredsize(new dimension(width, height)); setbackground(color.black); timer.start(); } //--------------------------------------------------------- // draws image in current location. //--------------------------------------------------------- public void paintcomponent(graphics page) { super.paintcomponent(page); image.painticon(this, page, x, y); } //********************************************************* // represents action listener timer. //********************************************************* private class reboundlistener implements actionlistener { //-------------------------------------------------------- // updates position of image , possibly direction // of movement whenever timer fires action event. //-------------------------------------------------------- public void actionperformed(actionevent event) { x += movex; y += movey; if (x <=0 || x >= width-image_size) movex =movex * -1; if (y <=0 || y >= height-image_size) movey = movey * -1; repaint(); } } private class reboundmouselistener implements mouselistener { //-------------------------------------------------------------- // stops or starts timer (and therefore animation) // when mouse button clicked. //-------------------------------------------------------------- public void mouseclicked(mouseevent event) { if (timer.isrunning()) timer.stop(); else timer.start(); } //-------------------------------------------------------------- // provide empty definitions unused event methods. //-------------------------------------------------------------- public void mouseentered(mouseevent event) {} public void mouseexited(mouseevent event) {} public void mousepressed(mouseevent event) {} public void mousereleased(mouseevent event) {} } }
public class rebound { public static void main(string[] args) { jframe frame = new jframe("rebound"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new reboundpanel()); frame.pack(); frame.setvisible(true); } }
looks missing addmouselistener()
calls:
public reboundpanel() { // other initializations ... addmouselistener(new reboundmouselistener()); // <-- add timer.start(); }
Comments
Post a Comment