LV2: modul zesilovače, dle oficiálního příkladu, ale bez závislosti na Pythonu – stačí gcc a make
1 package cz.frantovo.obrazkovyApplet;
3 import java.awt.BorderLayout;
5 import java.awt.Graphics;
6 import java.awt.event.MouseEvent;
7 import java.awt.event.MouseListener;
8 import java.awt.event.MouseMotionListener;
9 import java.io.BufferedReader;
10 import java.io.InputStreamReader;
11 import java.io.OutputStream;
13 import java.net.URLConnection;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16 import javax.imageio.ImageIO;
17 import javax.swing.UIManager;
18 import javax.swing.UIManager.LookAndFeelInfo;
21 * @author František Kučera, podnik.frantovo.cz
23 public class ObrazkovyApplet extends javax.swing.JApplet implements MouseMotionListener, MouseListener {
25 private static final Logger log = Logger.getLogger(ObrazkovyApplet.class.getSimpleName());
26 private static final String CILOVE_URL = "http://vm.frantovo.cz/temp/applet/";
29 private boolean kresli;
30 private Platno platno;
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(platno.getObrazek(), "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 platno.getObrazek().getGraphics().fillRect(0, 0, panel.getWidth(), panel.getHeight());
59 public void mouseDragged(MouseEvent e) {
64 vypisStav("kreslím: " + stareX + "x" + stareY + "→" + x + "x" + y);
66 Graphics g = platno.getObrazek().getGraphics();
67 g.setColor(Color.BLUE);
68 g.drawLine(stareX, stareY, x, y);
77 public void mousePressed(MouseEvent e) {
83 public void mouseReleased(MouseEvent e) {
87 public void mouseMoved(MouseEvent e) {
90 public void mouseClicked(MouseEvent e) {
93 public void mouseEntered(MouseEvent e) {
96 public void mouseExited(MouseEvent e) {
103 /** Lepší vzhled – LaF */
105 for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
106 if ("Nimbus".equals(info.getName())) {
107 UIManager.setLookAndFeel(info.getClassName());
111 } catch (Exception e) {
114 /** Spuštění appletu */
116 java.awt.EventQueue.invokeAndWait(new Runnable() {
119 platno = new Platno();
122 panel.setLayout(new BorderLayout());
123 panel.add(platno, BorderLayout.CENTER);
126 } catch (Exception e) {
127 log.log(Level.SEVERE, "Chyba appletu:", e);
130 /** Nastavení plátna */
131 panel.addMouseMotionListener(ObrazkovyApplet.this);
132 panel.addMouseListener(ObrazkovyApplet.this);
133 log.log(Level.INFO, "plátno nastaveno");
136 @SuppressWarnings("unchecked")
137 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
138 private void initComponents() {
140 odeslat = new javax.swing.JButton();
141 stavovyRadek = new javax.swing.JLabel();
142 panel = new javax.swing.JPanel();
144 odeslat.setText("Odeslat na server");
145 odeslat.addActionListener(new java.awt.event.ActionListener() {
146 public void actionPerformed(java.awt.event.ActionEvent evt) {
147 odeslatActionPerformed(evt);
151 stavovyRadek.setText(" ");
153 javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
154 panel.setLayout(panelLayout);
155 panelLayout.setHorizontalGroup(
156 panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
157 .addGap(0, 388, Short.MAX_VALUE)
159 panelLayout.setVerticalGroup(
160 panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
161 .addGap(0, 255, Short.MAX_VALUE)
164 javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
165 getContentPane().setLayout(layout);
166 layout.setHorizontalGroup(
167 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
168 .addGroup(layout.createSequentialGroup()
170 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
171 .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
172 .addGroup(layout.createSequentialGroup()
173 .addComponent(stavovyRadek, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
174 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
175 .addComponent(odeslat)))
178 layout.setVerticalGroup(
179 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
180 .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
182 .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
183 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
184 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
185 .addComponent(odeslat)
186 .addComponent(stavovyRadek))
189 }// </editor-fold>//GEN-END:initComponents
191 private void odeslatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_odeslatActionPerformed
193 }//GEN-LAST:event_odeslatActionPerformed
195 private void vypisStav(String text) {
196 stavovyRadek.setText(text);
198 // Variables declaration - do not modify//GEN-BEGIN:variables
199 private javax.swing.JButton odeslat;
200 private javax.swing.JPanel panel;
201 private javax.swing.JLabel stavovyRadek;
202 // End of variables declaration//GEN-END:variables