Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Faster line drawing?



I've found that Mac-Java's Swing double buffering is terrible. I had to
explicitly double buffer a component to get quick repaints. Even then, if
you resize the component, the screen goes blue and white for a second or so
before it correctly repaints again. I entered a bug to Apple on it.

See the difference for yourself. Use the following JDesktopPane and add an
internal window. Move around the internal window to force repaints.

In the first version, there is no explicit double buffering and the repaint
action is unacceptably slow. It takes up to 2 seconds on my G4/400 with the
screen maximized to move the JInternalFrame from upper left to lower right.

On Windows and Linux, I have no problem with this component and its paint
speed.

In the second version, moving the JInternalFrame is reasonably smooth.
However, maximizing creates a disastrous screen paint for about one second
before it recreates and repaints the background. Repeated maximizing and
restoring will show the flakiness of MRJs graphics through a painful variety
of poor repaints.

PatternDesktopPane (v1 - not explicitly double buffering):
==========================================================
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class PatternDesktopPane extends JDesktopPane {
private Paint _stripe = null;

public PatternDesktopPane() {
super();
BufferedImage image =
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().g
etDefaultConfiguration().createCompatibleImage(6,1);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.black);
g2.fillRect(0, 0, 3, 1);
g2.setColor(Color.gray);
g2.fillRect(3, 0, 6, 1);
_stripe = new TexturePaint(image, new Rectangle(0, 0, 6, 1));
}

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(_stripe);
g2.fill(new Rectangle(getSize()));
}
}


PatternDesktopPane (v2 - explicitly double buffering):
==========================================================
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;

public class PatternDesktopPane extends JDesktopPane {
private Paint _stripe = null;
private BufferedImage _buf = null;

public PatternDesktopPane() {
super();
BufferedImage image =
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().g
etDefaultConfiguration().createCompatibleImage(6,1);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(Color.black);
g2.fillRect(0, 0, 3, 1);
g2.setColor(Color.gray);
g2.fillRect(3, 0, 6, 1);
_stripe = new TexturePaint(image, new Rectangle(0, 0, 6, 1));
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

if (_buf == null ||
getHeight() != _buf.getHeight() ||
getWidth() != _buf.getWidth()) {
System.out.println("Creating image");
_buf = (BufferedImage)createImage(getWidth(), getHeight());
Graphics2D bufG2 = (Graphics2D)_buf.getGraphics();
bufG2.setPaint(_stripe);
bufG2.fill(new Rectangle(getSize()));
}

Graphics2D g2 = (Graphics2D)g;
System.out.println("Drawing image");
g2.drawImage(_buf, 0, 0, this);
}
}


TestApp.java (a sample controller app):
=============================================================
import java.awt.Dimension;
import javax.swing.*;

public class TestApp {
public static void main(String[] args) {
JFrame app = new JFrame("Test Slow Graphics2D");
app.setDefaultCloseOperation(app.EXIT_ON_CLOSE);

JInternalFrame internal = new JInternalFrame("Some internal
frame");
JLabel text = new JLabel("Moving me forces redrawing");
JPanel opaque = new JPanel();
opaque.add(text);
internal.getContentPane().add(opaque);
internal.pack();
internal.setVisible(true);

JDesktopPane desktop = null;
if (args != null && args.length >= 1 &&
"pattern".equals(args[0])) {
desktop = new PatternDesktopPane();
} else {
desktop = new JDesktopPane();
}
desktop.add(internal);
desktop.setMinimumSize(new Dimension(500, 500));
desktop.setPreferredSize(new Dimension(500, 500));

app.setContentPane(desktop);
app.pack();
app.setVisible(true);
}
}

-----Original Message-----
From: Stewart Greenhill [mailto:email@hidden]
Sent: Friday, November 22, 2002 1:50 AM
To: email@hidden
Cc: email@hidden
Subject: Re: Faster line drawing?

[SNIP]

> If you're desperate for speed, you might draw into an offscreen
> BufferedImage and see how that goes. No guarantees, but it might
make a
> difference. Or not.

Actually, this is a Swing component so its already double-buffered.
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.