开发者

overriding paint method in JApplet

开发者 https://www.devze.com 2023-02-03 16:27 出处:网络
I am working on a project to make a JApplet\'s contents automatically scale to the size specified in the html. I realize this is the kind of thing that layout managers were made for, however as I am n

I am working on a project to make a JApplet's contents automatically scale to the size specified in the html. I realize this is the kind of thing that layout managers were made for, however as I am not permitted to rewrite the entire applets structure, i decided i would try to override paint and simply set the AffineTransform of the Graphics object to an appropriately scaled version, then capture mouse Events in the top container and scale them back with an appropriate scaling transform. Im getting stuck on the drawing part at the moment. When viewed in a web browser, it renders the scaled version correctly once, then the image is shrunk back to its original size. In addition, 开发者_如何学运维it seems that the paint method in JApplet is only ever called once. Here is a cropped version of my code which focuses on the problem. Any help would be appreciated. Thanks in advance.

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


I figured out, after much research, that the problem was that JApplet's paint method doesn't get called very often. The instead the content pane has its own drawing surface, so i simply had to replace the content pane to get it to upload. heres the way i did it:

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

these paint methods were of course in addition to the ones in the applet.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号