By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,296 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,296 IT Pros & Developers. It's quick & easy.

how to draw rectangle on applet based on mousedrageven

P: 14
hi,
i want to draw rectangle based on mousedrag event. if user dragging the mouse, then the rectangle on the applet should increase or decrease basing on current mouse coordinates.
i have the following code.

in the following code i am using SelectionArea class which extends a canvas on which i am performing drawing operation. i am using image variable in this class for double buffering to reduce flickering and to save the applet's previous state(i.e drawing content of applet)

but the code is working fine if i draw first rectangle. if i start to draw second rectangle the previously drawn rectangle is disappearing. i want the previously drawn rectangle to be on the screen

can any one tell me how to solve this.
Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.applet.Applet;
  3. import java.awt.event.*;
  4.  
  5. /* 
  6.  * This displays a framed area.  When the user drags within
  7.  * the area, this program displays a rectangle extending from
  8.  * where the user first pressed the mouse button to the current
  9.  * cursor location.
  10.  */
  11.  
  12. public class RectangleDemo extends Applet {
  13.     SelectionArea drawingPanel;
  14.     Label label;
  15.  
  16.     public void init() {
  17.         GridBagLayout gridBag = new GridBagLayout();
  18.         GridBagConstraints c = new GridBagConstraints();
  19.  
  20.         setLayout(gridBag);
  21.  
  22.         drawingPanel = new SelectionArea(this);
  23.         c.fill = GridBagConstraints.BOTH;
  24.         c.weighty = 1.0;
  25.         c.gridwidth = GridBagConstraints.REMAINDER; //end row
  26.         gridBag.setConstraints(drawingPanel, c);
  27.         add(drawingPanel);
  28.  
  29.         label = new Label("Drag within the framed area.");
  30.         c.fill = GridBagConstraints.HORIZONTAL;
  31.         c.weightx = 1.0;
  32.         c.weighty = 0.0;
  33.         gridBag.setConstraints(label, c);
  34.         add(label);
  35.         drawingPanel.setVisible(true);
  36.  
  37.         validate();
  38.     }
  39.  
  40.     public void paint(Graphics g){
  41.         drawingPanel.repaint();
  42.     }
  43.  
  44.     public void update(Graphics g){
  45.         paint(g);
  46.     }
  47.  
  48.  
  49. }
  50.  
  51. class SelectionArea extends Canvas implements ActionListener, MouseListener, MouseMotionListener{
  52.     Rectangle currentRect;
  53.     RectangleDemo controller;
  54.     //for double buffering
  55.     Image image;
  56.     Graphics offscreen;
  57.     public SelectionArea(RectangleDemo controller) {
  58.         super();
  59.         this.controller = controller;
  60.         addMouseListener(this);
  61.         addMouseMotionListener(this);        
  62.     }
  63.  
  64.     public void actionPerformed(ActionEvent ae){
  65.         repaintoffscreen();
  66.     }
  67.  
  68.     public void repaintoffscreen(){
  69.         image = createImage(this.getWidth(), this.getHeight());
  70.         offscreen = image.getGraphics();
  71.         Dimension d = getSize();
  72.         if(currentRect != null){
  73.             Rectangle box = getDrawableRect(currentRect, d);            
  74.  
  75.             //Draw the box outline.
  76.             offscreen.drawRect(box.x, box.y, box.width - 1, box.height - 1);  
  77.             //repaint();
  78.         }
  79.     }
  80.  
  81.     public void mouseEntered(MouseEvent me) {}
  82.     public void mouseExited(MouseEvent me){ }
  83.     public void mouseClicked(MouseEvent me){}
  84.     public void mouseMoved(MouseEvent me){}
  85.  
  86.     public void mousePressed(MouseEvent me) {        
  87.         currentRect = new Rectangle(me.getX(), me.getY(), 0, 0);
  88.         repaintoffscreen();        
  89.     }
  90.  
  91.     public void mouseDragged(MouseEvent me) {
  92.         System.out.println("here in dragged()");
  93.         currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);
  94.         repaintoffscreen();    
  95.         repaint();
  96.     }
  97.  
  98.     public void mouseReleased(MouseEvent me) {
  99.         currentRect.setSize(me.getX() - currentRect.x, me.getY() - currentRect.y);
  100.         repaintoffscreen();  
  101.         repaint();
  102.     }
  103.  
  104.     public void update(Graphics g){
  105.         paint(g);
  106.     }
  107.  
  108.     public void paint(Graphics g) {
  109.         g.drawImage(image, 0, 0, this);
  110.     }
  111.  
  112.     Rectangle getDrawableRect(Rectangle originalRect, Dimension drawingArea) {
  113.         int x = originalRect.x;
  114.         int y = originalRect.y;
  115.         int width = originalRect.width;
  116.         int height = originalRect.height;
  117.  
  118.         //Make sure rectangle width and height are positive.
  119.         if (width < 0) {
  120.             width = 0 - width;
  121.             x = x - width + 1;
  122.             if (x < 0) {
  123.                 width += x;
  124.                 x = 0;
  125.             }
  126.         }
  127.         if (height < 0) {
  128.             height = 0 - height;
  129.             y = y - height + 1;
  130.             if (y < 0) {
  131.                 height += y;
  132.                 y = 0;
  133.             }
  134.         }
  135.  
  136.         //The rectangle shouldn't extend past the drawing area.
  137.         if ((x + width) > drawingArea.width) {
  138.             width = drawingArea.width - x;
  139.         }
  140.         if ((y + height) > drawingArea.height) {
  141.             height = drawingArea.height - y;
  142.         }
  143.  
  144.         return new Rectangle(x, y, width, height);
  145.     }
  146. }
  147.  
  148.  
also if i run this code on full screen mode then i am seeing that the rectangle is appering on screen only after i released the mouse. but i want the rectangle to be on the screen while dragging the mouse and it should change it's dimension according to the current mouse coordinates.
can any one help me pls.
May 19 '09 #1
Share this Question
Share on Google+
1 Reply


P: 14
hi
i modified my code as follows.
but now the rectangle is visible on the applet only after i release my mouse. but now i am able to draw multiple rectangles. the only problem is i can not see the rectangle while dragging the mouse.
Expand|Select|Wrap|Line Numbers
  1. import java.applet.*;
  2. import java.awt.*;
  3. import java.awt.BorderLayout;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Image;
  7. import java.awt.event.MouseEvent;
  8. import java.awt.event.MouseListener;
  9. import java.awt.event.MouseMotionListener;
  10.  
  11. public class testingDemo extends Applet {    
  12.     private final int RECT_OP = 1;
  13.     public int opStatus = 5;
  14.     private int mousex = 0;
  15.     private int mousey = 0;    
  16.     public boolean dragging = false;
  17.     private int Orx = 0;
  18.     private int Ory = 0;
  19.     private int OrWidth = 0;
  20.     private int OrHeight = 0;
  21.     private int drawX = 0;
  22.     private int drawY = 0;    
  23.     drawingPanel drawPanel;
  24.     public Image image;    
  25.  
  26.     public void init() {
  27.         setLayout(new BorderLayout());
  28.         drawPanel = new drawingPanel();
  29.         drawPanel.setVisible(true);
  30.         this.setBackground(Color.white);
  31.         add(drawPanel, BorderLayout.CENTER);
  32.     }
  33.  
  34.     class drawingPanel extends Panel implements MouseListener, MouseMotionListener {
  35.  
  36.         drawingPanel() {
  37.             addMouseListener(this);
  38.             addMouseMotionListener(this);
  39.         }
  40.  
  41.         public void rectOperation(MouseEvent e){
  42.             mousex = e.getX();
  43.             mousey = e.getY();
  44.             setActualBoundry();
  45.             if(image == null){
  46.                 image = this.createImage(this.getWidth(), this.getHeight());
  47.             }
  48.             Graphics offscreen = image.getGraphics();
  49.             offscreen.setXORMode(Color.white);
  50.             offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
  51.             offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
  52.             repaint();
  53.         }
  54.  
  55.         public boolean mouseHasMoved(MouseEvent e) {
  56.             return (mousex != e.getX() || mousey != e.getY());
  57.         }
  58.  
  59.         public void setActualBoundry() {
  60.             if (mousex < Orx || mousey < Ory) {
  61.                 if (mousex < Orx) {
  62.                     OrWidth = Orx - mousex;
  63.                     drawX = Orx - OrWidth;
  64.                 } else {
  65.                     drawX = Orx;
  66.                     OrWidth = mousex - Orx;
  67.                 }
  68.                 if (mousey < Ory) {
  69.                     OrHeight = Ory - mousey;
  70.                     drawY = Ory - OrHeight;
  71.                 } else {
  72.                     drawY = Ory;
  73.                     OrHeight = mousey - Ory;
  74.                 }
  75.             } else {
  76.                 drawX = Orx;
  77.                 drawY = Ory;
  78.                 OrWidth = mousex - Orx;
  79.                 OrHeight = mousey - Ory;
  80.             }
  81.         }
  82.  
  83.         public void setGraphicalDefaults(MouseEvent e) {
  84.             mousex = e.getX();
  85.             mousey = e.getY();
  86.             Orx = e.getX();
  87.             Ory = e.getY();
  88.             drawX = e.getX();
  89.             drawY = e.getY();
  90.             OrWidth =  0;
  91.             OrHeight = 0;
  92.         }
  93.  
  94.         public void mouseDragged(MouseEvent e) {            
  95.             dragging = true;
  96.             System.out.println("In mousedragged");
  97.             opStatus = RECT_OP;
  98.             rectOperation(e);            
  99.         }
  100.  
  101.         public void mouseReleased(MouseEvent e) {            
  102.             System.out.println("in mouseReleased()");
  103.             dragging = false;
  104.             opStatus = RECT_OP;
  105.             mousex = e.getX();
  106.             mousey = e.getY();
  107.             setActualBoundry();
  108.             releasedRect();            
  109.         }
  110.  
  111.         public void mouseEntered(MouseEvent e) { }
  112.  
  113.         public void releasedRect() {
  114.             System.out.println("in releasedRect()");            
  115.             Graphics offscreen = image.getGraphics();
  116.             offscreen.setXORMode(Color.white);
  117.             offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
  118.             offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
  119.             offscreen.setPaintMode();
  120.             offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
  121.             offscreen.dispose();
  122.             repaint();            
  123.         }
  124.  
  125.         public void mouseClicked(MouseEvent e) {}
  126.  
  127.         public void mouseExited(MouseEvent e) {}
  128.  
  129.         public void mouseMoved(MouseEvent e) {}
  130.  
  131.         public void mousePressed(MouseEvent e) {
  132.             setGraphicalDefaults(e);
  133.             rectOperation(e);
  134.         }
  135.  
  136.         public void paint(Graphics g) {
  137.             g.drawImage(image, 0, 0, this);           
  138.         }
  139.  
  140.         public void update(Graphics g) {
  141.             paint(g);
  142.         }
  143.     }
  144.  
  145.     public void paint(Graphics g) {
  146.         drawPanel.repaint();
  147.     }
  148.  
  149.     public void update(Graphics g) {
  150.         paint(g);
  151.     }
  152. }
  153.  
  154.  
can any one tell me what should i chagne in the above code so that the rectangle will be visible while dragging the mouse.
thanks for any help
May 19 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.