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){};

}

[8380 byte] By [CbbLea] at [2007-11-27 4:52:18]
# 1

Instead of storing every point the mouse has been, you can draw directly onto a BufferedImage from your mouseDragged() method. All you would need to do in your paintComponent() method is draw that BufferedImage.

public void mouseDragged(MouseEvent e)

{

Graphics g = image.getGraphics();

g.drawLine(lastX, lastY, currentX, currentY);

}

CaptainMorgan08a at 2007-7-12 10:06:15 > top of Java-index,Security,Cryptography...
# 2

But I need to redraw the object because I store it in a model. Aswell as I need to store all points so I can send it over network.

A repaint of the JPanel im drawing on can occur before my painting has finished. That's why I think I need to draw all the points all the time.

atleast I think..

CbbLea at 2007-7-12 10:06:15 > top of Java-index,Security,Cryptography...
# 3
but as it is now I redraw every point everytime in the object everytime I move the mouse...this is ofcourse verrry bad.I dont know how to do.
CbbLea at 2007-7-12 10:06:15 > top of Java-index,Security,Cryptography...