diff -r 90b924d89579 -r b166c6f49719 java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java Mon Mar 14 21:54:03 2011 +0100 @@ -0,0 +1,216 @@ +package cz.frantovo.obrazkovyApplet; + +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.awt.image.BufferedImage; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.URL; +import java.net.URLConnection; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.imageio.ImageIO; +import javax.swing.UIManager; +import javax.swing.UIManager.LookAndFeelInfo; + +public class ObrazkovyApplet extends javax.swing.JApplet implements MouseMotionListener, MouseListener { + + private static final Logger log = Logger.getLogger(ObrazkovyApplet.class.getSimpleName()); + private static final String CILOVE_URL = "http://vm.frantovo.cz/temp/applet/"; + private int stareX; + private int stareY; + private boolean kresli; + private Graphics g; + private Graphics2D vystupniGrafika; + private BufferedImage obrazek; + + private void odesliData() { + try { + URL url = new URL(CILOVE_URL); + URLConnection spojeni = url.openConnection(); + spojeni.setDoOutput(true); + spojeni.connect(); + OutputStream os = spojeni.getOutputStream(); + ImageIO.write(obrazek, "png", os); + os.flush(); + os.close(); + + BufferedReader r = new BufferedReader(new InputStreamReader(spojeni.getInputStream())); + vypisStav(r.readLine()); + r.close(); + + smazGrafiku(); + } catch (Exception e) { + vypisStav("odeslání selhalo: " + e.getMessage()); + log.log(Level.SEVERE, "Nepodařilo se odeslat data", e); + } + } + + private void smazGrafiku() { + getVystupniGrafika().clearRect(0, 0, platno.getWidth(), platno.getHeight()); + g.clearRect(0, 0, platno.getWidth(), platno.getHeight()); + } + + public void mouseDragged(MouseEvent e) { + int x = e.getX(); + int y = e.getY(); + + if (kresli) { + vypisStav("kreslím: " + stareX + "x" + stareY + "→" + x + "x" + y); + g = platno.getGraphics(); + g.setColor(Color.BLUE); + g.drawLine(stareX, stareY, x, y); + + platno.pa + + + getVystupniGrafika().drawLine(stareX, stareY, x, y); + } + + stareX = x; + stareY = y; + } + + public void mousePressed(MouseEvent e) { + stareX = e.getX(); + stareY = e.getY(); + kresli = true; + } + + public void mouseReleased(MouseEvent e) { + kresli = false; + } + + public void mouseMoved(MouseEvent e) { + } + + public void mouseClicked(MouseEvent e) { + } + + public void mouseEntered(MouseEvent e) { + } + + public void mouseExited(MouseEvent e) { + kresli = false; + } + + private Graphics2D getVystupniGrafika() { + if (obrazek == null) { + obrazek = new BufferedImage(platno.getWidth(), platno.getHeight(), BufferedImage.TYPE_INT_RGB); + } + if (vystupniGrafika == null) { + vystupniGrafika = obrazek.createGraphics(); + vystupniGrafika.setBackground(Color.WHITE); + vystupniGrafika.setColor(Color.BLUE); + vystupniGrafika.clearRect(0, 0, platno.getWidth(), platno.getHeight()); + } + return vystupniGrafika; + } + + @Override + public void init() { + + /** Lepší vzhled – LaF */ + try { + for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { + if ("Nimbus".equals(info.getName())) { + UIManager.setLookAndFeel(info.getClassName()); + break; + } + } + } catch (Exception e) { + } + + /** Spuštění appletu */ + try { + java.awt.EventQueue.invokeAndWait(new Runnable() { + + public void run() { + initComponents(); + } + }); + } catch (Exception e) { + log.log(Level.SEVERE, "Chyba appletu:", e); + } + + /** Nastavení plátna */ + platno.addMouseMotionListener(ObrazkovyApplet.this); + platno.addMouseListener(ObrazkovyApplet.this); + log.log(Level.INFO, "plátno nastaveno"); + } + + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + odeslat = new javax.swing.JButton(); + stavovyRadek = new javax.swing.JLabel(); + platno = new javax.swing.JPanel(); + + odeslat.setText("Odeslat na server"); + odeslat.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + odeslatActionPerformed(evt); + } + }); + + stavovyRadek.setText(" "); + + platno.setDoubleBuffered(true); + + javax.swing.GroupLayout platnoLayout = new javax.swing.GroupLayout(platno); + platno.setLayout(platnoLayout); + platnoLayout.setHorizontalGroup( + platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 388, Short.MAX_VALUE) + ); + platnoLayout.setVerticalGroup( + platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGap(0, 255, Short.MAX_VALUE) + ); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addContainerGap() + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(platno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addGroup(layout.createSequentialGroup() + .addComponent(stavovyRadek, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addComponent(odeslat))) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() + .addContainerGap() + .addComponent(platno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) + .addComponent(odeslat) + .addComponent(stavovyRadek)) + .addContainerGap()) + ); + }// //GEN-END:initComponents + + private void odeslatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odeslatActionPerformed + odesliData(); + }//GEN-LAST:event_odeslatActionPerformed + + private void vypisStav(String text) { + stavovyRadek.setText(text); + } + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton odeslat; + private javax.swing.JPanel platno; + private javax.swing.JLabel stavovyRadek; + // End of variables declaration//GEN-END:variables +}