简体   繁体   English

为什么此JComponent不能正确绘制?

[英]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. 说明:请参见JavaDoc for Graphics2D.setTransform:警告:永远不要使用此方法在现有转换之上应用新的坐标转换,因为Graphics2D可能已经具有其他用途所需的转换,例如渲染Swing组件。或应用缩放转换来调整打印机的分辨率。

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. setTransform方法仅用于渲染后恢复原始的Graphics2D变换。

http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#setTransform%28java.awt.geom.AffineTransform%29 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 : 尝试删除getPreferredSize()方法并在setShape方法中使用setPreferredSize():

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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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