0

when i zoom JPanel it zooms and its components zooms but it converts to painted component not the component its self which i can't control it or type on it if it JTextField and the original component stay at its size and location as shown in picture

panel after zooming

and this is my code

i make the panel which i want to zoom here in this class

public class designPanel extends JPanel{

public double zoomFactor = 1;
public boolean zoomer = true;
public AffineTransform at;


@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    if (zoomer == true) {
        at = g2.getTransform();
        at.scale(zoomFactor, zoomFactor);
        zoomer = false;
    }
    g2.transform(at);
}

public void setZoomFactor(double factor){

    if(factor<this.zoomFactor){
        this.zoomFactor=this.zoomFactor/1.1;
    }
    else{
        this.zoomFactor=factor;
    }
    this.zoomer=true;
}


public double getZoomFactor() {
    return zoomFactor;
}

}

and then zoom in and out this panel by mouse wheel in this code

  design_gridPanel.addMouseWheelListener(new MouseWheelListener() {
        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {


            if (e.getWheelRotation() < 0) {

                design_gridPanel.setZoomFactor(1.1 * design_gridPanel.getZoomFactor());

                design_gridPanel.revalidate();
                design_gridPanel.repaint();

            }

            if (e.getWheelRotation() > 0) {

                design_gridPanel.setZoomFactor(design_gridPanel.getZoomFactor() / 1.1);

                design_gridPanel.revalidate();
                design_gridPanel.repaint();
             }


        }});
  • Possible duplicate [Zooming JLayeredPane via JLayer and the LayerUI](https://stackoverflow.com/questions/21439341/zooming-jlayeredpane-via-jlayer-and-the-layerui/21445118#21445118); [How to add MouseListener to item on Java Swing Canvas](https://stackoverflow.com/questions/21174997/how-to-add-mouselistener-to-item-on-java-swing-canvas/21175125#21175125) – MadProgrammer Jun 05 '18 at 22:25
  • 1
    I'm just going to stop you there, zooming a live container full of components is actually really complicated, not only do you need to be able apply the zoom, but you need to translate all the coordinates and mouse events - that's a lot of work ... that someone else has already done. The JLayer project (now JXLayer included in the JDK) can provide this functionality, and some more – MadProgrammer Jun 05 '18 at 22:27
  • 1
    J/XLayer also provides you with a lot of fun stuff you can do, including this for [example](https://stackoverflow.com/questions/34149453/java-swing-application-too-small-in-hidpi-computers/34152675#34152675), [example](https://stackoverflow.com/questions/12982863/secure-desktop-mode-effect-for-java-application/12983564#12983564), [example](https://stackoverflow.com/questions/25274566/how-can-i-change-the-highlight-color-of-a-focused-jcombobox/25276658#25276658), [example](https://stackoverflow.com/questions/35079444/brightness-implementation-for-jpanel-in-swing/35079995#35079995) – MadProgrammer Jun 05 '18 at 22:33
  • 1
    [example](https://stackoverflow.com/questions/50310352/updating-a-rotated-jlabel-using-graphics2d-causes-old-and-new-text-to-merge-toge/50311131#50311131), [example](https://stackoverflow.com/questions/25252127/java-rotating-non-square-jpanel-component/25253453#25253453) and [example](https://stackoverflow.com/questions/22976226/is-there-any-way-i-can-rotate-this-90-degrees/22976755#22976755) - so, it's an API well worth spending the time to learn – MadProgrammer Jun 05 '18 at 22:34

0 Answers0