472,958 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

how to draw rectangle on applet based on mousedrageven

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
1 5025
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

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

Similar topics

1
by: dunkonu | last post by:
I am new to VB.net, can anybody help me how to create a form that ask user to enter the "WIDTH" and "HEIGHT" and draw a box display in the textbox? thanks
0
by: Steve | last post by:
I'm using CustomDraw to draw my listview item but there seems to be some basic characteristics of the draw operation that Windows refuses to allow you to change. 1) I want to most the position of...
3
by: Richard | last post by:
I have a requirement to put a GDI style circle or rectangle border around the selected row of a datagrid/ It will overlap into the row above and below the selected row. Doing this in a the OnPaint...
1
by: Praveen | last post by:
Hello, I have a web page which will display the map of a city/place. I have a toolbar which contains 'zoom in' and 'zoom out' buttons. When the user clicks on zoom in/zoom out button, he should...
3
by: Colin McGuire | last post by:
Hi there. I have written a small procedure to draw various shapes on things. A bit of it is shown below. Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics) Select Case...
1
by: Rob Richardson | last post by:
Greetings! I am creating a form that will contain information that will eventually be on a label. The label has a 2-column table with lines separating the cells. I want my form to resemble the...
3
by: Tom | last post by:
Hi Hi i am trying to draw on top of a button on a standard toolbar. All i want to do is draw a small rectangle on the button to represent the selected color. I have tried the folowing code in...
7
by: Mark Ingram | last post by:
Hi, how can i draw a rounded rectange, with a border within a specified area? i.e. if i have a Rectangle with width and height of 100, how can i draw a rectange with 2 pixel border inside of the...
1
by: Jeff Waskiewicz | last post by:
Hello All, I'm trying to solve a nagging problem. The goal is to draw a rectangle over the top of all the other controls on a form. Specifically, over a ChartFX control. The user would draw...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.