how to draw rectangle on applet based on mousedragevent

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2009
Posts: 2
Reputation: gigantic is an unknown quantity at this point 
Solved Threads: 0
gigantic gigantic is offline Offline
Newbie Poster

how to draw rectangle on applet based on mousedragevent

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

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.
Last edited by gigantic; May 18th, 2009 at 11:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: how to draw rectangle on applet based on mousedragevent

 
0
  #2
May 19th, 2009
How about storing all of the rectangles, not just the current rectangle, and painting them all on the image (not sure you really want to use an image rather than drawing the rectangles directly onto the applet. Does it really reduce flickering?), rather than just painting the current one? You can add a new rectangle every time mousePressed is called. Add the previous one to a Vector of rectangles, then paint them all onto your image's Graphics object. Keep in mind, the way you have it now, you keep creating a NEW image every time you call repaintoffscreen, so anything you painted on the old images from previous rectangles isn't going to be there anymore. So if you want to keep the old rectangles, I think you need to either keep track of a bunch of rectangles and paint them all each time, or keep the same image and paint the new rectangle onto it, but right now I think you are constantly creating a blank image and painting a single rectangle onto it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 32
Reputation: hardik.rajani is an unknown quantity at this point 
Solved Threads: 9
hardik.rajani's Avatar
hardik.rajani hardik.rajani is offline Offline
Light Poster

Re: how to draw rectangle on applet based on mousedragevent

 
0
  #3
May 19th, 2009
read this and try correct your code yourself:
http://www.tech-recipes.com/rx/1177/...t-and-repaint/
Anything that can go wrong, goes anyway.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: gigantic is an unknown quantity at this point 
Solved Threads: 0
gigantic gigantic is offline Offline
Newbie Poster

Re: how to draw rectangle on applet based on mousedragevent

 
0
  #4
May 19th, 2009
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.
  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. }

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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: how to draw rectangle on applet based on mousedragevent

 
0
  #5
May 19th, 2009
        public void rectOperation(MouseEvent e){
            mousex = e.getX();
            mousey = e.getY();
            setActualBoundry();
            if(image == null){
                image = this.createImage(this.getWidth(), this.getHeight());
            }
            Graphics offscreen = image.getGraphics();
            offscreen.setXORMode(Color.white);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
            repaint();
        }

I've never used XORMode, but it appears that it's visible if you draw the rectangle an odd number of times, and invisible if you draw it an even number of times. You draw the rectangle twice here. Why are you drawing it twice anyway?

Also, your dragging variable is never used. You assign it values, but never do anything with it.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 46
Reputation: zyaday is an unknown quantity at this point 
Solved Threads: 1
zyaday zyaday is offline Offline
Light Poster

Re: how to draw rectangle on applet based on mousedragevent

 
0
  #6
May 21st, 2009
[
import java.applet.*;
import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class testingdemo extends Applet {
private final int RECT_OP = 1;
public int opStatus = 5;
private int mousex = 0;
private int mousey = 0;
public boolean dragging = false;
private int Orx = 0;
private int Ory = 0;
private int OrWidth = 0;
private int OrHeight = 0;
private int drawX = 0;
private int drawY = 0;
drawingPanel drawPanel;
public Image image;

public void init() {
setLayout(new BorderLayout());
drawPanel = new drawingPanel();
drawPanel.setVisible(true);
this.setBackground(Color.white);
add(drawPanel, BorderLayout.CENTER);
}

class drawingPanel extends Panel implements MouseListener, MouseMotionListener {

drawingPanel() {
addMouseListener(this);
addMouseMotionListener(this);
}

public void rectOperation(MouseEvent e){
mousex = e.getX();
mousey = e.getY();
setActualBoundry();
image = null;
//if(image == null){
// image = this.createImage(this.getWidth(), this.getHeight());
// }
Graphics offscreen = image.getGraphics();
offscreen.setXORMode(Color.white);
setActualBoundry();
offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
// offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
repaint();
}

public boolean mouseHasMoved(MouseEvent e) {
return (mousex != e.getX() || mousey != e.getY());
}

public void setActualBoundry() {
if (mousex < Orx || mousey < Ory) {
if (mousex < Orx) {
OrWidth = Orx - mousex;
drawX = Orx - OrWidth;
} else {
drawX = Orx;
OrWidth = mousex - Orx;
}
if (mousey < Ory) {
OrHeight = Ory - mousey;
drawY = Ory - OrHeight;
} else {
drawY = Ory;
OrHeight = mousey - Ory;
}
} else {
drawX = Orx;
drawY = Ory;
OrWidth = mousex - Orx;
OrHeight = mousey - Ory;
}
}

public void setGraphicalDefaults(MouseEvent e) {
mousex = e.getX();
mousey = e.getY();
Orx = e.getX();
Ory = e.getY();
drawX = e.getX();
drawY = e.getY();
OrWidth = 0;
OrHeight = 0;
}

public void mouseDragged(MouseEvent e) {
dragging = true;
System.out.println("In mousedragged");
opStatus = RECT_OP;
rectOperation(e);
}

public void mouseReleased(MouseEvent e) {
System.out.println("in mouseReleased()");
dragging = false;
opStatus = RECT_OP;
mousex = e.getX();
mousey = e.getY();
setActualBoundry();
releasedRect();
}

public void mouseEntered(MouseEvent e) { }

public void releasedRect() {
System.out.println("in releasedRect()");
Graphics offscreen = image.getGraphics();
offscreen.setXORMode(Color.white);
offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
offscreen.setPaintMode();
offscreen.drawRect(drawX, drawY, OrWidth, OrHeight);
offscreen.dispose();
repaint();
}

public void mouseClicked(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseMoved(MouseEvent e) {}

public void mousePressed(MouseEvent e) {
setGraphicalDefaults(e);
rectOperation(e);
}

public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}

public void update(Graphics g) {
paint(g);
}
}

public void paint(Graphics g) {
drawPanel.repaint();
}

public void update(Graphics g) {
paint(g);
}
}] [/code]

check this out, I have done some changes in the
public void rectOperation(MouseEvent e){
function, hope this helps!
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 46
Reputation: zyaday is an unknown quantity at this point 
Solved Threads: 1
zyaday zyaday is offline Offline
Light Poster

Re: how to draw rectangle on applet based on mousedragevent

 
0
  #7
May 21st, 2009
sorry these code tagging never seems to work for me ..any help in that area?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC