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