• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

applet

 
murugesan subramani
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create my own object(like rectangle), should be able to
display 10(more than one) of these objects in random anywhere in the applet and they should be able to respond to events(like mousePressed). How to create the object and where to call (in paint?).
muru
 
murugesan subramani
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello! some one help me!
 
Amit Agarwal
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi murugesan,
don't be so impatient.
you will need a Vector to hold your objects. Next you will have to use the java2d api (java.awt.geom pakage). you have a Rectangle2D.Double and a Rectangle2D.Float class. you can use any of these to define your rectangle depending on the precision you want. you can create a new rectangle using new Rectangle2D.Double( x, y, width, height). if you want to store some extra information like the color etc. then you will have t define your own object otherwise you can use the Rectangle2D class itself to be stored on the Vector.
In the paint() method simply clear the draing area and redisplay the contents of the Vector using the Graphics2D.draw() method. Associate a mouse listener with the panel on which you are drawing. in the mousePressed() method of the listener, use Rectangle2D.contains method to find whether the clicked point is inside a rectangle or not. if it returns true, simply update the object in the Vector and call a repaint() for the panel.
Good Luck,
Amit
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic