java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java
changeset 15 b166c6f49719
child 16 131d2652b241
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java	Mon Mar 14 21:54:03 2011 +0100
     1.3 @@ -0,0 +1,216 @@
     1.4 +package cz.frantovo.obrazkovyApplet;
     1.5 +
     1.6 +import java.awt.Color;
     1.7 +import java.awt.Graphics;
     1.8 +import java.awt.Graphics2D;
     1.9 +import java.awt.event.MouseEvent;
    1.10 +import java.awt.event.MouseListener;
    1.11 +import java.awt.event.MouseMotionListener;
    1.12 +import java.awt.image.BufferedImage;
    1.13 +import java.io.BufferedReader;
    1.14 +import java.io.InputStreamReader;
    1.15 +import java.io.OutputStream;
    1.16 +import java.net.URL;
    1.17 +import java.net.URLConnection;
    1.18 +import java.util.logging.Level;
    1.19 +import java.util.logging.Logger;
    1.20 +import javax.imageio.ImageIO;
    1.21 +import javax.swing.UIManager;
    1.22 +import javax.swing.UIManager.LookAndFeelInfo;
    1.23 +
    1.24 +public class ObrazkovyApplet extends javax.swing.JApplet implements MouseMotionListener, MouseListener {
    1.25 +
    1.26 +	private static final Logger log = Logger.getLogger(ObrazkovyApplet.class.getSimpleName());
    1.27 +	private static final String CILOVE_URL = "http://vm.frantovo.cz/temp/applet/";
    1.28 +	private int stareX;
    1.29 +	private int stareY;
    1.30 +	private boolean kresli;
    1.31 +	private Graphics g;
    1.32 +	private Graphics2D vystupniGrafika;
    1.33 +	private BufferedImage obrazek;
    1.34 +
    1.35 +	private void odesliData() {
    1.36 +		try {
    1.37 +			URL url = new URL(CILOVE_URL);
    1.38 +			URLConnection spojeni = url.openConnection();
    1.39 +			spojeni.setDoOutput(true);
    1.40 +			spojeni.connect();
    1.41 +			OutputStream os = spojeni.getOutputStream();
    1.42 +			ImageIO.write(obrazek, "png", os);
    1.43 +			os.flush();
    1.44 +			os.close();
    1.45 +
    1.46 +			BufferedReader r = new BufferedReader(new InputStreamReader(spojeni.getInputStream()));
    1.47 +			vypisStav(r.readLine());
    1.48 +			r.close();
    1.49 +
    1.50 +			smazGrafiku();
    1.51 +		} catch (Exception e) {
    1.52 +			vypisStav("odeslání selhalo: " + e.getMessage());
    1.53 +			log.log(Level.SEVERE, "Nepodařilo se odeslat data", e);
    1.54 +		}
    1.55 +	}
    1.56 +
    1.57 +	private void smazGrafiku() {
    1.58 +		getVystupniGrafika().clearRect(0, 0, platno.getWidth(), platno.getHeight());
    1.59 +		g.clearRect(0, 0, platno.getWidth(), platno.getHeight());
    1.60 +	}
    1.61 +
    1.62 +	public void mouseDragged(MouseEvent e) {
    1.63 +		int x = e.getX();
    1.64 +		int y = e.getY();
    1.65 +
    1.66 +		if (kresli) {
    1.67 +			vypisStav("kreslím: " + stareX + "x" + stareY + "→" + x + "x" + y);
    1.68 +			g = platno.getGraphics();
    1.69 +			g.setColor(Color.BLUE);
    1.70 +			g.drawLine(stareX, stareY, x, y);
    1.71 +
    1.72 +			platno.pa
    1.73 +
    1.74 +			
    1.75 +			getVystupniGrafika().drawLine(stareX, stareY, x, y);
    1.76 +		}
    1.77 +
    1.78 +		stareX = x;
    1.79 +		stareY = y;
    1.80 +	}
    1.81 +
    1.82 +	public void mousePressed(MouseEvent e) {
    1.83 +		stareX = e.getX();
    1.84 +		stareY = e.getY();
    1.85 +		kresli = true;
    1.86 +	}
    1.87 +
    1.88 +	public void mouseReleased(MouseEvent e) {
    1.89 +		kresli = false;
    1.90 +	}
    1.91 +
    1.92 +	public void mouseMoved(MouseEvent e) {
    1.93 +	}
    1.94 +
    1.95 +	public void mouseClicked(MouseEvent e) {
    1.96 +	}
    1.97 +
    1.98 +	public void mouseEntered(MouseEvent e) {
    1.99 +	}
   1.100 +
   1.101 +	public void mouseExited(MouseEvent e) {
   1.102 +		kresli = false;
   1.103 +	}
   1.104 +
   1.105 +	private Graphics2D getVystupniGrafika() {
   1.106 +		if (obrazek == null) {
   1.107 +			obrazek = new BufferedImage(platno.getWidth(), platno.getHeight(), BufferedImage.TYPE_INT_RGB);
   1.108 +		}
   1.109 +		if (vystupniGrafika == null) {
   1.110 +			vystupniGrafika = obrazek.createGraphics();
   1.111 +			vystupniGrafika.setBackground(Color.WHITE);
   1.112 +			vystupniGrafika.setColor(Color.BLUE);
   1.113 +			vystupniGrafika.clearRect(0, 0, platno.getWidth(), platno.getHeight());
   1.114 +		}
   1.115 +		return vystupniGrafika;
   1.116 +	}
   1.117 +
   1.118 +	@Override
   1.119 +	public void init() {
   1.120 +
   1.121 +		/** Lepší vzhled – LaF */
   1.122 +		try {
   1.123 +			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
   1.124 +				if ("Nimbus".equals(info.getName())) {
   1.125 +					UIManager.setLookAndFeel(info.getClassName());
   1.126 +					break;
   1.127 +				}
   1.128 +			}
   1.129 +		} catch (Exception e) {
   1.130 +		}
   1.131 +
   1.132 +		/** Spuštění appletu */
   1.133 +		try {
   1.134 +			java.awt.EventQueue.invokeAndWait(new Runnable() {
   1.135 +
   1.136 +				public void run() {
   1.137 +					initComponents();
   1.138 +				}
   1.139 +			});
   1.140 +		} catch (Exception e) {
   1.141 +			log.log(Level.SEVERE, "Chyba appletu:", e);
   1.142 +		}
   1.143 +
   1.144 +		/** Nastavení plátna */
   1.145 +		platno.addMouseMotionListener(ObrazkovyApplet.this);
   1.146 +		platno.addMouseListener(ObrazkovyApplet.this);
   1.147 +		log.log(Level.INFO, "plátno nastaveno");
   1.148 +	}
   1.149 +
   1.150 +	@SuppressWarnings("unchecked")
   1.151 +    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   1.152 +    private void initComponents() {
   1.153 +
   1.154 +        odeslat = new javax.swing.JButton();
   1.155 +        stavovyRadek = new javax.swing.JLabel();
   1.156 +        platno = new javax.swing.JPanel();
   1.157 +
   1.158 +        odeslat.setText("Odeslat na server");
   1.159 +        odeslat.addActionListener(new java.awt.event.ActionListener() {
   1.160 +            public void actionPerformed(java.awt.event.ActionEvent evt) {
   1.161 +                odeslatActionPerformed(evt);
   1.162 +            }
   1.163 +        });
   1.164 +
   1.165 +        stavovyRadek.setText(" ");
   1.166 +
   1.167 +        platno.setDoubleBuffered(true);
   1.168 +
   1.169 +        javax.swing.GroupLayout platnoLayout = new javax.swing.GroupLayout(platno);
   1.170 +        platno.setLayout(platnoLayout);
   1.171 +        platnoLayout.setHorizontalGroup(
   1.172 +            platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.173 +            .addGap(0, 388, Short.MAX_VALUE)
   1.174 +        );
   1.175 +        platnoLayout.setVerticalGroup(
   1.176 +            platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.177 +            .addGap(0, 255, Short.MAX_VALUE)
   1.178 +        );
   1.179 +
   1.180 +        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
   1.181 +        getContentPane().setLayout(layout);
   1.182 +        layout.setHorizontalGroup(
   1.183 +            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.184 +            .addGroup(layout.createSequentialGroup()
   1.185 +                .addContainerGap()
   1.186 +                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.187 +                    .addComponent(platno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   1.188 +                    .addGroup(layout.createSequentialGroup()
   1.189 +                        .addComponent(stavovyRadek, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
   1.190 +                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   1.191 +                        .addComponent(odeslat)))
   1.192 +                .addContainerGap())
   1.193 +        );
   1.194 +        layout.setVerticalGroup(
   1.195 +            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.196 +            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
   1.197 +                .addContainerGap()
   1.198 +                .addComponent(platno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   1.199 +                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   1.200 +                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   1.201 +                    .addComponent(odeslat)
   1.202 +                    .addComponent(stavovyRadek))
   1.203 +                .addContainerGap())
   1.204 +        );
   1.205 +    }// </editor-fold>//GEN-END:initComponents
   1.206 +
   1.207 +	private void odeslatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odeslatActionPerformed
   1.208 +		odesliData();
   1.209 +	}//GEN-LAST:event_odeslatActionPerformed
   1.210 +
   1.211 +	private void vypisStav(String text) {
   1.212 +		stavovyRadek.setText(text);
   1.213 +	}
   1.214 +    // Variables declaration - do not modify//GEN-BEGIN:variables
   1.215 +    private javax.swing.JButton odeslat;
   1.216 +    private javax.swing.JPanel platno;
   1.217 +    private javax.swing.JLabel stavovyRadek;
   1.218 +    // End of variables declaration//GEN-END:variables
   1.219 +}