slow painting performance
Hi!
Im drawing lots of different shapes like rectangles, polygons, circles etc on a JPanel. All the shapes are stored inside a model and are redrawn by invoking each objects paint(). This works fine.
Problem
When I draw a shape, for example an rectangle, im painting it meanwhile im dragging my mouse. If i have alot of shapes I get slow performance because it has to repaint all shapes in the rectangles area "repaint(rectanglesarea)" for every mouseDragged call.
How can this be made faster? Please give concrete examples:
Here is my rectangle class:
/*
* MyRectangle.java
*
* Created on den 21 mars 2007, 16:41
*
*/
package graphic;
import java.awt.Point;
import java.awt.Color;
import javax.swing.event.MouseInputListener;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.io.Serializable;
import java.awt.Graphics2D;
/**
*
* @author Sebastian Gr鋘nby
*/
publicclass MyRectangleextends Rectangleimplements MouseInputListener, ShapeObject, Serializable
{
private Point start;
private Point stop;
private Color color;
private Boolean filled;
private Integer strokeSize;
private String userName;
privatestaticfinallong serialVersionUID = 42;
public MyRectangle()
{
start =new Point();
stop =new Point();
color = Color.BLACK;
filled =new Boolean(false);
strokeSize =new Integer(1);
}
public MyRectangle(Color color,boolean filled,int strokeSize, String userName)
{
this.userName = userName;
start =new Point();
stop =new Point();
this.color = color;
this.filled =new Boolean(filled);
this.strokeSize =new Integer(strokeSize);
}
public Point getStart()
{
return start;
}
public Point getStop()
{
return stop;
}
public Color getColor()
{
return color;
}
publicboolean getFilled()
{
return filled.booleanValue();
}
publicint getStrokeSize()
{
return strokeSize.intValue();
}
public String getUserName()
{
return userName;
}
publicvoid setColor(Color color)
{
this.color = color;
}
publicvoid setFilled(boolean filled)
{
this.filled = filled;
}
publicvoid setStrokeSize(int strokeSize)
{
this.strokeSize = strokeSize;
}
publicvoid paint(Graphics2D g)
{
g.setColor(color);
g.setStroke(StrokeLibrary.getDrawingStrokeSize(strokeSize));
if(filled)
g.fillRect((int)x, (int)y, (int)width, (int)height);
else
g.drawRect((int)x, (int)y, (int)width, (int)height);
}
publicvoid mousePressed(MouseEvent e)
{
start = e.getPoint();
}
publicvoid mouseDragged(MouseEvent e)
{
stop = e.getPoint();
//down and to the right
if(stop.x > start.x && stop.y > start.y)
{
setFrame(start.x, start.y, stop.x - start.x, stop.y - start.y);
}
//up and to the left
elseif(stop.x < start.x && stop.y < start.y)
{
setFrame(stop.x, stop.y, start.x - stop.x, start.y - stop.y);
}
//down and to the left
elseif(stop.x > start.x && stop.y < start.y)
{
setFrame(start.x, stop.y, stop.x - start.x, start.y - stop.y);
}
//up and to the right
else
{
setFrame(stop.x, start.y, start.x - stop.x, stop.y - start.y);
}
}
publicvoid mouseReleased(MouseEvent e)
{
stop = e.getPoint();
}
publicvoid mouseEntered(MouseEvent e){};
publicvoid mouseMoved(MouseEvent e){};
publicvoid mouseClicked(MouseEvent e){};
publicvoid mouseExited(MouseEvent e){};
}
Please Help!!
Message was edited by:
CbbLe

