I have written a program which allows the user to click on the screen which draws a circle and when the user clicks on the circle again they can drag it around. My code allows the circle to be dragged it still displays the original cirlce and draws the path the cricle takes rather than only displaying the circle being dragged. Here are my classes. I hope someone can spot the problem. Thank you
This displays the frame:
package finalproject;
public class RunApplication {
public static void main(String[] args) {
RunProgram prog = new RunProgram();
prog.setSize(800, 500);
prog.show();
}
}
This just fills the frame:
/*
package finalproject;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.swing.*;
import javax.swing.JLabel;
public class RunProgram extends JFrame {
static JMenuBar bar;
File fileL;
File fileR;
private JPanel toolPanel;
JButton openButton;
static PicturePanel pic1, pic2;
/** Creates a new instance of RunProgram */
public RunProgram() {
setLayout(new BorderLayout());
//create panel to show original pictures
pic1 = new PicturePanel();
pic2 = new PicturePanel();
toolPanel = new JPanel();
toolPanel.setPreferredSize(new Dimension(100, 50));
//create panel to show the phases
JToolBar toolBar = new JToolBar("Still draggable");
toolBar = new JToolBar(null, JToolBar.VERTICAL);
toolBar.setFloatable(false);
add(toolBar, BorderLayout.NORTH);
add(pic1, BorderLayout.WEST);
add(pic2, BorderLayout.EAST);
add(toolBar, BorderLayout.CENTER);
}
}
Here is the main code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
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) {
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>();
public void mouseClicked(MouseEvent e) {
if (e.getSource() == RunProgram.pic1) {
eL1 = e.getPoint();
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){
repaint(lx + (x - lx), ly + (y - ly),(2 * radius)+1, (2 * radius));
eL1.x = x;
eL1.y = y;
repaint(lx + (x - lx), ly + (y - ly), (2 * radius) + 1, (2 * radius)+1);
}
}
public void mouseDragged(MouseEvent e) {
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 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) {
startPoint = e.getPoint();
lx = startPoint.x;
ly = startPoint.y;
}
if (e.getSource() == RunProgram.pic2) {
startPoint = e.getPoint();
rx = startPoint.x;
ry = startPoint.y;
}
}
public void mouseReleased(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
int x, y;
int radius = 10;
Point startPoint;
public void mouseMoved(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
}