簡體   English   中英

覆蓋JApplet中的paint方法

[英]overriding paint method in JApplet

我正在開發一個項目,使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);
    }
}

經過大量研究,我發現問題在於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中的那些之外,這些繪制方法也是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM