| | |
Making a graphics component draggable
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 62
Reputation:
Solved Threads: 0
Hi, I have written some code which when a mouse is clicked, a red circle is drawn at that position. The x,y coordinates are stored in an ArrayList. What I now want to do is enable the circles to be selected and then dragged. When the mouse is released the new coordinates need to replace the old coordinates. I hope someone can help! thanks
Java Syntax (Toggle Plain Text)
ArrayList<Point> pointsLeft = new ArrayList<Point>(); ArrayList<Point> pointsRight = new ArrayList<Point>(); public void mouseClicked(MouseEvent e) { if (e.getSource() == picture1) { pointsLeft.add(e.getPoint()); drawCircleLeft(e.getX(), e.getY()); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(e.getX(), e.getY()); printPointsRight(); repaint(); } } public void printPointsLeft() { System.out.println(pointsLeft); } public void printPointsRight() { System.out.println(pointsRight); } public void drawCircleLeft(int x, int y) { Graphics g1 = picture1.getGraphics(); g1.setColor(Color.RED); g1.drawOval(x - radius, y - radius, 2 * radius, 2 * radius); g1.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); } int x, y; int radius = 5; public void drawCircleRight(int x, int y) { Graphics g2 = picture2.getGraphics(); g2.setColor(Color.RED); g2.drawOval(x - radius, y - radius, 2 * radius, 2 * radius); g2.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); } public void mousePressed(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseDragged(MouseEvent e) {} public void mouseMoved(MouseEvent e) {}
•
•
Join Date: Apr 2008
Posts: 996
Reputation:
Solved Threads: 146
Here (basically unedited) is some code from an old program I wrote that allows an object to be dragged around the screen. Perhaps you can use it as a start point?
JAVA Syntax (Toggle Plain Text)
private int startDragX, startDragY; private boolean draggable = false, dragging = false, dragged = false; public void mousePressed(MouseEvent e) { startDragX = e.getX(); // in case this is the start of a drag startDragY = e.getY(); dragging = false; // System.out.println(startDragX + " x " + startDragY); } public void mouseReleased(MouseEvent e) { // System.out.println("we had a move to " + e.getX() + " x " e.getY()); this.setLocation(this.getX() + (e.getX() - startDragX), this.getY() + (e.getY() - startDragY)); repaint(); } public void mouseDragged(MouseEvent e) { if (!dragging) { // this is the start of a drag event sequence // ignore if drag moves less than a pixel or two... if (Math.abs(startDragX - e.getX()) + Math.abs(startDragY - e.getY()) > 4) { // System.out.println("we are dragging from " + startDragX + " x // " + startDragY); } dragging = true; } if (dragging) { // drag in progress // System.out.println("we are at " + e.getX() + " x " + e.getY()); this.setLocation(this.getX() + (e.getX() - startDragX), this.getY() + (e.getY() - startDragY)); // for user feedback } }
•
•
Join Date: Mar 2009
Posts: 62
Reputation:
Solved Threads: 0
This is the code I have, however I am still unable to drag the circles I have drawn. This isn't the complete code. Thanks!
Java Syntax (Toggle Plain Text)
ArrayList<Point> pointsLeft = new ArrayList<Point>(); ArrayList<Point> pointsRight = new ArrayList<Point>(); public void mouseClicked(MouseEvent e) { if (e.getSource() == picture1) { pointsLeft.add(e.getPoint()); drawCircleLeft(e.getX(), e.getY()); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(e.getX(), e.getY()); printPointsRight(); repaint(); } } public void printPointsLeft() { System.out.println(pointsLeft); } public void printPointsRight() { System.out.println(pointsRight); } public void drawCircleLeft(int x, int y) { Graphics g1 = picture1.getGraphics(); g1.setColor(Color.RED); g1.drawOval(x - radius, y - radius, 2 * radius, 2 * radius); g1.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); } public void drawCircleRight(int x, int y) { Graphics g2 = picture2.getGraphics(); g2.setColor(Color.RED); g2.drawOval(x - radius, y - radius, 2 * radius, 2 * radius); g2.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); } public void mousePressed(MouseEvent e) { startDragX = e.getX(); startDragY = e.getY(); dragging = false; } public void mouseReleased(MouseEvent e) { this.setLocation(this.getX() + (e.getX() - startDragX), this.getY() + (e.getY() - startDragY)); repaint(); } public void mouseDragged(MouseEvent e) { if (!dragging){ if(Math.abs(startDragX - e.getX()) + Math.abs(startDragY - e.getY())> 4){ System.out.println("We are dragging from" + startDragX + " x" + startDragY + " y" ); } dragging = true; } if (dragging){ System.out.println("We are at" + e.getX() + " x" + e.getY() + " y"); this.setLocation(this.getX() + (e.getX() - startDragX), this.getY() + (e.getY() - startDragY)); } repaint(); } public void mouseExited(MouseEvent e) { } int x, y; int radius = 5; ImageIcon largeImage; private int startDragX, startDragY; private boolean draggable = false, dragging = false, dragged = false; public void mouseMoved(MouseEvent e) {} public void mouseEntered(MouseEvent e) { }
•
•
Join Date: Apr 2008
Posts: 996
Reputation:
Solved Threads: 146
•
•
•
•
Moving an actual component would trigger a repaint automatically, but it looks like he is "dragging" a shape that's been painted on the component, which would require a repaint() to update the location.
OP: In the new mouse events you need to re-draw your circle at the new coords - this should replace my this.setLocation calls
Last edited by JamesCherrill; Jul 10th, 2009 at 3:58 am.
•
•
Join Date: Mar 2009
Posts: 62
Reputation:
Solved Threads: 0
I have adapted the code to redraw the circle at the new positions, however what happens I can click the panel and the circle will be drawn, but when I click the panel again to drag the circle a line of circles are drawn from the left hand corner of the panel, not where I clicked the mouse. Also what I want is to click a cirlce then be able to hold it and move it to another position, while all the time that circle which was originally drawn is still visible.
Java Syntax (Toggle Plain Text)
/* * RunProgram.java * * Created on 04 November 2008, 17:46 * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package finalproject; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.io.File; import java.util.ArrayList; import javax.swing.*; import javax.swing.JLabel; import javax.swing.event.*; /** * * @author */ public class RunProgram extends JPanel implements ActionListener, ChangeListener, MouseListener, MouseMotionListener { static JMenuBar bar; private JMenuItem fileOpen; private JPanel phasePanel; static JPanel contentPane; private JPanel picture1; private JPanel picture2; private JFileChooser fileChooser; File file; private JLabel leftPic; private JLabel rightPic; private JPanel toolPanel; JButton openButton; JSlider phaseSlider; JButton t; private JButton openRight, openLeft; /** Creates a new instance of RunProgram */ public RunProgram() { setLayout(new BorderLayout()); bar = new JMenuBar(); JMenu fileMenu = new JMenu("File"); bar.add(fileMenu); fileOpen = new JMenuItem("Open"); fileMenu.add(fileOpen); fileOpen.addActionListener(this); //Create a panel and make it the content pane. contentPane = new JPanel(new BorderLayout()); contentPane.setBorder(BorderFactory.createRaisedBevelBorder()); //create panel to show original pictures picture1 = new JPanel(); picture1.addMouseListener(this); picture1.addMouseMotionListener(this); // picture1.setBackground(Color.BLUE); picture1.setBorder(BorderFactory.createLoweredBevelBorder()); picture1.setPreferredSize(new Dimension(450, 1000)); picture2 = new JPanel(); picture2.addMouseListener(this); picture2.addMouseMotionListener(this); //picture2.setBackground(Color.GREEN); picture2.setBorder(BorderFactory.createLoweredBevelBorder()); picture2.setPreferredSize(new Dimension(450, 1000)); JPanel controlPanel = new JPanel(); //controlPanel.setBackground(Color.GRAY); controlPanel.setBorder(BorderFactory.createLoweredBevelBorder()); controlPanel.setPreferredSize(new Dimension(800, 50)); controlPanel.setLayout(new FlowLayout()); openLeft = new JButton("<<<Open"); openLeft.addActionListener(this); openRight = new JButton("Open>>>"); openRight.addActionListener(this); picture1.add(openLeft); picture2.add(openRight); toolPanel = new JPanel(); toolPanel.setPreferredSize(new Dimension(100, 50)); //create panel to show the phases phasePanel = new JPanel(); //JScrollPane scroller = new JScrollPane(phasePanel); //phasePanel.setBackground(Color.DARK_GRAY); phasePanel.setBorder(BorderFactory.createRaisedBevelBorder()); phasePanel.setLayout(new FlowLayout()); phasePanel.setPreferredSize(new Dimension(1000, 200)); JToolBar toolBar = new JToolBar("Still draggable"); toolBar = new JToolBar(null, JToolBar.VERTICAL); toolBar.setFloatable(false); //Create the label JLabel sliderLabel = new JLabel("Number of Phases", JLabel.CENTER); toolBar.add(sliderLabel, BorderLayout.SOUTH); toolBar.setPreferredSize(new Dimension(100, 50)); openButton = new JButton("Open"); toolBar.add(openButton); openButton.addActionListener(this); JButton p = new JButton(); p.setPreferredSize(new Dimension(150, 150)); JButton q = new JButton(); q.setPreferredSize(new Dimension(150, 150)); JButton r = new JButton(); r.setPreferredSize(new Dimension(150, 150)); JButton s = new JButton(); s.setPreferredSize(new Dimension(150, 150)); contentPane.add(phasePanel, BorderLayout.SOUTH); contentPane.add(toolBar, BorderLayout.NORTH); contentPane.add(picture1, BorderLayout.WEST); contentPane.add(picture2, BorderLayout.EAST); contentPane.add(bar, BorderLayout.NORTH); contentPane.add(toolBar, BorderLayout.CENTER); createPhases(3); } /** * @param args the command line arguments */ public static void main(String[] args) { JFrame frame = new JFrame("ImageViewer"); frame.add(new RunProgram()); frame.setSize(1100, 900); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setJMenuBar(bar); frame.setContentPane(contentPane); frame.setVisible(true); } /** Listen to the slider. */ public void stateChanged(ChangeEvent e) { } ArrayList<Point> pointsLeft = new ArrayList<Point>(); ArrayList<Point> pointsRight = new ArrayList<Point>(); public void mouseClicked(MouseEvent e) { if (e.getSource() == picture1) { pointsLeft.add(e.getPoint()); drawCircleLeft(e.getX(), e.getY()); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(e.getX(), e.getY()); printPointsRight(); repaint(); } } public void printPointsLeft() { System.out.println(pointsLeft); } public void printPointsRight() { System.out.println(pointsRight); } public void drawCircleLeft(int x, int y) { Graphics g1 = picture1.getGraphics(); g1.setColor(Color.RED); g1.drawOval(x - radius, y - radius, 2 * radius, 2 * radius); g1.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); } public void drawCircleRight(int x, int y) { Graphics g2 = picture2.getGraphics(); g2.setColor(Color.RED); g2.drawOval(x - radius, y - radius, 2 * radius, 2 * radius); g2.fillOval(x - radius, y - radius, 2 * radius, 2 * radius); } public void mousePressed(MouseEvent e) { startDragX = e.getX(); startDragY = e.getY(); dragging = false; } public void mouseReleased(MouseEvent e) { // System.out.println("we moved to x: " + e.getX() + "y: " + e.getY()); if (e.getSource() == picture1) { pointsLeft.add(e.getPoint()); drawCircleLeft(this.getX() + (e.getX() - startDragX), this.getY() + (e.getY() - startDragY)); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(this.getX() + (e.getX() - startDragX), this.getY() + (e.getY() - startDragY)); printPointsRight(); repaint(); } repaint(); } public void mouseDragged(MouseEvent e) { if (!dragging){//start of a drag evet sequence //ignore if drag moves less than a pixel or two... if(Math.abs(startDragX - e.getX()) + Math.abs(startDragY - e.getY())> 4){ System.out.println("We are dragging from" + startDragX + " x" + startDragY + " y" ); } dragging = true; } if (dragging){ //System.out.println("We are at" + e.getX() + " x" + e.getY() + " y"); if (e.getSource() == picture1) { pointsLeft.add(e.getPoint()); drawCircleLeft(this.getX() + (e.getX() - startDragX), this.getY() + (e.getY() - startDragY)); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(this.getX() + (e.getX() - startDragX), this.getY() + (e.getY() - startDragY)); printPointsRight(); repaint(); } } repaint(); } public void mouseExited(MouseEvent e) { } int x, y; int radius = 5; ImageIcon largeImage; private int startDragX, startDragY; private boolean draggable = false, dragging = false, dragged = false; public void mouseMoved(MouseEvent e) {} public void mouseEntered(MouseEvent e) { } }
•
•
Join Date: Apr 2008
Posts: 996
Reputation:
Solved Threads: 146
drawCircleLeft(this.getX() + (e.getX() - startDragX), this.getY()
in this line (and any others like it) you need to replace the this.getX() with the original X coord of your circle, ditto the Y coord. You need to store the original coords in the mousePressed event. What we are doing here is computing the latest position of the circle as original position + (distance the mouse has moved)
in this line (and any others like it) you need to replace the this.getX() with the original X coord of your circle, ditto the Y coord. You need to store the original coords in the mousePressed event. What we are doing here is computing the latest position of the circle as original position + (distance the mouse has moved)
![]() |
Similar Threads
- for newbies, what header file to use for graphics? (C++)
- review my link directory (Website Reviews)
- Displaying Label over other controls (C#)
- project idea's (Geeks' Lounge)
- Amazing Website Designs - How do they do this? (Site Layout and Usability)
- DreamweaverKit.com launched (Post your Resume)
Other Threads in the Java Forum
- Previous Thread: Dragging a circle
- Next Thread: Adding Articles to database
| Thread Tools | Search this Thread |
Tag cloud for graphics







