franta-hg@15: package cz.frantovo.obrazkovyApplet; franta-hg@15: franta-hg@16: import java.awt.BorderLayout; franta-hg@15: import java.awt.Color; franta-hg@15: import java.awt.Graphics; franta-hg@15: import java.awt.event.MouseEvent; franta-hg@15: import java.awt.event.MouseListener; franta-hg@15: import java.awt.event.MouseMotionListener; franta-hg@15: import java.io.BufferedReader; franta-hg@15: import java.io.InputStreamReader; franta-hg@15: import java.io.OutputStream; franta-hg@15: import java.net.URL; franta-hg@15: import java.net.URLConnection; franta-hg@15: import java.util.logging.Level; franta-hg@15: import java.util.logging.Logger; franta-hg@15: import javax.imageio.ImageIO; franta-hg@15: import javax.swing.UIManager; franta-hg@15: import javax.swing.UIManager.LookAndFeelInfo; franta-hg@15: franta-hg@16: /** franta-hg@16: * @author František Kučera, podnik.frantovo.cz franta-hg@16: */ franta-hg@15: public class ObrazkovyApplet extends javax.swing.JApplet implements MouseMotionListener, MouseListener { franta-hg@15: franta-hg@15: private static final Logger log = Logger.getLogger(ObrazkovyApplet.class.getSimpleName()); franta-hg@15: private static final String CILOVE_URL = "http://vm.frantovo.cz/temp/applet/"; franta-hg@15: private int stareX; franta-hg@15: private int stareY; franta-hg@15: private boolean kresli; franta-hg@16: private Platno platno; franta-hg@15: franta-hg@15: private void odesliData() { franta-hg@15: try { franta-hg@15: URL url = new URL(CILOVE_URL); franta-hg@15: URLConnection spojeni = url.openConnection(); franta-hg@15: spojeni.setDoOutput(true); franta-hg@15: spojeni.connect(); franta-hg@15: OutputStream os = spojeni.getOutputStream(); franta-hg@16: ImageIO.write(platno.getObrazek(), "png", os); franta-hg@15: os.flush(); franta-hg@15: os.close(); franta-hg@15: franta-hg@15: BufferedReader r = new BufferedReader(new InputStreamReader(spojeni.getInputStream())); franta-hg@15: vypisStav(r.readLine()); franta-hg@15: r.close(); franta-hg@15: franta-hg@15: smazGrafiku(); franta-hg@15: } catch (Exception e) { franta-hg@15: vypisStav("odeslání selhalo: " + e.getMessage()); franta-hg@15: log.log(Level.SEVERE, "Nepodařilo se odeslat data", e); franta-hg@15: } franta-hg@15: } franta-hg@15: franta-hg@15: private void smazGrafiku() { franta-hg@16: platno.getObrazek().getGraphics().fillRect(0, 0, panel.getWidth(), panel.getHeight()); franta-hg@16: platno.repaint(); franta-hg@15: } franta-hg@15: franta-hg@15: public void mouseDragged(MouseEvent e) { franta-hg@15: int x = e.getX(); franta-hg@15: int y = e.getY(); franta-hg@15: franta-hg@15: if (kresli) { franta-hg@15: vypisStav("kreslím: " + stareX + "x" + stareY + "→" + x + "x" + y); franta-hg@16: franta-hg@16: Graphics g = platno.getObrazek().getGraphics(); franta-hg@15: g.setColor(Color.BLUE); franta-hg@15: g.drawLine(stareX, stareY, x, y); franta-hg@15: franta-hg@16: platno.repaint(); franta-hg@15: } franta-hg@15: franta-hg@15: stareX = x; franta-hg@15: stareY = y; franta-hg@15: } franta-hg@15: franta-hg@15: public void mousePressed(MouseEvent e) { franta-hg@15: stareX = e.getX(); franta-hg@15: stareY = e.getY(); franta-hg@15: kresli = true; franta-hg@15: } franta-hg@15: franta-hg@15: public void mouseReleased(MouseEvent e) { franta-hg@15: kresli = false; franta-hg@15: } franta-hg@15: franta-hg@15: public void mouseMoved(MouseEvent e) { franta-hg@15: } franta-hg@15: franta-hg@15: public void mouseClicked(MouseEvent e) { franta-hg@15: } franta-hg@15: franta-hg@15: public void mouseEntered(MouseEvent e) { franta-hg@15: } franta-hg@15: franta-hg@15: public void mouseExited(MouseEvent e) { franta-hg@15: kresli = false; franta-hg@15: } franta-hg@15: franta-hg@15: @Override franta-hg@15: public void init() { franta-hg@15: franta-hg@15: /** Lepší vzhled – LaF */ franta-hg@15: try { franta-hg@15: for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { franta-hg@15: if ("Nimbus".equals(info.getName())) { franta-hg@15: UIManager.setLookAndFeel(info.getClassName()); franta-hg@15: break; franta-hg@15: } franta-hg@15: } franta-hg@15: } catch (Exception e) { franta-hg@15: } franta-hg@15: franta-hg@15: /** Spuštění appletu */ franta-hg@15: try { franta-hg@15: java.awt.EventQueue.invokeAndWait(new Runnable() { franta-hg@15: franta-hg@15: public void run() { franta-hg@16: platno = new Platno(); franta-hg@16: platno.repaint(); franta-hg@15: initComponents(); franta-hg@16: panel.setLayout(new BorderLayout()); franta-hg@16: panel.add(platno, BorderLayout.CENTER); franta-hg@15: } franta-hg@15: }); franta-hg@15: } catch (Exception e) { franta-hg@15: log.log(Level.SEVERE, "Chyba appletu:", e); franta-hg@15: } franta-hg@15: franta-hg@15: /** Nastavení plátna */ franta-hg@16: panel.addMouseMotionListener(ObrazkovyApplet.this); franta-hg@16: panel.addMouseListener(ObrazkovyApplet.this); franta-hg@15: log.log(Level.INFO, "plátno nastaveno"); franta-hg@15: } franta-hg@15: franta-hg@15: @SuppressWarnings("unchecked") franta-hg@15: // //GEN-BEGIN:initComponents franta-hg@15: private void initComponents() { franta-hg@15: franta-hg@15: odeslat = new javax.swing.JButton(); franta-hg@15: stavovyRadek = new javax.swing.JLabel(); franta-hg@16: panel = new javax.swing.JPanel(); franta-hg@15: franta-hg@15: odeslat.setText("Odeslat na server"); franta-hg@15: odeslat.addActionListener(new java.awt.event.ActionListener() { franta-hg@15: public void actionPerformed(java.awt.event.ActionEvent evt) { franta-hg@15: odeslatActionPerformed(evt); franta-hg@15: } franta-hg@15: }); franta-hg@15: franta-hg@15: stavovyRadek.setText(" "); franta-hg@15: franta-hg@16: javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel); franta-hg@16: panel.setLayout(panelLayout); franta-hg@16: panelLayout.setHorizontalGroup( franta-hg@16: panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) franta-hg@15: .addGap(0, 388, Short.MAX_VALUE) franta-hg@15: ); franta-hg@16: panelLayout.setVerticalGroup( franta-hg@16: panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) franta-hg@15: .addGap(0, 255, Short.MAX_VALUE) franta-hg@15: ); franta-hg@15: franta-hg@15: javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); franta-hg@15: getContentPane().setLayout(layout); franta-hg@15: layout.setHorizontalGroup( franta-hg@15: layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) franta-hg@15: .addGroup(layout.createSequentialGroup() franta-hg@15: .addContainerGap() franta-hg@15: .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) franta-hg@16: .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) franta-hg@15: .addGroup(layout.createSequentialGroup() franta-hg@15: .addComponent(stavovyRadek, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE) franta-hg@15: .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) franta-hg@15: .addComponent(odeslat))) franta-hg@15: .addContainerGap()) franta-hg@15: ); franta-hg@15: layout.setVerticalGroup( franta-hg@15: layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) franta-hg@15: .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() franta-hg@15: .addContainerGap() franta-hg@16: .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) franta-hg@15: .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) franta-hg@15: .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) franta-hg@15: .addComponent(odeslat) franta-hg@15: .addComponent(stavovyRadek)) franta-hg@15: .addContainerGap()) franta-hg@15: ); franta-hg@15: }// //GEN-END:initComponents franta-hg@15: franta-hg@15: private void odeslatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odeslatActionPerformed franta-hg@15: odesliData(); franta-hg@15: }//GEN-LAST:event_odeslatActionPerformed franta-hg@15: franta-hg@15: private void vypisStav(String text) { franta-hg@15: stavovyRadek.setText(text); franta-hg@15: } franta-hg@15: // Variables declaration - do not modify//GEN-BEGIN:variables franta-hg@15: private javax.swing.JButton odeslat; franta-hg@16: private javax.swing.JPanel panel; franta-hg@15: private javax.swing.JLabel stavovyRadek; franta-hg@15: // End of variables declaration//GEN-END:variables franta-hg@15: }