Results 1 to 2 of 2

Thread: Moving Java Graphics (More than one)

  1. #1
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    372
    Thanks
    5
    Thanked 35 Times in 33 Posts

    Default Moving Java Graphics (More than one)

    Currently I am working on moving Java Graphics2D. I know how to make one move but I don't know how to make the other one move without creating a ton of functions. Which I don't won't to do.
    EG. IF you click the rect1 it will know that you have clicked rect1 else if you clicked rect2 it will know only to move rect2.
    Any help would be great. Thanks.

    Here is my current program:
    Code:
    import java.awt.*;
    import java.awt.event.*; 
     
    import javax.swing.*;
    import javax.swing.event.MouseInputAdapter;
     
    @SuppressWarnings("serial")
    public class Main extends JPanel {
    	private Graphics2D g2;
     
    	private int rectX2 = 100;
    	private int rectY2 = 10;
     
    	private int rectX = 270;
    	private int rectY = 10;
     
     
        Rectangle rect2 = new Rectangle(rectX2,rectY2,149,25);
        Rectangle rect1 = new Rectangle(rectX,rectY,149,25);
     
        private final Color white = new Color(255,255,255);
     
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
     
            g2.setColor(white);
            g2.draw(rect2);
            g2.fill(rect2);
            g2.draw(rect1);
            g2.fill(rect1);
            groupOne(g2, (int)rect2.getBounds().getX(),(int)rect2.getBounds().getY());
            groupTwo(g2, (int)rect1.getBounds().getX(),(int)rect1.getBounds().getY());
        }
     
        public void setRect(int x, int y) {
            //rect.setLocation(x-1, y-1);
        	rect1.setLocation(x, y);
            repaint();
        }
     
        public void groupOne(Graphics g, int x, int y){
        	g2 = (Graphics2D)g;
        	g2.setPaint(Color.blue);
        	g2.drawRect(x-1, y-1, 150, 175);
        	g2.drawString("Test",x+2,y+15);
        }
     
        public void groupTwo(Graphics g, int x, int y){
        	g2 = (Graphics2D)g;
        	g2.setPaint(Color.blue);
        	g2.drawRect(x-1, y-1, 150, 175);
        	g2.drawString("Test",x+2,y+15);
        }
     
        public static void main(String[] args) {
            Main test = new Main();
            new GraphicDragController(test);
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(test);
            f.setSize(700,400);
            f.setLocation(100,100);
            f.setVisible(true);
     
        }
    }
     
    class GraphicDragController extends MouseInputAdapter {
        Main component;
        Point offset = new Point();
        boolean dragging = false;
     
     
     
     
        public GraphicDragController(Main gdad) {
            component=gdad;
            component.addMouseListener(this);
            component.addMouseMotionListener(this);
        }
     
        public void mousePressed(MouseEvent e) {
            Point p = e.getPoint();
            Rectangle r = component.rect2;
            Rectangle rx = component.rect1;
     
            if(rx.contains(p)){
            	offset.x = p.x - rx.x;
                offset.y = p.y - rx.y;
                dragging = true;
            }
     
            if(r.contains(p)) {
                offset.x = p.x - r.x;
                offset.y = p.y - r.y;
                dragging = true;
            }
        }
     
        public void mouseReleased(MouseEvent e) {
            dragging = false;
        }
     
        public void mouseDragged(MouseEvent e) {
            if(dragging) {
                int x = e.getX() - offset.x;
                int y = e.getY() - offset.y;
     
                component.setRect(x, y);
            }
        }
    }
    Last edited by Deadweight; 03-07-2014 at 06:16 AM.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

  2. #2
    Join Date
    Mar 2010
    Location
    Florida
    Posts
    372
    Thanks
    5
    Thanked 35 Times in 33 Posts

    Default

    The best way I figured to do this is actually make an array with the Rectangles. Check the array for to see if the point is contained in one of the rectangles. If so then you you move that rectangle.

    IF anyone was wondering!

    (Figured it out myself)

    Just was informing my way to solve this if anyone else had this problem.
    -DW [Deadweight]
    Resolving your thread: First Post: => EDIT => Lower right: => GO ADVANCED => Top Advance Editor drop down: => PREFIX:Resolved

Similar Threads

  1. Graphics
    By pompano in forum General Paid Work Requests
    Replies: 2
    Last Post: 09-09-2011, 03:50 PM
  2. inline java popup within another java code...
    By bm13084 in forum JavaScript
    Replies: 0
    Last Post: 02-17-2009, 04:05 AM
  3. graphics help
    By Bytch_Amy in forum HTML
    Replies: 2
    Last Post: 09-03-2008, 10:54 PM
  4. graphics or ram?
    By nekum in forum Computer hardware and software
    Replies: 25
    Last Post: 04-25-2008, 09:13 AM
  5. Java and the ACM Graphics Package Help
    By Buschmaster in forum Java
    Replies: 2
    Last Post: 10-13-2007, 05:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •