I am writing a program where the user can draw points on a JPanel by clicking and dragging the mouse. In addition, the drawing area is divided into a number of sectors, and the points are rotated such that every sector is the same. For example, a twelve sector arrangement with a single point inside will rotate that point twelve times through 360/12 degrees. The rotation works fine, but there is some very strange behaviour when trying to draw points. If one attempts to draw a circle around the origin, the points will appear very sporadically for a short time, before being added smoothly. This image shows what I mean (the result of drawing a quarter circle in one of the sectors):
You can see that, when approaching one side of a sector division, the points are added smoothly. However, initially the points are separated and not smoothly being drawn. The code is shown below (the superfluous GUI elements and imports have been removed for ease of reading):
public class Doiles extends JPanel implements MouseListener,ActionListener,MouseMotionListener
{
//global variable declarations
JFrame window = new JFrame("Draw");
final int linelength = 340;//length of sector defining lines
int nlines = 12;//store the number of sector defining lines
String numsectors=null;
int currentovalsize = 10;
Color currentcolour = Color.WHITE;
Deque<DoilyPoint> points = new LinkedList<DoilyPoint>();
public Doiles()
{
window.setSize(2000,1000);
//drawing panel + paint method
JPanel drawingPanel = new JPanel()
{
public void paintComponent(Graphics g)
{
super.paintComponent(g);
//calculate angle between sectors
double theta = (2*Math.PI)/nlines;
g.setColor(Color.WHITE);
//calculate line coordinates and draw the sector lines
for(int i=0; i <nlines;i++)
{
g.drawLine(400, 350, 400+(int)Math.round(linelength*Math.cos(theta*i)), 350+(int)Math.round(linelength*Math.sin(theta*i)));
}
for(DoilyPoint j : points)
{
g.fillOval(j.getX(), j.getY(), j.getSize(), j.getSize());
for(int h = 1;h<nlines;h++)
{
double rtheta;
if(j.getX()==400)
rtheta = Math.PI/2;
else
rtheta = Math.atan((j.getY()-350)/(j.getX()-400));
System.out.println(rtheta);
double r = Math.sqrt(Math.pow(j.getX()-400,2)+Math.pow(j.getY()-350,2));
double angle = (h*theta)+rtheta;
double x = r*Math.cos(angle);
double y = r*Math.sin(angle);
g.fillOval((int)Math.round(x)+400,(int)Math.round(y)+350, j.getSize(), j.getSize());
}
}
}
};
}
public static void main(String[] args)
{
new Doiles();
}
public void addPoint(int x, int y)
{
points.addFirst(new DoilyPoint(currentovalsize,x,y,currentcolour));
window.repaint();
}
@Override
public void mouseDragged(MouseEvent e)
{
addPoint(e.getX(),e.getY());
}
}
class DoilyPoint
{
private int size;
private int x;
private int y;
private Color colour;
void setSize(int a){this.size = a;}
int getSize(){return size;}
void setX(int a){this.x =a;}
int getX(){return x;}
void setY(int a){this.y = a;}
int getY(){return y;}
void setColor(Color r){this.colour = r;}
Color getColor(){return colour;}
public DoilyPoint(int size,int x, int y,Color colour)
{
this.size = size;
this.x = x;
this.y = y;
this.colour = colour;
}
}
I assume it's something to do with the way Java handles dragging the mouse, but I'd like to know how to smooth out the drawing. Can anyone tell me what's wrong?