java - mouse location and graphics painting -


this program suppose shift 2 rectangles 10 points right each time click on screen @ x coordinate 300 or greater, doesn't, whats problem?

import javax.imageio.imageio; import javax.swing.*; import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.io.ioexception;  /**  * created intellij idea.  * change template use file | settings | file templates.  */ public class mastermind extends jcomponent implements actionlistener,mouselistener {     //private mouseevent me;     private int screenx=0;     private int screeny=0;     private actionevent e;     private int xx=10;      public mastermind() throws  ioexception {     }      public static void main(string [] args) throws ioexception {         jframe window = new jframe("master mind");         mastermind game= new mastermind();         window.add(game);         window.pack();         window.setlocationrelativeto(null);         window.setdefaultcloseoperation(windowconstants.exit_on_close);         window.setvisible(true);         timer t =   new timer(30, game);         t.start();         window.addmouselistener(game);     }      @override     public dimension getpreferredsize() {         return new dimension(800,600);     }      @override     protected void paintcomponent(graphics g) {         g.setcolor(color.red);         g.drawrect(xx,30,200,200);         g.setcolor(color.red);         g.drawstring("x,y coordinates: "+screenx+" , "+screeny,400,100);          g.setcolor(color.blue);         g.drawrect(xx+3, 33, 194, 194);     }      @override     public void mousepressed(mouseevent me) {         }     @override     public void mousereleased(mouseevent me) {         repaint();     }     @override     public void mouseentered(mouseevent me) {     }     @override     public void mouseexited(mouseevent me) {     }      @override     public void mouseclicked(mouseevent e) {             mouseevent mouseivent = (mouseevent) e;             int screenx = mouseivent.getx();             int screeny = mouseivent.gety();             system.out.println("screen(x,y) = " + screenx + "\t" + screeny);             repaint();     }      @override     public void actionperformed(actionevent e) {         //to change body of implemented methods use file | settings | file templates.         if (screenx>300)  {         xx=xx+10;  }         //timer animation          repaint();     } } 

i new java. please answer in details if may ask. thank all.

these lines should in mouseclicked method rather in actionperformed.

if (screenx > 300) {     xx = xx + 10; } 

immediately before existing repaint method there. ensure x coordinate variable xx updated use later in paintcomponent method.

unrelated make sure invoke super.paintcomponent(g) first statement of paintcomponent


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -