import javax.swing.*;
import java.awt.*; import java.awt.event.*;
public class ScalingFun extends JFrame
                        implements AdjustmentListener {
    private JScrollBar scrH, scrW;
    private JComponent content;
    private Image image;
    private int width, height;

    class MyComponent extends JComponent {
        MyComponent(int w, int h) {
            setPreferredSize(new Dimension(w,h));
        }
        public void paintComponent(Graphics page) {
            int l = getWidth()/2  - width/2;
            int r = getHeight()/2 - height/2;
            page.drawImage(image,l,r,width,height,null);
    }   }
// the following is shifted to the left in order to make
// it fit on a slide
ScalingFun() { // Konstruktor
    image = Toolkit.getDefaultToolkit().
                       getImage("al-Chwarizmi.png");
    // wait for image to load...
    while (image.getHeight(null) == -1); 
    int h = height = image.getHeight(null); 
    int w = width  = image.getWidth(null);
    setLayout(new BorderLayout());
    scrH=new JScrollBar(JScrollBar.VERTICAL,h,50,0,h+50);
    scrH.addAdjustmentListener(this);
    add(scrH,"West");
    scrW=new JScrollBar(JScrollBar.HORIZONTAL,w,50,0,w+50);
    scrW.addAdjustmentListener(this);
    add(scrW,"South");
    setVisible(true);
    add(content = new MyComponent(w,h));
    pack();
}
    public void adjustmentValueChanged(AdjustmentEvent e) {
        Adjustable s = e.getAdjustable();
        int value = e.getValue();
        if (s == scrH) height = value;
        else width = value;
        revalidate();
        repaint();
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new ScalingFun());
    }
} // end of ScalingFun
