How can I react on Mouse events in a Java applet and then paint accordingly? -


my homework is

write applet draw house shown on left in figure 14-32. when user clicks on door or windows, should close. figure on right shows house door , windows closed.

i want java applet where, if user clicks on rectangle, 1 sudden created , drawn.

here code far.

import javax.swing.*; import java.awt.*; import java.awt.event.*;  public class test2 extends japplet {     private final int currentx = 0;       public void init()     {         addmouselistener(new mymouselistener());     }      public void paint (final graphics g)     {         super.paint (g);          g.drawrect(100, 100, 200, 200);     }      private class mymouselistener extends mouseadapter     {          currentx = e.getx();     } } 

take @ java tutorial | how write mouse listener. determine when , user clicks. once have these (x,y) coordinates can check if lie within window or door , if so, draw else.

sample code:

   public void mouseclicked(mouseevent e) {        int x = e.getx();        int y = e.gety();         //check if (x,y) lie in rectangle        if(x>100 && x<300 && y>100 && y<300){            //set variable , repaint            closedoors = true;            repaint();        }    } 

in paint method, need check if closedoors variable set , if so, draw else.

public void paint (final graphics g){     super.paint (g);     g.drawrect(100, 100, 200, 200);     if(closedoors){         g.fillrect(100, 100, 200, 200);     } } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -