// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ImageFunApplet extends JApplet {
    private double v;
    private int x;
    private int y;
    private String imgnam;
    private JLabel imgicn;
    public void init() {
        v = 0;
        x = getWidth()/2;
        y = getHeight()/2;
        imgnam = null;
        imgicn = null;
        getContentPane().setLayout(new BorderLayout());
        JPanel rot = new JPanel();
        rot.setLayout(new GridLayout(1, 4));
        JButton clockfast = new JButton("Rotate ++");
        clockfast.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                v += 5;
                update();
            }
        });
        rot.add(clockfast);
        JButton clock = new JButton("Rotate +");
        clock.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                v += 1;
                update();
            }
        });
        rot.add(clock);
        JButton cclock = new JButton("Rotate -");
        cclock.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                v -= 1;
                update();
            }
        });
        rot.add(cclock);
        JButton cclockfast = new JButton("Rotate --");
        cclockfast.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                v -= 5;
                update();
            }
        });
        rot.add(cclockfast);
        JButton reset = new JButton("Reset");
        reset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                v = 0;
                x = getWidth()/2;
                y = getHeight()/2;
                update();
            }
        });
        rot.add(reset);
        getContentPane().add(rot, BorderLayout.SOUTH);
        update();
    }
    public void setImage(String imgnam) {
        this.imgnam = imgnam;
        v = 0;
        x = getWidth()/2;
        y = getHeight()/2;
        update();
    }
    private void update() {
        if(imgicn != null) {
            getContentPane().remove(imgicn);
        }
        if(imgnam == null) {
            imgicn = new JLabel(getBlankIcon());
        } else {
            imgicn = new JLabel(getIcon());
        }
        imgicn.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent ev) {
                x = ev.getPoint().x;
                y = ev.getPoint().y;
                update();
            }
            public void mouseEntered(MouseEvent ev) {
            }
            public void mouseExited(MouseEvent ev) {
            }
            public void mousePressed(MouseEvent ev) {
            }
            public void mouseReleased(MouseEvent ev) {
            }
        });
        getContentPane().add(imgicn, BorderLayout.CENTER);
        invalidate();
        validate();
        getContentPane().repaint();
    }
    private ImageIcon getIcon() {
        ImageIcon imgicn = new ImageIcon(getClass().getClassLoader().getResource(imgnam));
        BufferedImage bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);  
        Graphics2D g2 = bi.createGraphics();  
        AffineTransform at = new AffineTransform();
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, bi.getWidth(), bi.getHeight());
        at.translate(x - imgicn.getIconWidth()/2, y);
        at.rotate(Math.toRadians(v), bi.getWidth()/2 - x + imgicn.getIconWidth()/2, bi.getHeight()/2 - y + imgicn.getIconHeight()/2);
        g2.drawImage(imgicn.getImage(), at, this);  
        g2.transform(at);
        g2.dispose();  
        return new ImageIcon(bi);
    }
    private ImageIcon getBlankIcon() {
        BufferedImage bi = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);  
        Graphics2D g2 = bi.createGraphics();  
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, bi.getWidth(), bi.getHeight());
        g2.dispose();  
        return new ImageIcon(bi);
    }
}
