java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java
author František Kučera <franta-hg@frantovo.cz>
Mon, 14 Mar 2011 21:54:03 +0100
changeset 15 b166c6f49719
child 16 131d2652b241
permissions -rw-r--r--
První návrh
     1 package cz.frantovo.obrazkovyApplet;
     2 
     3 import java.awt.Color;
     4 import java.awt.Graphics;
     5 import java.awt.Graphics2D;
     6 import java.awt.event.MouseEvent;
     7 import java.awt.event.MouseListener;
     8 import java.awt.event.MouseMotionListener;
     9 import java.awt.image.BufferedImage;
    10 import java.io.BufferedReader;
    11 import java.io.InputStreamReader;
    12 import java.io.OutputStream;
    13 import java.net.URL;
    14 import java.net.URLConnection;
    15 import java.util.logging.Level;
    16 import java.util.logging.Logger;
    17 import javax.imageio.ImageIO;
    18 import javax.swing.UIManager;
    19 import javax.swing.UIManager.LookAndFeelInfo;
    20 
    21 public class ObrazkovyApplet extends javax.swing.JApplet implements MouseMotionListener, MouseListener {
    22 
    23 	private static final Logger log = Logger.getLogger(ObrazkovyApplet.class.getSimpleName());
    24 	private static final String CILOVE_URL = "http://vm.frantovo.cz/temp/applet/";
    25 	private int stareX;
    26 	private int stareY;
    27 	private boolean kresli;
    28 	private Graphics g;
    29 	private Graphics2D vystupniGrafika;
    30 	private BufferedImage obrazek;
    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(obrazek, "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 		getVystupniGrafika().clearRect(0, 0, platno.getWidth(), platno.getHeight());
    56 		g.clearRect(0, 0, platno.getWidth(), platno.getHeight());
    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 			g = platno.getGraphics();
    66 			g.setColor(Color.BLUE);
    67 			g.drawLine(stareX, stareY, x, y);
    68 
    69 			platno.pa
    70 
    71 			
    72 			getVystupniGrafika().drawLine(stareX, stareY, x, y);
    73 		}
    74 
    75 		stareX = x;
    76 		stareY = y;
    77 	}
    78 
    79 	public void mousePressed(MouseEvent e) {
    80 		stareX = e.getX();
    81 		stareY = e.getY();
    82 		kresli = true;
    83 	}
    84 
    85 	public void mouseReleased(MouseEvent e) {
    86 		kresli = false;
    87 	}
    88 
    89 	public void mouseMoved(MouseEvent e) {
    90 	}
    91 
    92 	public void mouseClicked(MouseEvent e) {
    93 	}
    94 
    95 	public void mouseEntered(MouseEvent e) {
    96 	}
    97 
    98 	public void mouseExited(MouseEvent e) {
    99 		kresli = false;
   100 	}
   101 
   102 	private Graphics2D getVystupniGrafika() {
   103 		if (obrazek == null) {
   104 			obrazek = new BufferedImage(platno.getWidth(), platno.getHeight(), BufferedImage.TYPE_INT_RGB);
   105 		}
   106 		if (vystupniGrafika == null) {
   107 			vystupniGrafika = obrazek.createGraphics();
   108 			vystupniGrafika.setBackground(Color.WHITE);
   109 			vystupniGrafika.setColor(Color.BLUE);
   110 			vystupniGrafika.clearRect(0, 0, platno.getWidth(), platno.getHeight());
   111 		}
   112 		return vystupniGrafika;
   113 	}
   114 
   115 	@Override
   116 	public void init() {
   117 
   118 		/** Lepší vzhled – LaF */
   119 		try {
   120 			for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
   121 				if ("Nimbus".equals(info.getName())) {
   122 					UIManager.setLookAndFeel(info.getClassName());
   123 					break;
   124 				}
   125 			}
   126 		} catch (Exception e) {
   127 		}
   128 
   129 		/** Spuštění appletu */
   130 		try {
   131 			java.awt.EventQueue.invokeAndWait(new Runnable() {
   132 
   133 				public void run() {
   134 					initComponents();
   135 				}
   136 			});
   137 		} catch (Exception e) {
   138 			log.log(Level.SEVERE, "Chyba appletu:", e);
   139 		}
   140 
   141 		/** Nastavení plátna */
   142 		platno.addMouseMotionListener(ObrazkovyApplet.this);
   143 		platno.addMouseListener(ObrazkovyApplet.this);
   144 		log.log(Level.INFO, "plátno nastaveno");
   145 	}
   146 
   147 	@SuppressWarnings("unchecked")
   148     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   149     private void initComponents() {
   150 
   151         odeslat = new javax.swing.JButton();
   152         stavovyRadek = new javax.swing.JLabel();
   153         platno = new javax.swing.JPanel();
   154 
   155         odeslat.setText("Odeslat na server");
   156         odeslat.addActionListener(new java.awt.event.ActionListener() {
   157             public void actionPerformed(java.awt.event.ActionEvent evt) {
   158                 odeslatActionPerformed(evt);
   159             }
   160         });
   161 
   162         stavovyRadek.setText(" ");
   163 
   164         platno.setDoubleBuffered(true);
   165 
   166         javax.swing.GroupLayout platnoLayout = new javax.swing.GroupLayout(platno);
   167         platno.setLayout(platnoLayout);
   168         platnoLayout.setHorizontalGroup(
   169             platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   170             .addGap(0, 388, Short.MAX_VALUE)
   171         );
   172         platnoLayout.setVerticalGroup(
   173             platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   174             .addGap(0, 255, Short.MAX_VALUE)
   175         );
   176 
   177         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
   178         getContentPane().setLayout(layout);
   179         layout.setHorizontalGroup(
   180             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   181             .addGroup(layout.createSequentialGroup()
   182                 .addContainerGap()
   183                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   184                     .addComponent(platno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   185                     .addGroup(layout.createSequentialGroup()
   186                         .addComponent(stavovyRadek, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
   187                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   188                         .addComponent(odeslat)))
   189                 .addContainerGap())
   190         );
   191         layout.setVerticalGroup(
   192             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   193             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
   194                 .addContainerGap()
   195                 .addComponent(platno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   196                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   197                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   198                     .addComponent(odeslat)
   199                     .addComponent(stavovyRadek))
   200                 .addContainerGap())
   201         );
   202     }// </editor-fold>//GEN-END:initComponents
   203 
   204 	private void odeslatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odeslatActionPerformed
   205 		odesliData();
   206 	}//GEN-LAST:event_odeslatActionPerformed
   207 
   208 	private void vypisStav(String text) {
   209 		stavovyRadek.setText(text);
   210 	}
   211     // Variables declaration - do not modify//GEN-BEGIN:variables
   212     private javax.swing.JButton odeslat;
   213     private javax.swing.JPanel platno;
   214     private javax.swing.JLabel stavovyRadek;
   215     // End of variables declaration//GEN-END:variables
   216 }