Paint Overlay Method in JApplet

I am working on a project so that the contents of the JApplet will automatically scale to the size specified in html. I understand that this is exactly what layout managers were created for, however, since I am not allowed to rewrite the entire structure of the applets, I decided that I would try to override the paint and just set the AffineTransform of the Graphics object to the appropriate scaled version, then grab the Events mouse in the top container and scale them using the appropriate scaling transformation. I'm stuck on the drawing part now. When viewed in a web browser, it displays the scaled version correctly once, and then the image is reduced to its original size. Also, it seems that the drawing method in JApplet is called only once. Here is a cropped version of my code that focuses on the issue. Any help would be greatly 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); } } 
+4
source share
1 answer

After numerous studies, it turned out that the problem is that the JApplet paint method very often calls. Instead, the content area has its own drawing surface, so I just had to replace the content panel to load it. here is how 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 drawing methods were, of course, in addition to those that were in the applet.

+1
source

Source: https://habr.com/ru/post/1335249/


All Articles