Log in

View Full Version : Resolved Moving Java Graphics (More than one)



Deadweight
03-05-2014, 02:45 PM
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:


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);
}
}
}

Deadweight
03-07-2014, 07:19 AM
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.