package finalproject;
import java.awt.Color;
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.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
/**
*
*/
public class PicturePanel extends JPanel implements MouseListener, MouseMotionListener {
public PicturePanel() {
this.setBorder(BorderFactory.createLoweredBevelBorder());
this.addMouseListener(this);
this.addMouseMotionListener(this);
setPreferredSize(new Dimension(250, 150));
setVisible(true);
}
@Override
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
for (Shape s : shapes) {
if (s == selectedShape) {
g2.setColor(Color.GREEN);
g2.fill(s);
g2.setColor(Color.BLACK);
g2.draw(s);
}
g2.setColor(Color.RED);
g2.fill(s);
g2.setColor(Color.BLACK);
g2.draw(s);
}
}
ArrayList<Point> pointsLeft = new ArrayList<Point>();
ArrayList<Point> pointsRight = new ArrayList<Point>();
public int lx, ly, rx, ry;
public Point eR1, eR2 , eL1 , eL2 ;
public Point lil;
public Point lil2;
ArrayList<Shape> shapes = new ArrayList<Shape>();
private Shape selectedShape = null;
public void mouseClicked(MouseEvent e) {
if (e.getSource() == RunProgram.pic1) {
if (selectedShape != null) {
drawSelectedLeft(eL1.x, eL1.y);
} else if (hitTest(e.getPoint()) == null) {
eL1 = e.getPoint();
//pointsLeft.add(eL1);
lx = eL1.x;
ly = eL1.y;
drawCircleLeft(eL1.x, eL1.y);
System.out.println("eL1 = " + eL1);
}
}
}
public void printPointsLeft() {
System.out.println(pointsLeft);
}
public void printPointsRight() {
System.out.println(pointsRight);
}
//method to determine which shape is under the cursor using the contains()
//method of each Shape against your mouse coordinates
private Shape hitTest(Point p) {
Shape hitShape = null;
for (Shape testShape : shapes) {
if (testShape.contains(p)) {
hitShape = testShape;
break;
}
}
return hitShape;
}
private void moveC(int x, int y) {
if (eL1.x != x || eL1.y != y) {
eL1.x = x;
eL1.y = y;
repaint(lx + (x - lx), ly + (y - ly), (2 * radius), (2 * radius));
}
}
public void mouseDragged(MouseEvent e) {
dragging = true;
if (selectedShape != null) {
moveC(e.getX(), e.getY());
}
}
public void drawCircleLeft(int x, int y) {
Shape circle = new Ellipse2D.Double(x - radius, y - radius, 2 * radius, 2 * radius);
shapes.add(circle);
repaint(x - radius, y - radius, 2 * radius, 2 * radius);
}
public void drawSelectedLeft(int x, int y) {
Shape circle = new Ellipse2D.Double(x - radius, y - radius, 2 * radius, 2 * radius);
selectedShape = circle;
shapes.add(selectedShape);
repaint(x - radius, y - radius, 2 * radius, 2 * radius);
}
public void drawCircleRight(int x, int y) {
Shape circle = new Ellipse2D.Double(x - radius, y - radius, 2 * radius, 2 * radius);
shapes.add(circle);
repaint(x - radius, y - radius, 2 * radius, 2 * radius);
}
public void mousePressed(MouseEvent e) {
if (e.getSource() == RunProgram.pic1) {
selectedShape = hitTest(e.getPoint());
startPoint = e.getPoint();
lx = startPoint.x;
ly = startPoint.y;
dragging = false;
}
if (e.getSource() == RunProgram.pic2) {
selectedShape = hitTest(e.getPoint());
startPoint = e.getPoint();
rx = startPoint.x;
ry = startPoint.y;
dragging = false;
}
}
public void mouseReleased(MouseEvent e) {
selectedShape = null;
}
public void mouseExited(MouseEvent e) {
}
int x, y;
int radius = 10;
private boolean dragging = false;
Point startPoint;
public void mouseMoved(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
}