1 package cz.frantovo.obrazkovyApplet;
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;
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;
21 public class ObrazkovyApplet extends javax.swing.JApplet implements MouseMotionListener, MouseListener {
23 private static final Logger log = Logger.getLogger(ObrazkovyApplet.class.getSimpleName());
24 private static final String CILOVE_URL = "http://vm.frantovo.cz/temp/applet/";
27 private boolean kresli;
29 private Graphics2D vystupniGrafika;
30 private BufferedImage obrazek;
32 private void odesliData() {
34 URL url = new URL(CILOVE_URL);
35 URLConnection spojeni = url.openConnection();
36 spojeni.setDoOutput(true);
38 OutputStream os = spojeni.getOutputStream();
39 ImageIO.write(obrazek, "png", os);
43 BufferedReader r = new BufferedReader(new InputStreamReader(spojeni.getInputStream()));
44 vypisStav(r.readLine());
48 } catch (Exception e) {
49 vypisStav("odeslání selhalo: " + e.getMessage());
50 log.log(Level.SEVERE, "Nepodařilo se odeslat data", e);
54 private void smazGrafiku() {
55 getVystupniGrafika().clearRect(0, 0, platno.getWidth(), platno.getHeight());
56 g.clearRect(0, 0, platno.getWidth(), platno.getHeight());
59 public void mouseDragged(MouseEvent e) {
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);
72 getVystupniGrafika().drawLine(stareX, stareY, x, y);
79 public void mousePressed(MouseEvent e) {
85 public void mouseReleased(MouseEvent e) {
89 public void mouseMoved(MouseEvent e) {
92 public void mouseClicked(MouseEvent e) {
95 public void mouseEntered(MouseEvent e) {
98 public void mouseExited(MouseEvent e) {
102 private Graphics2D getVystupniGrafika() {
103 if (obrazek == null) {
104 obrazek = new BufferedImage(platno.getWidth(), platno.getHeight(), BufferedImage.TYPE_INT_RGB);
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());
112 return vystupniGrafika;
118 /** Lepší vzhled – LaF */
120 for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
121 if ("Nimbus".equals(info.getName())) {
122 UIManager.setLookAndFeel(info.getClassName());
126 } catch (Exception e) {
129 /** Spuštění appletu */
131 java.awt.EventQueue.invokeAndWait(new Runnable() {
137 } catch (Exception e) {
138 log.log(Level.SEVERE, "Chyba appletu:", e);
141 /** Nastavení plátna */
142 platno.addMouseMotionListener(ObrazkovyApplet.this);
143 platno.addMouseListener(ObrazkovyApplet.this);
144 log.log(Level.INFO, "plátno nastaveno");
147 @SuppressWarnings("unchecked")
148 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
149 private void initComponents() {
151 odeslat = new javax.swing.JButton();
152 stavovyRadek = new javax.swing.JLabel();
153 platno = new javax.swing.JPanel();
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);
162 stavovyRadek.setText(" ");
164 platno.setDoubleBuffered(true);
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)
172 platnoLayout.setVerticalGroup(
173 platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
174 .addGap(0, 255, Short.MAX_VALUE)
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()
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)))
191 layout.setVerticalGroup(
192 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
193 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
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))
202 }// </editor-fold>//GEN-END:initComponents
204 private void odeslatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odeslatActionPerformed
206 }//GEN-LAST:event_odeslatActionPerformed
208 private void vypisStav(String text) {
209 stavovyRadek.setText(text);
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