Ask Experts Questions for FREE Help !
Ask
    Aftershock's Avatar
    Aftershock Posts: 2, Reputation: 1
    New Member
     
    #1

    Mar 5, 2014, 07:28 AM
    Moving Java Graphics
    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:
    package mainArea;
    
    
    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);
            }
        }
    }

Check out some similar questions!

Graphics color in java applet [ 1 Answers ]

import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Polygon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JApplet; import...

Java netbeans program- Connection between java and mysql,How to conn [ 0 Answers ]

which I wrote from a book but when I run it, it shows exception please give me the code to connect jlistbox to database when I click on btn1. btn coding should search "name" in database and shows all the names in listbox my coding: here-- r1,r2,r3 are radio btns and l1,l2,l3 are labels,...

What is the equivalent of Intel HD Graphics 3000 to Nvidia and AMD graphics? [ 0 Answers ]

Technology, computer, laptop, graphics.

Graphics [ 1 Answers ]

Is there any software that offers photo editing,making passport size photo,colourful background of photo ?

Xp graphics [ 2 Answers ]

I have recently installed xp on my pc it went on OK but to get the screen to display xp I have to uninstall my graphics card it is a radion a t I. Has anyone got any ideas please john


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.