F
fxn
I am quite new to Swing programming. I wrote a Swing component which is
a matrix of small rectangles, following the guidelines in the
documentation online.
Each cell of the matrix can have a background color and a list of
Drawables, which are objects who live in a cell and typically display
themselves just via setColor + fillRect or something like that. Nothing
fancy.
The whole matrix is changed very often (it is not like a chessboard for
instance) since it is the display of some type of discrete simulations,
and the code I have so far is:
public class Grid extends JComponent {
private Cell[][] cells;
private Set cellsToPaint;
// ...
public Grid() {
setOpaque(true);
// ...
}
protected synchronized void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
cellWidth = getWidth()/xcells;
cellHeight = getHeight()/ycells;
Iterator iter = cellsToPaint.iterator(); // subset of cells to paint
while (iter.hasNext()) {
Point p = (Point) iter.next();
Cell c = cells[p.x][p.y];
if (c.drawables.isEmpty() && c.bgcolor == null)
iter.remove(); // cache maintenance
else
paintCell(p.x, p.y, c, g);
}
}
private void paintCell(int x, int y, Cell c, Graphics g) {
int[] cr = getCellRectangle(x, y);
if (c.bgcolor != null && !c.bgcolor.equals(getBackground())) {
g.setColor(c.bgcolor);
g.fillRect(cr[0], cr[1], cr[2], cr[3]);
}
Iterator iter = c.drawables.iterator();
while (iter.hasNext()) {
// we pass a copy of the graphics object
// to ensure the original one remains untouched
// as the Swing tutorial recommends
Graphics2D g2 = (Graphics2D) g.create();
((Drawable) iter.next()).draw(cr, g2);
g2.dispose();
}
}
// ...
}
This implementation works, but based on some comparisons looks like it
is clear that it is slow. That looked like the natural way to write
that, but it Is there some alternative Swing technique to speed this up
fundamentally?
-- fxn
a matrix of small rectangles, following the guidelines in the
documentation online.
Each cell of the matrix can have a background color and a list of
Drawables, which are objects who live in a cell and typically display
themselves just via setColor + fillRect or something like that. Nothing
fancy.
The whole matrix is changed very often (it is not like a chessboard for
instance) since it is the display of some type of discrete simulations,
and the code I have so far is:
public class Grid extends JComponent {
private Cell[][] cells;
private Set cellsToPaint;
// ...
public Grid() {
setOpaque(true);
// ...
}
protected synchronized void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
cellWidth = getWidth()/xcells;
cellHeight = getHeight()/ycells;
Iterator iter = cellsToPaint.iterator(); // subset of cells to paint
while (iter.hasNext()) {
Point p = (Point) iter.next();
Cell c = cells[p.x][p.y];
if (c.drawables.isEmpty() && c.bgcolor == null)
iter.remove(); // cache maintenance
else
paintCell(p.x, p.y, c, g);
}
}
private void paintCell(int x, int y, Cell c, Graphics g) {
int[] cr = getCellRectangle(x, y);
if (c.bgcolor != null && !c.bgcolor.equals(getBackground())) {
g.setColor(c.bgcolor);
g.fillRect(cr[0], cr[1], cr[2], cr[3]);
}
Iterator iter = c.drawables.iterator();
while (iter.hasNext()) {
// we pass a copy of the graphics object
// to ensure the original one remains untouched
// as the Swing tutorial recommends
Graphics2D g2 = (Graphics2D) g.create();
((Drawable) iter.next()).draw(cr, g2);
g2.dispose();
}
}
// ...
}
This implementation works, but based on some comparisons looks like it
is clear that it is slow. That looked like the natural way to write
that, but it Is there some alternative Swing technique to speed this up
fundamentally?
-- fxn