Hi,

I am new to Java2D and am wondering if there are any
issues with using Graphics2D with Swing  components.

In the example code attached below,
I have a JPanel whose paintComponent() method is overridden
to use Graphics2D  to just draw a rectangle and a piece of text.
When the window gets obscured and then exposed, I see some artifacts.

However, if I use AWT Canvas instead of JPanel, the repainting works
as expected.

Also, I noticed that the Java2D demos override paint() method
on DemoSurface
which is a subclass of JPanel. Shouldn't they be overriding
paintComponent()
instead?

I am using Blackdown's Linux port for Java version: 1.2 on Intel
platform.

Thanks,
Satish.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.*;

public class Test2D extends JFrame {
    public Test2D() {
        JPanel panel = new DrawPanel();
        getContentPane().add(panel);

//      Canvas panel = new DrawPanel();
//      getContentPane().add(panel);

        pack();
        setVisible(true);
    }
    public static void main( String[] args ) {
        Test2D test = new Test2D();
    }
}

// class DrawPanel extends Canvas {
//     public void paint(Graphics g) {
class DrawPanel extends JPanel {
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        g2d.setBackground(Color.blue);
        g2d.setColor(Color.white);
        g2d.clearRect(0, 0, getWidth(), getHeight());
        g2d.setTransform(new AffineTransform());
        Rectangle2D rect = new Rectangle2D.Float(0, 0, getWidth()/2, getHeight()/2);
        g2d.fill(rect);
        g2d.drawString("Test2D", getWidth()/2, getHeight()/2);
    }
    public Dimension getPreferredSize() {
        return new Dimension(100, 100);
    }
}

Reply via email to