Hi everybody
i have written a java code (GUI) , which a red marker (red point) should follow a line between 2 positions, the program works good but the red point is not exact on the line (it just moves one t edge of the line). now i need some help to change x and y coordinate to be one the line not on the edge..
thanks
import java.util.LinkedList;
import java.util.ListIterator;
import javax.swing.*;
import java.awt.*;
public class JavaGrafikPanel1 extends JPanel
{
public boolean animation;
public animcircle mc;
JavaGrafikPanel1()
{
Color mycolor = new Color(10,50,100);
setBackground(mycolor);
this.animation = false;
mc = new animcircle();
mc.firstPoint = new Position(0, 0);
mc.lastPoint = new Position(0, 0);
}
private LinkedList points;
public LinkedList getPoints()
{
return points;
}
public void setPoints(LinkedList points)
{
this.points = points;
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
ListIterator li = points.listIterator();
Position p = new Position(0, 0);
Position q = new Position(0, 0);
while(li.hasNext())
{
p = (Position)li.next();
g.setColor(Color.pink);
g.drawOval((int)p.getX(), (int)p.getY(),3,3);
if(li.hasNext())
{
q = (Position)li.next();
g.setColor(Color.white);
g.drawLine((int)p.getX(), (int)p.getY(), (int)q.getX(), (int)q.getY());
li.previous();
}
else
break;
}
if(this.animation)
{
Position cr = new Position(0,0);
int index = 0;
if((cr = mc.getNextPosition()) == null)
{
index = points.indexOf(mc.lastPoint);
mc.firstPoint = mc.lastPoint;
if(points.size() > index+1)
mc.lastPoint = (Position)points.get(index+1);
else // stop animation
{
animation = false;
return;
}
mc.step = 0;
if((cr = mc.getNextPosition()) == null)
{
animation = false;
return;
}
}
g.drawOval(((int)cr.getX())/2,((int)cr.getY())/2,8,8);
g.setColor(Color.red);
g.fillOval(((int)cr.getX())/2,((int)cr.getY())/2,8,8);
}
}
}