| | |
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
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:
Here if the code so far:
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; File file; private JLabel leftPic; private JLabel rightPic; private JPanel toolPanel; JButton openButton; 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.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.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(); 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); phaseSlider = new JSlider(1, 6); phaseSlider.setMajorTickSpacing(1); phaseSlider.setPaintTicks(true); phaseSlider.setPaintLabels(true); phaseSlider.addChangeListener(this); toolBar.add(phaseSlider, BorderLayout.SOUTH); toolBar.setPreferredSize(new Dimension(100, 50)); openButton = new JButton("Open"); toolBar.add(openButton); openButton.addActionListener(this); 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); } ArrayList<Point> pointsLeft = new ArrayList<Point>(); ArrayList<Point> pointsRight = new ArrayList<Point>(); int lx, ly, rx, ry; public void mouseClicked(MouseEvent e) { if (e.getSource() == picture1) { pointsLeft.add(e.getPoint()); lx = e.getX(); ly = e.getY(); drawCircleLeft(lx, ly); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); rx = e.getX(); ry = e.getY(); drawCircleRight(rx, ry); 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) { if (e.getSource() == picture1) { startPoint = e.getPoint(); lx = startPoint.x; ly = startPoint.y; dragging = false; } if (e.getSource() == picture1) { startPoint = e.getPoint(); rx = startPoint.x; ry = startPoint.y; 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(lx + (e.getX() - lx), ly + (e.getY() - ly)); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry)); 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(lx + (e.getX() - lx), ly + (e.getY() - ly)); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry)); 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; Point startPoint; public void mouseMoved(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } }
•
•
Join Date: Mar 2009
Posts: 62
Reputation:
Solved Threads: 0
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
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:
I need to set the content pane because nothing will show but the frame.
Java Syntax (Toggle Plain Text)
frame.setContentPane(contentPane);
Here's the example:
Java Syntax (Toggle Plain Text)
public class RunApplication{ public static void main(String[] args){ RunProgram p = new RunProgram(); p.setSize(1100,900); p.setVisible(true); } }
•
•
Join Date: Mar 2009
Posts: 62
Reputation:
Solved Threads: 0
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.
Thanks
Here is my code, I've removed parts of it to make it easier to read, but it should run.
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 JFrame implements ActionListener, ChangeListener, MouseListener, MouseMotionListener { static JMenuBar bar; private JMenuItem fileOpen; private JPanel phasePanel; 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 panel to show original pictures picture1 = new JPanel(); picture1.addMouseListener(this); picture1.addMouseMotionListener(this); 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.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(); 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); phaseSlider = new JSlider(1, 6); phaseSlider.setMajorTickSpacing(1); phaseSlider.setPaintTicks(true); phaseSlider.setPaintLabels(true); phaseSlider.addChangeListener(this); toolBar.add(phaseSlider, BorderLayout.SOUTH); toolBar.setPreferredSize(new Dimension(100, 50)); openButton = new JButton("Open"); toolBar.add(openButton); openButton.addActionListener(this); getContentPane().add(phasePanel, BorderLayout.SOUTH); getContentPane().add(toolBar, BorderLayout.NORTH); getContentPane().add(picture1, BorderLayout.WEST); getContentPane().add(picture2, BorderLayout.EAST); getContentPane().add(bar, BorderLayout.NORTH); getContentPane().add(toolBar, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { } ArrayList<Point> pointsLeft = new ArrayList<Point>(); ArrayList<Point> pointsRight = new ArrayList<Point>(); int lx, ly, rx, ry; public void mouseClicked(MouseEvent e) { if (e.getSource() == picture1) { pointsLeft.add(e.getPoint()); lx = e.getX(); ly = e.getY(); drawCircleLeft(lx, ly); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); rx = e.getX(); ry = e.getY(); drawCircleRight(rx, ry); 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) { if (e.getSource() == picture1) { startPoint = e.getPoint(); lx = startPoint.x; ly = startPoint.y; dragging = false; } if (e.getSource() == picture1) { startPoint = e.getPoint(); rx = startPoint.x; ry = startPoint.y; 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(lx + (e.getX() - lx), ly + (e.getY() - ly)); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry)); 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(lx + (e.getX() - lx), ly + (e.getY() - ly)); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry)); printPointsRight(); repaint(); } } repaint(); } public void mouseExited(MouseEvent e) { } int x, y; int radius = 5; ImageIcon largeImage; private int startDragX, startDragY; private boolean dragging = false; Point startPoint; public void mouseMoved(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } }
Java Syntax (Toggle Plain Text)
package finalproject; /** * * */ public class RunApplication { public static void main(String[] args) { RunProgram prog = new RunProgram(); prog.setSize(1100, 900); //prog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //frame.setJMenuBar(bar); //prog.setContentPane(contentPane); prog.show(); //prog.setVisible(true); } }
•
•
Join Date: Mar 2009
Posts: 62
Reputation:
Solved Threads: 0
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
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
Java Syntax (Toggle Plain Text)
private static Image imageto; ArrayList<Point> pointsLeft = new ArrayList<Point>(); ArrayList<Point> pointsRight = new ArrayList<Point>(); public int lx, ly, rx, ry; public Point lil; public Point lil2; public void mouseClicked(MouseEvent e) { if (e.getSource() == picture1) { lil = e.getPoint(); pointsLeft.add(lil); lx = e.getX(); ly = e.getY(); drawCircleLeft(lx, ly); printPointsLeft(); //repaint(); //picture1.removeAll(); } if (e.getSource() == picture2) { lil2 = e.getPoint(); pointsRight.add(lil2); rx = e.getX(); ry = e.getY(); drawCircleRight(rx, ry); printPointsRight(); //repaint(); } //align = new AlignFace(rx, ry, lx, ly); //align.align(); } 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) { if (e.getSource() == picture1) { startPoint = e.getPoint(); lx = startPoint.x; ly = startPoint.y; dragging = false; } if (e.getSource() == picture2) { startPoint = e.getPoint(); rx = startPoint.x; ry = startPoint.y; dragging = false; } } public void mouseReleased(MouseEvent e) { } 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) { if (e.getSource() == picture1) { pointsLeft.add(e.getPoint()); drawCircleLeft(lx + (e.getX() - lx), ly + (e.getY() - ly)); printPointsLeft(); repaint(); } if (e.getSource() == picture2) { pointsRight.add(e.getPoint()); drawCircleRight(rx + (e.getX() - rx), ry + (e.getY() - ry)); printPointsRight(); repaint(); } } } public void mouseExited(MouseEvent e) { } int x, y; int radius = 5; ImageIcon largeImage; ImageIcon largeImageL; ImageIcon largeImageR; private int startDragX, startDragY; private boolean dragging = false; Point startPoint; public void mouseMoved(MouseEvent e) { } public void mouseEntered(MouseEvent e) { }
•
•
Join Date: Sep 2008
Posts: 1,595
Reputation:
Solved Threads: 200
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.
•
•
Join Date: Mar 2009
Posts: 62
Reputation:
Solved Threads: 0
Thanks for the help, I have done this:
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
Java Syntax (Toggle Plain Text)
public void mouseReleased(MouseEvent e) { if (e.getSource() == picture1) { drawCircleLeft(e.getPoint().x, e.getPoint().y); } if (e.getSource() == picture2) { drawCircleRight(e.getPoint().x, e.getPoint().y); } }
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
•
•
Join Date: Sep 2008
Posts: 1,595
Reputation:
Solved Threads: 200
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.
![]() |
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 |







