| | |
Making a graphics component draggable
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
If you maintain a list of shapes You can override the paintComponent method to paint them like so If you want to move one of the shapes, your mouse routines first need to determine which shape is under the cursor. You can do that easily by checking the contains() method of each Shape against your mouse coordinates. Here is a method you could use to check them
java Syntax (Toggle Plain Text)
List<Shape> shapes = new ArrayList<Shape>();
java Syntax (Toggle Plain Text)
public void paintComponent(Graphics g){ Graphics2D g2 = (Graphics2D)g; for (Shape s : shapes){ g2.draw(s); } }
java Syntax (Toggle Plain Text)
private Shape hitTest(Point p){ Shape hitShape = null; for (Shape testShape : shapes){ if (testShape.contains(p)){ hitShape = testShape; break; } } return hitShape; }
•
•
Join Date: Mar 2009
Posts: 62
Reputation:
Solved Threads: 0
This is my mouseclicked method
and this is my methods to draw the cirlces
You said to create the shapes in my listener method rather than drawing them directly. I'm not sure how I can do this with the methods I have.
Thanks
Java Syntax (Toggle Plain Text)
public void mouseClicked(MouseEvent e) { if (e.getSource() == pic1) { if (pointsLeft.isEmpty()) { eL1 = e.getPoint(); //pointsLeft.add(eL1); lx = eL1.x; ly = eL1.y; drawCircleLeft(eL1.x, eL1.y); System.out.println("eL1 = " + eL1); } else if (pointsLeft.size() == 1) { eL2 = e.getPoint(); //pointsLeft.add(eL2); lx = eL2.x; ly = eL2.y; drawCircleLeft(eL2.x, eL2.y); System.out.println("eL2 " + eL2); //picture1.removeAll(); } else if (pointsLeft.size() > 1) { System.out.println("Maximum selections"); } } if (e.getSource() == pic2) { if (pointsRight.isEmpty()) { eR1 = e.getPoint(); pointsRight.add(eR1); lx = eR1.x; ly = eR1.y; drawCircleRight(eR1.x, eR1.y); System.out.println("eR1 = " + eR1); } else if (pointsRight.size() == 1) { eR2 = e.getPoint(); pointsRight.add(eR2); lx = eR2.x; ly = eR2.y; drawCircleRight(eR2.x, eR2.y); System.out.println("eR2 " + eR2); } else if (pointsRight.size() > 1) { System.out.println("Maximum selections"); } } }
and this is my methods to draw the cirlces
Java Syntax (Toggle Plain Text)
public void drawCircleLeft(int x, int y) { Graphics2D g1 = (Graphics2D) pic1.getGraphics(); Shape circle = new Ellipse2D.Double(x - radius, y - radius, 2 * radius, 2 * radius); g1.draw(circle); g1.setColor(Color.RED); g1.fill(circle); shapes.add(circle); } public void drawCircleRight(int x, int y) { Graphics2D g2 = (Graphics2D) pic2.getGraphics(); Shape circle = new Ellipse2D.Double(x - radius, y - radius, 2 * radius, 2 * radius); g2.draw(circle); g2.setColor(Color.RED); g2.fill(circle); shapes.add(circle); }
You said to create the shapes in my listener method rather than drawing them directly. I'm not sure how I can do this with the methods I have.
Thanks
You should restructure your code a bit to override the paintComponent() method of the JPanel objects you wish to draw to, rather then calling getGraphics() to obtain the graphics context. I gave an example above of how paintComponent() can loop through and render the shapes. Your mouse methods just need to create or modify shapes in the List and call repaint to update the graphics.
I'd recommend looking through this short tutorial on custom painting in Swing to see how to override paintComponent().
http://java.sun.com/docs/books/tutor...ing/index.html
I'd recommend looking through this short tutorial on custom painting in Swing to see how to override paintComponent().
http://java.sun.com/docs/books/tutor...ing/index.html
Here's a small example of a custom JPanel class that keeps a List of Shape objects and lets you drag them with the mouse. Perhaps it will clear up some of your uncertainties In your case, with two panels, you would have two instances of this "PaintPanel" and each would be self-contained. Each has its own shapes and handles its own mouse events.
java Syntax (Toggle Plain Text)
import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Shape; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import java.awt.geom.Ellipse2D; import java.awt.geom.Rectangle2D; import java.awt.geom.RectangularShape; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; public class DraggableGraphic extends JFrame { private JPanel paintPanel; public DraggableGraphic() { setDefaultCloseOperation(EXIT_ON_CLOSE); setMinimumSize(new Dimension(300, 300)); paintPanel = new PaintPanel(); getContentPane().add(paintPanel, BorderLayout.CENTER); pack(); } class PaintPanel extends JPanel implements MouseMotionListener,MouseListener { private List<Shape> shapes; private Shape mouseOverShape=null; public PaintPanel(){ super(); addMouseListener(this); addMouseMotionListener(this); shapes = new ArrayList<Shape>(); shapes.add(new Ellipse2D.Float(25, 15, 60, 30)); shapes.add(new Ellipse2D.Float(75, 35, 60, 30)); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; for (Shape s : shapes){ g2.draw(s); } } public void mouseDragged(MouseEvent e) { if (mouseOverShape!=null){ if(mouseOverShape instanceof RectangularShape){ // Move the shape center to the mouse location RectangularShape rShape = (RectangularShape)mouseOverShape; float width = (float)rShape.getWidth(); float height = (float)rShape.getHeight(); rShape.setFrame(new Rectangle2D.Float(e.getX()-width/2, e.getY()-height/2, width, height)); } repaint(); } } public void mouseMoved(MouseEvent e) {} /** returns the first Shape that contains Point p or null if none contain p */ private Shape hitTest(Point p){ Shape hitShape = null; for (Shape testShape : shapes){ if (testShape.contains(p)){ hitShape = testShape; break; } } return hitShape; } public void mousePressed(MouseEvent e) { // figure out what shape, if any, that we are clicking on mouseOverShape = hitTest(e.getPoint()); } public void mouseReleased(MouseEvent e) { mouseOverShape = null; repaint(); } public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new DraggableGraphic().setVisible(true); } }); } }
![]() |
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
Views: 1467 | Replies: 28
| Thread Tools | Search this Thread |
Tag cloud for graphics







