简体   繁体   中英

Why doesn't this JComponent paints correctly?

The following component paints correctly if move and resize but does not if dragged from out of the screen.

Why?

public class Test_ShapeDraw {
public static class JShape extends JComponent {

    private Shape shape;
    private AffineTransform tx;
    private Rectangle2D bounds;

    public JShape() {
    }

    public void setShape(Shape value) {
        this.shape = value;
        bounds = shape.getBounds2D();
        setPreferredSize(new Dimension((int) bounds.getWidth(), (int)bounds.getHeight()));
        tx = AffineTransform.getTranslateInstance(-bounds.getMinX(), -bounds.getMinY());

    }

    public Shape getShape() {
        return shape;
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        if( shape != null ) {
            Graphics2D g2d = (Graphics2D)g;
            g2d.setTransform(tx);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            ((Graphics2D)g).draw(shape);
        }

    }




}


public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            createAndShowGUI();
        }
    });
}


private static void createAndShowGUI() {

    Shape shape = new Ellipse2D.Double(0,0,300,300);

    JShape jShape = new JShape();
    jShape.setShape(shape);

    JFrame f = new JFrame("Shape Test");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.add(jShape);
    f.pack();
    f.setVisible(true);
}
}

这是从屏幕左边缘拖动时绘制的内容

            AffineTransform originalTransform = g2d.getTransform();
            g2d.transform(tx);

            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            ((Graphics2D)g).draw(shape);

            g2d.setTransform(originalTransform);

Explanation: see the JavaDoc for Graphics2D.setTransform: WARNING: This method should never be used to apply a new coordinate transform on top of an existing transform because the Graphics2D might already have a transform that is needed for other purposes, such as rendering Swing components or applying a scaling transformation to adjust for the resolution of a printer.

To add a coordinate transform, use the transform, rotate, scale, or shear methods. The setTransform method is intended only for restoring the original Graphics2D transform after rendering.

http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#setTransform%28java.awt.geom.AffineTransform%29

Try to remove the getPreferredSize() method and to use setPreferredSize() in the setShape method :

public void setShape(Shape value) {
    this.shape = value;
    bounds = shape.getBounds2D();
    setPreferredSize(new Dimension((int) bounds.getWidth(), (int)bounds.getHeight()));
    tx = AffineTransform.getTranslateInstance(-bounds.getMinX(), -bounds.getMinY());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM