Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

drawing with the mouse and zooming  RSS feed

 
tony navaratnam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am currently making a basic drawing program. i currently have it so that when the mouse button is pressed its xy co-ordinates are passed to a variable and again when the button is released. these four are then used with drawrect etc to produce the shapes. the canvas is currently a image icon on a jLabel on a scrollplane. I have two questions. is it posble as the mouse dragged across the canvas to show the intermediate shape of what ever is being drawn?. also how do i add a method for zooming the image and is it possible to maintain the same xy co-ordinates of the image and the scrollplane??
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For dragging:
One possibility:-
  • Add MouseMotionListener to whichever Component you are drawing on.
  • In mouseDragged method, set x2 and y2 equal to evt.getX() and evt.getY(). Check whether you need an (int) cast.
  • Find the methods to draw a rectangle, eg g.drawRect(), and repaint()
  • For scaling, try reading the bit about scaling a Graphics2D object in its API documentation.Try using the scale method to alter its size, and the translate method to move the origin of the coordinates. Reverse the scale and translate methods after use so subsequent drawing is the expected size.

    You do get some odd effects by combining rotation and shearing, but you should be all right only using translate and scale.
    [ April 29, 2007: Message edited by: Campbell Ritchie ]
     
    tony navaratnam
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks i'll do that
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!