// 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.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class DrawApplet extends JApplet implements ActionListener {
    private final static String UPLOAD_URL = "http://localhost/upload.asp";
    private DrawablePicture dp;
    private JButton black;
    private JButton red;
    private JButton green;
    private JButton blue;
    private JButton upload;
    public void init() {
        setLayout(new BorderLayout());
        dp = new DrawablePicture();
        getContentPane().add(dp, BorderLayout.CENTER);
        JPanel btns = new JPanel();
        btns.setLayout(new GridLayout(1,5));
        black = new JButton("Black");
        black.setForeground(Color.black);
        black.addActionListener(this);
        btns.add(black);
        red = new JButton("Red");
        red.setForeground(Color.RED);
        red.addActionListener(this);
        btns.add(red);
        green = new JButton("Green");
        green.setForeground(Color.GREEN);
        green.addActionListener(this);
        btns.add(green);
        blue = new JButton("Blue");
        blue.setForeground(Color.BLUE);
        blue.addActionListener(this);
        btns.add(blue);
        upload = new JButton("Upload");
        upload.addActionListener(this);
        btns.add(upload);
        getContentPane().add(btns, BorderLayout.SOUTH);
    }
    public void actionPerformed(ActionEvent ev) {
        if(ev.getSource() == black) {
            dp.setColor(Color.BLACK);
        } else if(ev.getSource() == red) {
            dp.setColor(Color.RED);
        } else if(ev.getSource() == green) {
            dp.setColor(Color.GREEN);
        } else if(ev.getSource() == blue) {
            dp.setColor(Color.BLUE);
        } else if(ev.getSource() == upload) {
            try {
                String filename = this.getParameter("filename");
                URL url = new URL(UPLOAD_URL + "?filename=" + filename);
                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("POST");
                con.setDoOutput(true);
                OutputStream os = con.getOutputStream();
                ImageIO.write(dp.getImg(), "jpeg", os);
                con.connect();
                if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    JOptionPane.showMessageDialog(this, "Picture uploaded");
                } else {
                    JOptionPane.showMessageDialog(this, "Picture not uploaded");
                }
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(this, "Picture not uploaded");
            }
        }
    }
}

class DrawablePicture extends JPanel implements MouseMotionListener {
    private final static String STARTPIC_URL = "http://localhost/bg.jpg";
    private final static int W = 750;
    private final static int H = 250;
    private BufferedImage img;
    private Color drawcol;
    private int lastx;
    private int lasty;
    public DrawablePicture() {
        try {
            img = ImageIO.read(new URL(STARTPIC_URL));
            setPreferredSize(new Dimension(W, H));
            this.addMouseMotionListener(this);
            drawcol = Color.BLACK;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
    }
    public BufferedImage getImg() {
        return img;
    }
    public void mouseDragged(MouseEvent ev) {
        Graphics g = img.getGraphics();
        g.setColor(drawcol);
        g.drawLine(lastx, lasty, ev.getX(), ev.getY());
        repaint();
        lastx = ev.getX();
        lasty = ev.getY();
    }
    public void mouseMoved(MouseEvent ev) {
        lastx = ev.getX();
        lasty = ev.getY();
    }
    public void setColor(Color drawcol) {
        this.drawcol = drawcol;
    }
}
