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

[8455 byte] By [CbbLea] at [2007-11-27 6:27:19]
# 1

Use a BufferedImage as your canvas and then draw the image in paintComponent of the JPanel.

Then you can use Xor mode to draw the rectangle on the BufferedImage, which means you only have to draw the rectangle and not the other shapes.

Xor means that when you draw the shape the second time in the same location it removes the shape.

You can read in Graphics class API setXorMode:

http://java.sun.com/j2se/1.5.0/docs/api/java/awt/Graphics.html

Rodney_McKaya at 2007-7-12 17:49:09 > top of Java-index,Security,Cryptography...
# 2
Can you please give a simple example? I do not understand.
CbbLea at 2007-7-12 17:49:09 > top of Java-index,Security,Cryptography...
# 3

Here's a simple example.

Just press the mouse and drag it over the other drawings.

import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Point;

import java.awt.event.MouseEvent;

import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.event.MouseInputAdapter;

public class MouseDragXorExample {

public static void main(String[] args) {

try {

JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);

Graphics2D g = image.createGraphics();

g.setColor(Color.WHITE);

g.fillRect(0, 0, image.getWidth(), image.getHeight());

g.setColor(Color.BLUE);

g.drawRect(200, 10, 50, 50);

g.setColor(Color.MAGENTA);

g.fillRect(100, 100, 20, 50);

g.setColor(Color.RED);

g.fillOval(200, 200, 80, 100);

JLabel label = new JLabel(new ImageIcon(image));

RectangleDragMouseListener listener = new RectangleDragMouseListener(g);

label.addMouseListener(listener);

label.addMouseMotionListener(listener);

frame.add(label);

frame.pack();

frame.setVisible(true);

}

catch (Exception e) {e.printStackTrace();}

}

private static class RectangleDragMouseListener extends MouseInputAdapter {

private Point point1 = null;

private Point point2 = null;

private Graphics2D g;

public RectangleDragMouseListener(Graphics2D g) {

this.g = g;

}

public void mousePressed(MouseEvent e) {

point1 = e.getPoint();

}

public void mouseReleased(MouseEvent e) {

if (point2 != null)

drawRect(e);

point2 = null;

}

public void mouseDragged(MouseEvent e) {

g.setColor(Color.BLACK);

g.setXORMode(Color.WHITE);

if (point2 != null)

drawRect(e);

point2 = e.getPoint();

drawRect(e);

}

private void drawRect(MouseEvent e) {

int x = Math.min(point1.x, point2.x);

int y = Math.min(point1.y, point2.y);

int width = Math.abs(point1.x - point2.x);

int height = Math.abs(point1.y - point2.y);

g.drawRect(x, y, width, height);

e.getComponent().repaint(x, y, width+1, height+1);

}

}

}

Rodney_McKaya at 2007-7-12 17:49:09 > top of Java-index,Security,Cryptography...