Making a graphics component draggable

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

Join Date: Mar 2009
Posts: 62
Reputation: jooa is an unknown quantity at this point 
Solved Threads: 0
jooa jooa is offline Offline
Junior Poster in Training

Re: Making a graphics component draggable

 
0
  #11
Jul 11th, 2009
Thank you, I've done that and it works correctly. However right now what is does is it behaves like a paint brush, painting the first last and the dragging of the mouse. How would I only make visible only one circle moving, rather than the path it takes?

Here if the code so far:

  1. /*
  2.  * RunProgram.java
  3.  *
  4.  * Created on 04 November 2008, 17:46
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9. package finalproject;
  10.  
  11. import java.awt.*;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.MouseEvent;
  15. import java.awt.event.MouseListener;
  16. import java.awt.event.MouseMotionListener;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import javax.swing.*;
  20. import javax.swing.JLabel;
  21. import javax.swing.event.*;
  22.  
  23. /**
  24.  *
  25.  * @author
  26.  */
  27. public class RunProgram extends JPanel implements ActionListener, ChangeListener, MouseListener, MouseMotionListener {
  28.  
  29. static JMenuBar bar;
  30. private JMenuItem fileOpen;
  31. private JPanel phasePanel;
  32. static JPanel contentPane;
  33. private JPanel picture1;
  34. private JPanel picture2;
  35. File file;
  36. private JLabel leftPic;
  37. private JLabel rightPic;
  38. private JPanel toolPanel;
  39. JButton openButton;
  40. JButton t;
  41. private JButton openRight, openLeft;
  42.  
  43. /** Creates a new instance of RunProgram */
  44. public RunProgram() {
  45. setLayout(new BorderLayout());
  46. bar = new JMenuBar();
  47. JMenu fileMenu = new JMenu("File");
  48. bar.add(fileMenu);
  49.  
  50. fileOpen = new JMenuItem("Open");
  51. fileMenu.add(fileOpen);
  52. fileOpen.addActionListener(this);
  53.  
  54. //Create a panel and make it the content pane.
  55. contentPane = new JPanel(new BorderLayout());
  56. contentPane.setBorder(BorderFactory.createRaisedBevelBorder());
  57.  
  58. //create panel to show original pictures
  59. picture1 = new JPanel();
  60. picture1.addMouseListener(this);
  61. picture1.addMouseMotionListener(this);
  62. picture1.setBorder(BorderFactory.createLoweredBevelBorder());
  63. picture1.setPreferredSize(new Dimension(450, 1000));
  64.  
  65. picture2 = new JPanel();
  66. picture2.addMouseListener(this);
  67. picture2.addMouseMotionListener(this);
  68. //picture2.setBackground(Color.GREEN);
  69. picture2.setBorder(BorderFactory.createLoweredBevelBorder());
  70. picture2.setPreferredSize(new Dimension(450, 1000));
  71.  
  72. JPanel controlPanel = new JPanel();
  73. controlPanel.setBorder(BorderFactory.createLoweredBevelBorder());
  74. controlPanel.setPreferredSize(new Dimension(800, 50));
  75. controlPanel.setLayout(new FlowLayout());
  76.  
  77. openLeft = new JButton("<<<Open");
  78. openLeft.addActionListener(this);
  79. openRight = new JButton("Open>>>");
  80. openRight.addActionListener(this);
  81. picture1.add(openLeft);
  82. picture2.add(openRight);
  83.  
  84.  
  85. toolPanel = new JPanel();
  86. toolPanel.setPreferredSize(new Dimension(100, 50));
  87.  
  88. //create panel to show the phases
  89. phasePanel = new JPanel();
  90.  
  91. phasePanel.setBorder(BorderFactory.createRaisedBevelBorder());
  92. phasePanel.setLayout(new FlowLayout());
  93. phasePanel.setPreferredSize(new Dimension(1000, 200));
  94.  
  95. JToolBar toolBar = new JToolBar("Still draggable");
  96. toolBar = new JToolBar(null, JToolBar.VERTICAL);
  97. toolBar.setFloatable(false);
  98.  
  99. //Create the label
  100. JLabel sliderLabel = new JLabel("Number of Phases", JLabel.CENTER);
  101. toolBar.add(sliderLabel, BorderLayout.SOUTH);
  102.  
  103. phaseSlider = new JSlider(1, 6);
  104. phaseSlider.setMajorTickSpacing(1);
  105. phaseSlider.setPaintTicks(true);
  106. phaseSlider.setPaintLabels(true);
  107. phaseSlider.addChangeListener(this);
  108.  
  109. toolBar.add(phaseSlider, BorderLayout.SOUTH);
  110.  
  111. toolBar.setPreferredSize(new Dimension(100, 50));
  112. openButton = new JButton("Open");
  113. toolBar.add(openButton);
  114. openButton.addActionListener(this);
  115.  
  116. contentPane.add(phasePanel, BorderLayout.SOUTH);
  117. contentPane.add(toolBar, BorderLayout.NORTH);
  118. contentPane.add(picture1, BorderLayout.WEST);
  119. contentPane.add(picture2, BorderLayout.EAST);
  120. contentPane.add(bar, BorderLayout.NORTH);
  121. contentPane.add(toolBar, BorderLayout.CENTER);
  122. createPhases(3);
  123. }
  124.  
  125. /**
  126.   * @param args the command line arguments
  127.   */
  128. public static void main(String[] args) {
  129.  
  130. JFrame frame = new JFrame("ImageViewer");
  131. frame.add(new RunProgram());
  132. frame.setSize(1100, 900);
  133. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  134. frame.setJMenuBar(bar);
  135. frame.setContentPane(contentPane);
  136. frame.setVisible(true);
  137. }
  138.  
  139. ArrayList<Point> pointsLeft = new ArrayList<Point>();
  140. ArrayList<Point> pointsRight = new ArrayList<Point>();
  141. int lx, ly, rx, ry;
  142.  
  143. public void mouseClicked(MouseEvent e) {
  144. if (e.getSource() == picture1) {
  145. pointsLeft.add(e.getPoint());
  146. lx = e.getX();
  147. ly = e.getY();
  148. drawCircleLeft(lx, ly);
  149. printPointsLeft();
  150. repaint();
  151. }
  152. if (e.getSource() == picture2) {
  153. pointsRight.add(e.getPoint());
  154. rx = e.getX();
  155. ry = e.getY();
  156. drawCircleRight(rx, ry);
  157. printPointsRight();
  158. repaint();
  159. }
  160. }
  161.  
  162. public void printPointsLeft() {
  163. System.out.println(pointsLeft);
  164. }
  165.  
  166. public void printPointsRight() {
  167. System.out.println(pointsRight);
  168. }
  169.  
  170. public void drawCircleLeft(int x, int y) {
  171. Graphics g1 = picture1.getGraphics();
  172. g1.setColor(Color.RED);
  173. g1.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
  174. g1.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
  175. }
  176.  
  177. public void drawCircleRight(int x, int y) {
  178. Graphics g2 = picture2.getGraphics();
  179. g2.setColor(Color.RED);
  180. g2.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
  181. g2.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
  182. }
  183.  
  184. public void mousePressed(MouseEvent e) {
  185. if (e.getSource() == picture1) {
  186. startPoint = e.getPoint();
  187. lx = startPoint.x;
  188. ly = startPoint.y;
  189. dragging = false;
  190. }
  191. if (e.getSource() == picture1) {
  192. startPoint = e.getPoint();
  193. rx = startPoint.x;
  194. ry = startPoint.y;
  195. dragging = false;
  196. }
  197. }
  198.  
  199. public void mouseReleased(MouseEvent e) {
  200. // System.out.println("we moved to x: " + e.getX() + "y: " + e.getY());
  201. if (e.getSource() == picture1) {
  202. pointsLeft.add(e.getPoint());
  203. drawCircleLeft(lx + (e.getX() - lx), ly + (e.getY() - ly));
  204. printPointsLeft();
  205. repaint();
  206. }
  207. if (e.getSource() == picture2) {
  208. pointsRight.add(e.getPoint());
  209. drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry));
  210. printPointsRight();
  211. repaint();
  212. }
  213. repaint();
  214. }
  215.  
  216.  
  217. public void mouseDragged(MouseEvent e) {
  218. if (!dragging) {//start of a drag evet sequence
  219. //ignore if drag moves less than a pixel or two...
  220. if (Math.abs(startDragX - e.getX()) + Math.abs(startDragY - e.getY()) > 4) {
  221. System.out.println("We are dragging from" + startDragX + " x" + startDragY + " y");
  222. }
  223. dragging = true;
  224. }
  225. if (dragging) {
  226. //System.out.println("We are at" + e.getX() + " x" + e.getY() + " y");
  227. if (e.getSource() == picture1) {
  228. pointsLeft.add(e.getPoint());
  229. drawCircleLeft(lx + (e.getX() - lx), ly + (e.getY() - ly));
  230. printPointsLeft();
  231. repaint();
  232. }
  233. if (e.getSource() == picture2) {
  234. pointsRight.add(e.getPoint());
  235. drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry));
  236. printPointsRight();
  237. repaint();
  238. }
  239. }
  240. repaint();
  241. }
  242.  
  243. public void mouseExited(MouseEvent e) {
  244. }
  245. int x, y;
  246. int radius = 5;
  247. ImageIcon largeImage;
  248. private int startDragX, startDragY;
  249. private boolean draggable = false, dragging = false, dragged = false;
  250. Point startPoint;
  251.  
  252. public void mouseMoved(MouseEvent e) {
  253. }
  254.  
  255. public void mouseEntered(MouseEvent e) {
  256. }
  257. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Making a graphics component draggable

 
0
  #12
Jul 11th, 2009
I guess, in your drawCircle methods you need to clear the previous contents before drawing the circle at its new coords.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 62
Reputation: jooa is an unknown quantity at this point 
Solved Threads: 0
jooa jooa is offline Offline
Junior Poster in Training

Re: Making a graphics component draggable

 
0
  #13
Jul 11th, 2009
I'm still working on the clearing the previous contents. But at the moment I want to have a separate class for running this application and another class for the contents of the frame, however because originally my RunProgram class extends a JPanel and I have the code
  1. frame.setContentPane(contentPane);
to set the contentpane. Now I have change the RunProgram class to extend a JFrame and I have removed the main method and put it in another class, how can I now set the contentPane in a separate class?

Here's the example:

  1. public class RunApplication{
  2. public static void main(String[] args){
  3. RunProgram p = new RunProgram();
  4. p.setSize(1100,900);
  5. p.setVisible(true);
  6. }
  7. }
I need to set the content pane because nothing will show but the frame.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Making a graphics component draggable

 
0
  #14
Jul 11th, 2009
Not enough info for a proper answer, but probably you need to create a public method that sets the content pane, then you can call that method from a different class. (Normally you don't need to set the content pane, JFrame has one by default.)
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 62
Reputation: jooa is an unknown quantity at this point 
Solved Threads: 0
jooa jooa is offline Offline
Junior Poster in Training

Re: Making a graphics component draggable

 
0
  #15
Jul 11th, 2009
I figured out how to do it, but there's still another problem. I may try what you suggested but at the moment instead of creating a panel and making it the contentPane, I used the default contentpane. And now the frame and the contents display. The problem now is when I click on the panel the red circle is drawn then immediately disappears. Do you know why this is?

Thanks

Here is my code, I've removed parts of it to make it easier to read, but it should run.

  1. /*
  2.  * RunProgram.java
  3.  *
  4.  * Created on 04 November 2008, 17:46
  5.  *
  6.  * To change this template, choose Tools | Template Manager
  7.  * and open the template in the editor.
  8.  */
  9. package finalproject;
  10.  
  11. import java.awt.*;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14. import java.awt.event.MouseEvent;
  15. import java.awt.event.MouseListener;
  16. import java.awt.event.MouseMotionListener;
  17. import java.io.File;
  18. import java.util.ArrayList;
  19. import javax.swing.*;
  20. import javax.swing.JLabel;
  21. import javax.swing.event.*;
  22.  
  23. /**
  24.  *
  25.  * @author
  26.  */
  27. public class RunProgram extends JFrame implements ActionListener, ChangeListener, MouseListener, MouseMotionListener {
  28.  
  29. static JMenuBar bar;
  30. private JMenuItem fileOpen;
  31. private JPanel phasePanel;
  32. private JPanel picture1;
  33. private JPanel picture2;
  34. private JFileChooser fileChooser;
  35. File file;
  36. private JLabel leftPic;
  37. private JLabel rightPic;
  38. private JPanel toolPanel;
  39. JButton openButton;
  40. JSlider phaseSlider;
  41. JButton t;
  42. private JButton openRight, openLeft;
  43.  
  44. /** Creates a new instance of RunProgram */
  45. public RunProgram() {
  46. setLayout(new BorderLayout());
  47. bar = new JMenuBar();
  48. JMenu fileMenu = new JMenu("File");
  49. bar.add(fileMenu);
  50.  
  51. fileOpen = new JMenuItem("Open");
  52. fileMenu.add(fileOpen);
  53. fileOpen.addActionListener(this);
  54.  
  55. //create panel to show original pictures
  56. picture1 = new JPanel();
  57. picture1.addMouseListener(this);
  58. picture1.addMouseMotionListener(this);
  59. picture1.setBorder(BorderFactory.createLoweredBevelBorder());
  60. picture1.setPreferredSize(new Dimension(450, 1000));
  61.  
  62. picture2 = new JPanel();
  63. picture2.addMouseListener(this);
  64. picture2.addMouseMotionListener(this);
  65. //picture2.setBackground(Color.GREEN);
  66. picture2.setBorder(BorderFactory.createLoweredBevelBorder());
  67. picture2.setPreferredSize(new Dimension(450, 1000));
  68.  
  69. JPanel controlPanel = new JPanel();
  70. controlPanel.setBorder(BorderFactory.createLoweredBevelBorder());
  71. controlPanel.setPreferredSize(new Dimension(800, 50));
  72. controlPanel.setLayout(new FlowLayout());
  73.  
  74. openLeft = new JButton("<<<Open");
  75. openLeft.addActionListener(this);
  76. openRight = new JButton("Open>>>");
  77. openRight.addActionListener(this);
  78. picture1.add(openLeft);
  79. picture2.add(openRight);
  80.  
  81.  
  82. toolPanel = new JPanel();
  83. toolPanel.setPreferredSize(new Dimension(100, 50));
  84.  
  85. //create panel to show the phases
  86. phasePanel = new JPanel();
  87.  
  88. phasePanel.setBorder(BorderFactory.createRaisedBevelBorder());
  89. phasePanel.setLayout(new FlowLayout());
  90. phasePanel.setPreferredSize(new Dimension(1000, 200));
  91.  
  92. JToolBar toolBar = new JToolBar("Still draggable");
  93. toolBar = new JToolBar(null, JToolBar.VERTICAL);
  94. toolBar.setFloatable(false);
  95.  
  96. //Create the label
  97. JLabel sliderLabel = new JLabel("Number of Phases", JLabel.CENTER);
  98. toolBar.add(sliderLabel, BorderLayout.SOUTH);
  99.  
  100. phaseSlider = new JSlider(1, 6);
  101. phaseSlider.setMajorTickSpacing(1);
  102. phaseSlider.setPaintTicks(true);
  103. phaseSlider.setPaintLabels(true);
  104. phaseSlider.addChangeListener(this);
  105.  
  106. toolBar.add(phaseSlider, BorderLayout.SOUTH);
  107.  
  108. toolBar.setPreferredSize(new Dimension(100, 50));
  109. openButton = new JButton("Open");
  110. toolBar.add(openButton);
  111. openButton.addActionListener(this);
  112.  
  113. getContentPane().add(phasePanel, BorderLayout.SOUTH);
  114. getContentPane().add(toolBar, BorderLayout.NORTH);
  115. getContentPane().add(picture1, BorderLayout.WEST);
  116. getContentPane().add(picture2, BorderLayout.EAST);
  117. getContentPane().add(bar, BorderLayout.NORTH);
  118. getContentPane().add(toolBar, BorderLayout.CENTER);
  119. }
  120.  
  121.  
  122. public void actionPerformed(ActionEvent e) {
  123. }
  124.  
  125. ArrayList<Point> pointsLeft = new ArrayList<Point>();
  126. ArrayList<Point> pointsRight = new ArrayList<Point>();
  127. int lx, ly, rx, ry;
  128.  
  129. public void mouseClicked(MouseEvent e) {
  130. if (e.getSource() == picture1) {
  131. pointsLeft.add(e.getPoint());
  132. lx = e.getX();
  133. ly = e.getY();
  134. drawCircleLeft(lx, ly);
  135. printPointsLeft();
  136. repaint();
  137. }
  138. if (e.getSource() == picture2) {
  139. pointsRight.add(e.getPoint());
  140. rx = e.getX();
  141. ry = e.getY();
  142. drawCircleRight(rx, ry);
  143. printPointsRight();
  144. repaint();
  145. }
  146. }
  147.  
  148. public void printPointsLeft() {
  149. System.out.println(pointsLeft);
  150. }
  151.  
  152. public void printPointsRight() {
  153. System.out.println(pointsRight);
  154. }
  155.  
  156. public void drawCircleLeft(int x, int y) {
  157. Graphics g1 = picture1.getGraphics();
  158. g1.setColor(Color.RED);
  159. g1.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
  160. g1.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
  161. }
  162.  
  163. public void drawCircleRight(int x, int y) {
  164. Graphics g2 = picture2.getGraphics();
  165. g2.setColor(Color.RED);
  166. g2.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
  167. g2.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
  168. }
  169.  
  170. public void mousePressed(MouseEvent e) {
  171. if (e.getSource() == picture1) {
  172. startPoint = e.getPoint();
  173. lx = startPoint.x;
  174. ly = startPoint.y;
  175. dragging = false;
  176. }
  177. if (e.getSource() == picture1) {
  178. startPoint = e.getPoint();
  179. rx = startPoint.x;
  180. ry = startPoint.y;
  181. dragging = false;
  182. }
  183. }
  184.  
  185. public void mouseReleased(MouseEvent e) {
  186. // System.out.println("we moved to x: " + e.getX() + "y: " + e.getY());
  187. if (e.getSource() == picture1) {
  188. pointsLeft.add(e.getPoint());
  189. drawCircleLeft(lx + (e.getX() - lx), ly + (e.getY() - ly));
  190. printPointsLeft();
  191. repaint();
  192. }
  193. if (e.getSource() == picture2) {
  194. pointsRight.add(e.getPoint());
  195. drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry));
  196. printPointsRight();
  197. repaint();
  198. }
  199. repaint();
  200. }
  201.  
  202.  
  203. public void mouseDragged(MouseEvent e) {
  204. if (!dragging) {//start of a drag evet sequence
  205. //ignore if drag moves less than a pixel or two...
  206. if (Math.abs(startDragX - e.getX()) + Math.abs(startDragY - e.getY()) > 4) {
  207. System.out.println("We are dragging from" + startDragX + " x" + startDragY + " y");
  208. }
  209. dragging = true;
  210. }
  211. if (dragging) {
  212. //System.out.println("We are at" + e.getX() + " x" + e.getY() + " y");
  213. if (e.getSource() == picture1) {
  214. pointsLeft.add(e.getPoint());
  215. drawCircleLeft(lx + (e.getX() - lx), ly + (e.getY() - ly));
  216. printPointsLeft();
  217. repaint();
  218. }
  219. if (e.getSource() == picture2) {
  220. pointsRight.add(e.getPoint());
  221. drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry));
  222. printPointsRight();
  223. repaint();
  224. }
  225. }
  226. repaint();
  227. }
  228.  
  229. public void mouseExited(MouseEvent e) {
  230. }
  231. int x, y;
  232. int radius = 5;
  233. ImageIcon largeImage;
  234. private int startDragX, startDragY;
  235. private boolean dragging = false;
  236. Point startPoint;
  237.  
  238. public void mouseMoved(MouseEvent e) {
  239. }
  240.  
  241. public void mouseEntered(MouseEvent e) {
  242. }
  243. }

  1. package finalproject;
  2. /**
  3.  *
  4.  *
  5.  */
  6. public class RunApplication {
  7. public static void main(String[] args) {
  8.  
  9. RunProgram prog = new RunProgram();
  10. prog.setSize(1100, 900);
  11. //prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12. //frame.setJMenuBar(bar);
  13. //prog.setContentPane(contentPane);
  14. prog.show();
  15. //prog.setVisible(true);
  16. }
  17. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Making a graphics component draggable

 
0
  #16
Jul 11th, 2009
Can't immediately see why you have that problem. You'll have to do some more dubugging!
ps: getContentPane().add(... this is not needed since Java 1.5, you can just call add(...
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 62
Reputation: jooa is an unknown quantity at this point 
Solved Threads: 0
jooa jooa is offline Offline
Junior Poster in Training

Re: Making a graphics component draggable

 
0
  #17
Aug 13th, 2009
Dragging the drawn circle is working however when I relase the mouse the final position of the circle is not drawn. I have tried a number of things but they do not work. I think it will be something to do with the mouseReleased() method but I'm am not sure what i'll need to include in the method.

This is the section of code which deals with the drawing etc. Do you know what I'll need to include to get the last circle drawn?

Thank you

  1. private static Image imageto;
  2. ArrayList<Point> pointsLeft = new ArrayList<Point>();
  3. ArrayList<Point> pointsRight = new ArrayList<Point>();
  4. public int lx, ly, rx, ry;
  5. public Point lil;
  6. public Point lil2;
  7.  
  8. public void mouseClicked(MouseEvent e) {
  9. if (e.getSource() == picture1) {
  10. lil = e.getPoint();
  11. pointsLeft.add(lil);
  12. lx = e.getX();
  13. ly = e.getY();
  14. drawCircleLeft(lx, ly);
  15. printPointsLeft();
  16. //repaint();
  17. //picture1.removeAll();
  18. }
  19. if (e.getSource() == picture2) {
  20. lil2 = e.getPoint();
  21. pointsRight.add(lil2);
  22. rx = e.getX();
  23. ry = e.getY();
  24. drawCircleRight(rx, ry);
  25. printPointsRight();
  26. //repaint();
  27. }
  28. //align = new AlignFace(rx, ry, lx, ly);
  29. //align.align();
  30. }
  31.  
  32. public void printPointsLeft() {
  33. System.out.println(pointsLeft);
  34. }
  35.  
  36. public void printPointsRight() {
  37. System.out.println(pointsRight);
  38. }
  39.  
  40. public void drawCircleLeft(int x, int y) {
  41. Graphics g1 = picture1.getGraphics();
  42. g1.setColor(Color.RED);
  43. g1.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
  44. g1.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
  45. }
  46.  
  47. public void drawCircleRight(int x, int y) {
  48. Graphics g2 = picture2.getGraphics();
  49. g2.setColor(Color.RED);
  50. g2.drawOval(x - radius, y - radius, 2 * radius, 2 * radius);
  51. g2.fillOval(x - radius, y - radius, 2 * radius, 2 * radius);
  52. }
  53.  
  54. public void mousePressed(MouseEvent e) {
  55. if (e.getSource() == picture1) {
  56. startPoint = e.getPoint();
  57. lx = startPoint.x;
  58. ly = startPoint.y;
  59. dragging = false;
  60. }
  61. if (e.getSource() == picture2) {
  62. startPoint = e.getPoint();
  63. rx = startPoint.x;
  64. ry = startPoint.y;
  65. dragging = false;
  66. }
  67. }
  68.  
  69. public void mouseReleased(MouseEvent e) {
  70. }
  71.  
  72. public void mouseDragged(MouseEvent e) {
  73. if (!dragging) {
  74. if (Math.abs(startDragX - e.getX()) + Math.abs(startDragY - e.getY()) > 4) {
  75. System.out.println("We are dragging from" + startDragX + " x" + startDragY + " y");
  76. }
  77. dragging = true;
  78. }
  79. if (dragging) {
  80. if (e.getSource() == picture1) {
  81. pointsLeft.add(e.getPoint());
  82. drawCircleLeft(lx + (e.getX() - lx), ly + (e.getY() - ly));
  83. printPointsLeft();
  84. repaint();
  85. }
  86. if (e.getSource() == picture2) {
  87. pointsRight.add(e.getPoint());
  88. drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry));
  89. printPointsRight();
  90. repaint();
  91. }
  92. }
  93. }
  94.  
  95. public void mouseExited(MouseEvent e) {
  96. }
  97.  
  98. int x, y;
  99. int radius = 5;
  100. ImageIcon largeImage;
  101. ImageIcon largeImageL;
  102. ImageIcon largeImageR;
  103. private int startDragX, startDragY;
  104. private boolean dragging = false;
  105. Point startPoint;
  106.  
  107. public void mouseMoved(MouseEvent e) {
  108. }
  109.  
  110. public void mouseEntered(MouseEvent e) {
  111. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,595
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 200
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Making a graphics component draggable

 
0
  #18
Aug 13th, 2009
All you need to do to draw the circle in its final position is call repaint, set the coordinates of where you want the circle to be drawn, then draw the circle at that position. You'd put the method calls to do that inside of mouseReleased.
Last edited by BestJewSinceJC; Aug 13th, 2009 at 8:18 pm.
Out.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 62
Reputation: jooa is an unknown quantity at this point 
Solved Threads: 0
jooa jooa is offline Offline
Junior Poster in Training

Re: Making a graphics component draggable

 
0
  #19
Aug 15th, 2009
Thanks for the help, I have done this:
  1. public void mouseReleased(MouseEvent e) {
  2. if (e.getSource() == picture1) {
  3. drawCircleLeft(e.getPoint().x, e.getPoint().y);
  4. }
  5. if (e.getSource() == picture2) {
  6. drawCircleRight(e.getPoint().x, e.getPoint().y);
  7. }
  8. }

However the problem I know have is that if I click the panel twice and try to drag one of the circles drawn, the other dissappears, the same thing happens when I click on picture1 and then picture2, when I then come to drag a circle on picture1 the circle on picture2 dissappears. Do you know why this is?
Thank you
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,595
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 200
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Making a graphics component draggable

 
0
  #20
Aug 16th, 2009
Yeah, I'm pretty sure it's because by calling repaint, you are calling paintComponent, which I believe clears the screen for you before drawing whatever it draws. So you'll either have to redraw everything that you want on the screen (which would require your programremembering what was there) or you'll have to edit paintComponent method so that it does not "erase" the screen. I could be slightly off in my advice here, but I will check a previous thread of mine that contains the correct info and I'll get back to you later tonight. (But I think this is right)
Last edited by BestJewSinceJC; Aug 16th, 2009 at 8:17 pm.
Out.
Reply With Quote Quick reply to this message  
Reply

Tags
graphics

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC