So as the title states, I would want to move the ellipse on mouse drag. I have declared ellipses first and drew them through ArrayLists
(since I have 8 ellipses which carry different color information. Four are white and the other are red). I tried doing what I did for the rectangles:
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
}
public void mouseDragged(MouseEvent e)
{
if(e.getSource()==MainPane)
{
int dx = e.getX() - x;
int dy = e.getY() - y;
Point p = getLocation();
if(el1.getBounds().contains(x,y))
{
el1.x += dx;
el1.y += dy;
}
x += dx;
y += dy;
}
}
But this doesn't seem to work. It gives me an error
cannot find symbol
symbol: variable x
location: <ellipse name> of type Ellipse2D
I am kind of confused since I have read the documentation and such variable exists for an Ellipse2D.Double.
Here is an MCVE:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
public class Sample extends JFrame implements MouseListener, MouseMotionListener {
JPanel MainPane;
Container contentPane;
ArrayList<Ellipse2D> redEl = new ArrayList<Ellipse2D>();
ArrayList<Ellipse2D> whiteEl = new ArrayList<Ellipse2D>();
Ellipse2D el1 = new Ellipse2D.Double(120,110,50, 50);
Ellipse2D el2 = new Ellipse2D.Double(250,110,50, 50);
Ellipse2D el3 = new Ellipse2D.Double(390,110,50, 50);
Ellipse2D el4 = new Ellipse2D.Double(540,110,50, 50);
Ellipse2D el5 = new Ellipse2D.Double(120,390,50, 50);
Ellipse2D el6 = new Ellipse2D.Double(250,390,50, 50);
Ellipse2D el7 = new Ellipse2D.Double(390,390,50, 50);
Ellipse2D el8 = new Ellipse2D.Double(540,390,50, 50);
int x;
int y;
public Sample(){
redEl.add(el1);
redEl.add(el2);
redEl.add(el3);
redEl.add(el4);
whiteEl.add(el5);
whiteEl.add(el6);
whiteEl.add(el7);
whiteEl.add(el8);
MainPane = new JPanel()
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
for (Shape red : redEl) {
g2.setColor(Color.RED);
g2.fill(red);
g2.draw(red);
}
for (Shape white : whiteEl) {
g2.setColor(Color.WHITE);
g2.fill(white);
g2.draw(white);
}
}
};
MainPane.setBackground(Color.BLACK);
contentPane = this.getContentPane();
contentPane.add(MainPane);
MainPane.setLayout(null);
setVisible(true);
setSize(701, 701);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MainPane.addMouseListener(this);
MainPane.addMouseMotionListener(this);
}
public void mousePressed(MouseEvent e)
{
x = e.getX();
y = e.getY();
}
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseDragged(MouseEvent e)
{
if(e.getSource() == MainPane)
{
int dx = e.getX() - x;
int dy = e.getY() - y;
if(el1.getBounds().contains(x,y))
{
}
x += dx;
y += dy;
}
}
public void mouseMoved(MouseEvent e){}
public static void main(String args[])
{
new Sample();
}
}
So are there alternative algorithms or am I just missing something in the syntax? I would like to know solutions. Thank you.
UPDATE:
Solved it through MadProgrammer's suggestion
Here is my mouseDragged
method.
public void mouseDragged(MouseEvent e)
{
if(e.getSource()==MainPane)
{
int dx = e.getX() - x;
int dy = e.getY() - y;
Point p = getLocation();
if(el1.getBounds().contains(x,y))
{
double xc = el1.getX();
double yc = el1.getY();
el1.setFrame(xc+=dx, yc+=dy, 50, 50);
MainPane.repaint();
}
x += dx;
y += dy;
}
}