覆盖JApplet中的paint方法

时间:2011-01-12 14:11:36

标签: java applet paint

我正在开发一个项目,使JApplet的内容自动扩展到html中指定的大小。我意识到这是布局管理器所做的事情,但由于我不允许重写整个applet结构,我决定尝试覆盖paint并简单地将Graphics对象的AffineTransform设置为适当缩放的版本,然后在顶部容器中捕获鼠标事件,并使用适当的缩放变换将其缩小。我现在卡在绘图部分。在Web浏览器中查看时,它会正确呈现缩放版本一次,然后将图像缩小回原始大小。此外,似乎JApplet中的paint方法只被调用一次。这是我的代码的裁剪版本,专注于问题。任何帮助,将不胜感激。提前谢谢。

import javax.swing.JApplet;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

public class Test extends JApplet
{
        public static final int ORIGINAL_APPLET_WIDTH = 1024;
        public static final int ORIGINAL_APPLET_HEIGHT = 800;
        private AffineTransform scalingTransform;
        private AffineTransform inverseScalingTransform;
 @Override
 public void init()
 {
            double xFactor = ((double)(this.getWidth()))/((double)(Test.ORIGINAL_APPLET_WIDTH));
            double yFactor = ((double)(this.getHeight()))/((double)(Test.ORIGINAL_APPLET_HEIGHT));
            this.scalingTransform = new AffineTransform();
            this.inverseScalingTransform = new AffineTransform();
            this.scalingTransform.scale(xFactor,yFactor);
            this.inverseScalingTransform.scale(1D/xFactor,1D/yFactor);
 }
    @Override
    public void paint(Graphics g)
    {
        ((Graphics2D)g).setTransform(Test.this.scalingTransform);
        super.paint(g);
    }
}

1 个答案:

答案 0 :(得分:1)

经过大量研究后,我发现问题在于JApplet的绘制方法不经常被调用。相反,内容窗格有自己的绘图表面,所以我只需要替换内容窗格以使其上传。继续我这样做的方式:

@Override
    public void init()
    {
            double xFactor = ((double)(this.getWidth()))/((double)(qt.ORIGINAL_APPLET_WIDTH));
            double yFactor = ((double)(this.getHeight()))/((double)(qt.ORIGINAL_APPLET_HEIGHT));
            this.scalingTransform = new AffineTransform();
            this.inverseScalingTransform = new AffineTransform();
            this.scalingTransform.scale(xFactor,yFactor);
            this.inverseScalingTransform.scale(1D/xFactor,1D/yFactor);
            JPanel drawScale = new JPanel()
            {
                @Override
                public void paint(Graphics g)
                {
                    ((Graphics2D)g).setTransform(Test.this.scalingTransform);
                    super.paint(g);
                }
                @Override
                public void paintAll(Graphics g)
                {
                    ((Graphics2D)g).setTransform(Test.this.scalingTransform);
                    super.paintAll(g);
                }
                @Override
                public void paintComponents(Graphics g)
                {
                    ((Graphics2D)g).setTransform(Test.this.scalingTransform);
                    super.paintComponents(g);
                }
                @Override
                public void paintComponent(Graphics g)
                {
                    ((Graphics2D)g).setTransform(Test.this.scalingTransform);
                    super.paintComponents(g);
                }
            };
            Container oldPane = this.getContentPane();
            drawScale.setLayout(oldPane.getLayout());

            this.setContentPane(drawScale);
}

这些绘画方法当然是applet中的那些。