My pencil (drawing program) is too slow!
I have a "pencil" function similar to the one in paint() where I press the mouse and it draws on a JPanel.
However, pretty quickly it gets slow and the drawing can't keep up with the mouseDragged events. How can I make this faster?
this is my class representing the pencil drawingfunction:
import javax.swing.event.MouseInputListener;
import java.io.Serializable;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.LinkedList;
import java.awt.Point;
import java.awt.Color;
import java.util.ListIterator;
import java.awt.Graphics2D;
publicclass MyFreeDrawimplements MouseInputListener, ShapeObject, Serializable
{
private LinkedList allPoints;
private Color color;
private Integer strokeSize;
private String userName;
privatestaticfinallong serialVersionUID = 42;//needed to reduce errors that can arise ina weird way
public MyFreeDraw()
{
color = Color.BLACK;
allPoints =new LinkedList();
strokeSize =new Integer(1);
}
public MyFreeDraw(Color color,int strokeSize, String userName)
{
this.userName = userName;
this.color = color;
this.strokeSize =new Integer(strokeSize);
allPoints =new LinkedList();
}
public LinkedList getAllPoints()
{
return allPoints;
}
publicint getSize()
{
return allPoints.size();
}
public Color getColor()
{
return color;
}
publicint getStrokeSize()
{
return strokeSize.intValue();
}
public String getUserName()
{
return userName;
}
publicvoid setColor(Color color)
{
this.color = color;
}
publicvoid setStrokeSize(int strokeSize)
{
this.strokeSize = strokeSize;
}
publicvoid paint(Graphics2D g)
{
g.setColor(color);
g.setStroke(StrokeLibrary.getDrawingStrokeSize(strokeSize));
LinkedList tempPoints =new LinkedList(allPoints);//avoids ConcurrentException
if(allPoints.size()>=2)
{
ListIterator it2 = tempPoints.listIterator(0);
ListIterator it3 = tempPoints.listIterator(1);//must be one step ahead since two points are needed to draw
while(it2.hasNext() && it3.hasNext())
{
Object ob2 = it2.next();
Object ob3 = it3.next();
if(ob2instanceof Point)
{
Point p = (Point) ob2;
Point t = (Point) ob3;
g.drawLine(p.x, p.y, t.x, t.y);
}
}
}
}
publicboolean equals(Object other)//needed because otherwise the same object will be saved twice in the model!
{
if(this == other)
returntrue;
else
{
if(otherinstanceof MyFreeDraw)
{
MyFreeDraw test = (MyFreeDraw) other;
if(this.userName.equals(test.userName) && this.color.equals(test.getColor()) && this.strokeSize.equals(test.getStrokeSize())&& this.allPoints.equals(test.getAllPoints()))
returntrue;
else
returnfalse;
}
else
returnfalse;
}
}
/*
public int hashCode() //not tested
{
int ret;
ret = userName.hashCode() +color.hashCode() + strokeSize.hashCode() +allPoints.hashCode();
return ret;
}
*/
publicvoid mousePressed(MouseEvent e)
{
Point p =new Point(e.getPoint());//Point p = new Point(e.getPoint());
allPoints.add(p);
}
publicvoid mouseDragged(MouseEvent e)
{
Point p =new Point(e.getPoint());
allPoints.add(p);
}
publicvoid mouseReleased(MouseEvent e)
{
Point p =new Point(e.getPoint());//Point p = new Point(e.getPoint());
allPoints.add(p);
}
publicvoid mouseEntered(MouseEvent e){};
publicvoid mouseMoved(MouseEvent e){};
publicvoid mouseClicked(MouseEvent e){};
publicvoid mouseExited(MouseEvent e){};
}

