import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class Obrazky extends JFrame {

	private JPanel jContentPane = null;
	private JMyPanel jMyPanel1 = null;
	private File bmp = null;
	private BufferedImage img = null;
	private boolean clicked = false;
	
	private class JMyPanel extends JPanel {
		BufferedImage image = null;
		
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			if (image == null) initImage();
			g.drawImage(image,0,0,null);
		}
		
		private void initImage() {
			if (image == null) {
//				jMyPanel1.setBackground(java.awt.Color.white);
				image = (BufferedImage) this.createImage(this.getWidth(), this.getHeight());
				Graphics g = image.getGraphics();
				g.setColor(Color.WHITE);
				g.fillRect(0,0,image.getWidth(),image.getHeight());
			}
		}
		
		public void drawImage(Image img, int x, int y) {
			if (image == null) initImage();
			image.getGraphics().drawImage(img, x, y, null);
			repaint();
		}
	}

	/**
	 * This method initializes jMyPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JMyPanel getJMyPanel1() {
		if (jMyPanel1 == null) {
			jMyPanel1 = new JMyPanel();
			jMyPanel1.setBounds(new java.awt.Rectangle(9,9,553,273));
			jMyPanel1.setBackground(java.awt.Color.white);
			jMyPanel1.addMouseListener(new java.awt.event.MouseAdapter() {   
				public void mouseReleased(java.awt.event.MouseEvent e) {    
					clicked = false;
				}   
				public void mousePressed(java.awt.event.MouseEvent e) {    
					if (e.getButton() == MouseEvent.BUTTON3) clicked = true;
				}
				public void mouseClicked(java.awt.event.MouseEvent e) {
					if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
						jMyPanel1.drawImage(Transparency.makeColorTransparent(img, Color.WHITE),
							e.getX()-img.getWidth()/2,e.getY()-img.getHeight()/2);
					}
				}
			});
			jMyPanel1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
				public void mouseDragged(java.awt.event.MouseEvent e) {
					if (clicked) {
						jMyPanel1.drawImage(Transparency.makeColorTransparent(img, Color.WHITE),
								e.getX()-img.getWidth()/2,e.getY()-img.getHeight()/2);
					}
				}
			});
		}
		return jMyPanel1;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Obrazky app = new Obrazky();
		app.setVisible(true);
	}

	/**
	 * This is the default constructor
	 */
	public Obrazky() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(576, 315);
		this.setContentPane(getJContentPane());
		this.setTitle("Okynko");
		bmp = new File("img.bmp");
		try {
			img = ImageIO.read(bmp);
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,"oops...");
		}
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.setBackground(java.awt.Color.white);
			jContentPane.add(getJMyPanel1(), null);
		}
		return jContentPane;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
