java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 19 Nov 2016 20:10:17 +0100
changeset 34 117e8ff77326
parent 16 131d2652b241
permissions -rw-r--r--
libevent: předávání dat z listener_cb() do conn_read_cb()
     1 package cz.frantovo.obrazkovyApplet;
     2 
     3 import java.awt.BorderLayout;
     4 import java.awt.Color;
     5 import java.awt.Graphics;
     6 import java.awt.event.MouseEvent;
     7 import java.awt.event.MouseListener;
     8 import java.awt.event.MouseMotionListener;
     9 import java.io.BufferedReader;
    10 import java.io.InputStreamReader;
    11 import java.io.OutputStream;
    12 import java.net.URL;
    13 import java.net.URLConnection;
    14 import java.util.logging.Level;
    15 import java.util.logging.Logger;
    16 import javax.imageio.ImageIO;
    17 import javax.swing.UIManager;
    18 import javax.swing.UIManager.LookAndFeelInfo;
    19 
    20 /**
    21  * @author František Kučera, podnik.frantovo.cz
    22  */
    23 public class ObrazkovyApplet extends javax.swing.JApplet implements MouseMotionListener, MouseListener {
    24 
    25 	private static final Logger log = Logger.getLogger(ObrazkovyApplet.class.getSimpleName());
    26 	private static final String CILOVE_URL = "http://vm.frantovo.cz/temp/applet/";
    27 	private int stareX;
    28 	private int stareY;
    29 	private boolean kresli;
    30 	private Platno platno;
    31 
    32 	private void odesliData() {
    33 		try {
    34 			URL url = new URL(CILOVE_URL);
    35 			URLConnection spojeni = url.openConnection();
    36 			spojeni.setDoOutput(true);
    37 			spojeni.connect();
    38 			OutputStream os = spojeni.getOutputStream();
    39 			ImageIO.write(platno.getObrazek(), "png", os);
    40 			os.flush();
    41 			os.close();
    42 
    43 			BufferedReader r = new BufferedReader(new InputStreamReader(spojeni.getInputStream()));
    44 			vypisStav(r.readLine());
    45 			r.close();
    46 
    47 			smazGrafiku();
    48 		} catch (Exception e) {
    49 			vypisStav("odeslání selhalo: " + e.getMessage());
    50 			log.log(Level.SEVERE, "Nepodařilo se odeslat data", e);
    51 		}
    52 	}
    53 
    54 	private void smazGrafiku() {
    55 		platno.getObrazek().getGraphics().fillRect(0, 0, panel.getWidth(), panel.getHeight());
    56 		platno.repaint();
    57 	}
    58 
    59 	public void mouseDragged(MouseEvent e) {
    60 		int x = e.getX();
    61 		int y = e.getY();
    62 
    63 		if (kresli) {
    64 			vypisStav("kreslím: " + stareX + "x" + stareY + "→" + x + "x" + y);
    65 
    66 			Graphics g = platno.getObrazek().getGraphics();
    67 			g.setColor(Color.BLUE);
    68 			g.drawLine(stareX, stareY, x, y);
    69 
    70 			platno.repaint();
    71 		}
    72 
    73 		stareX = x;
    74 		stareY = y;
    75 	}
    76 
    77 	public void mousePressed(MouseEvent e) {
    78 		stareX = e.getX();
    79 		stareY = e.getY();
    80 		kresli = true;
    81 	}
    82 
    83 	public void mouseReleased(MouseEvent e) {
    84 		kresli = false;
    85 	}
    86 
    87 	public void mouseMoved(MouseEvent e) {
    88 	}
    89 
    90 	public void mouseClicked(MouseEvent e) {
    91 	}
    92 
    93 	public void mouseEntered(MouseEvent e) {
    94 	}
    95 
    96 	public void mouseExited(MouseEvent e) {
    97 		kresli = false;
    98 	}
    99 
   100 	@Override
   101 	public void init() {
   102 
   103 		/** Lepší vzhled – LaF */
   104 		try {
   105 			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
   106 				if ("Nimbus".equals(info.getName())) {
   107 					UIManager.setLookAndFeel(info.getClassName());
   108 					break;
   109 				}
   110 			}
   111 		} catch (Exception e) {
   112 		}
   113 
   114 		/** Spuštění appletu */
   115 		try {
   116 			java.awt.EventQueue.invokeAndWait(new Runnable() {
   117 
   118 				public void run() {
   119 					platno = new Platno();
   120 					platno.repaint();
   121 					initComponents();
   122 					panel.setLayout(new BorderLayout());
   123 					panel.add(platno, BorderLayout.CENTER);
   124 				}
   125 			});
   126 		} catch (Exception e) {
   127 			log.log(Level.SEVERE, "Chyba appletu:", e);
   128 		}
   129 
   130 		/** Nastavení plátna */
   131 		panel.addMouseMotionListener(ObrazkovyApplet.this);
   132 		panel.addMouseListener(ObrazkovyApplet.this);
   133 		log.log(Level.INFO, "plátno nastaveno");
   134 	}
   135 
   136 	@SuppressWarnings("unchecked")
   137     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   138     private void initComponents() {
   139 
   140         odeslat = new javax.swing.JButton();
   141         stavovyRadek = new javax.swing.JLabel();
   142         panel = new javax.swing.JPanel();
   143 
   144         odeslat.setText("Odeslat na server");
   145         odeslat.addActionListener(new java.awt.event.ActionListener() {
   146             public void actionPerformed(java.awt.event.ActionEvent evt) {
   147                 odeslatActionPerformed(evt);
   148             }
   149         });
   150 
   151         stavovyRadek.setText(" ");
   152 
   153         javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
   154         panel.setLayout(panelLayout);
   155         panelLayout.setHorizontalGroup(
   156             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   157             .addGap(0, 388, Short.MAX_VALUE)
   158         );
   159         panelLayout.setVerticalGroup(
   160             panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   161             .addGap(0, 255, Short.MAX_VALUE)
   162         );
   163 
   164         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
   165         getContentPane().setLayout(layout);
   166         layout.setHorizontalGroup(
   167             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   168             .addGroup(layout.createSequentialGroup()
   169                 .addContainerGap()
   170                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   171                     .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   172                     .addGroup(layout.createSequentialGroup()
   173                         .addComponent(stavovyRadek, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
   174                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   175                         .addComponent(odeslat)))
   176                 .addContainerGap())
   177         );
   178         layout.setVerticalGroup(
   179             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   180             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
   181                 .addContainerGap()
   182                 .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   183                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   184                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   185                     .addComponent(odeslat)
   186                     .addComponent(stavovyRadek))
   187                 .addContainerGap())
   188         );
   189     }// </editor-fold>//GEN-END:initComponents
   190 
   191 	private void odeslatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odeslatActionPerformed
   192 		odesliData();
   193 	}//GEN-LAST:event_odeslatActionPerformed
   194 
   195 	private void vypisStav(String text) {
   196 		stavovyRadek.setText(text);
   197 	}
   198     // Variables declaration - do not modify//GEN-BEGIN:variables
   199     private javax.swing.JButton odeslat;
   200     private javax.swing.JPanel panel;
   201     private javax.swing.JLabel stavovyRadek;
   202     // End of variables declaration//GEN-END:variables
   203 }