diff -r b166c6f49719 -r c681a3a6cbac java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java --- a/java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java Mon Mar 14 21:54:03 2011 +0100 +++ b/java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java Sat Jun 22 16:00:39 2013 +0200 @@ -1,12 +1,11 @@ package cz.frantovo.obrazkovyApplet; +import java.awt.BorderLayout; 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; @@ -18,6 +17,9 @@ import javax.swing.UIManager; import javax.swing.UIManager.LookAndFeelInfo; +/** + * @author František Kučera, podnik.frantovo.cz + */ public class ObrazkovyApplet extends javax.swing.JApplet implements MouseMotionListener, MouseListener { private static final Logger log = Logger.getLogger(ObrazkovyApplet.class.getSimpleName()); @@ -25,9 +27,7 @@ private int stareX; private int stareY; private boolean kresli; - private Graphics g; - private Graphics2D vystupniGrafika; - private BufferedImage obrazek; + private Platno platno; private void odesliData() { try { @@ -36,7 +36,7 @@ spojeni.setDoOutput(true); spojeni.connect(); OutputStream os = spojeni.getOutputStream(); - ImageIO.write(obrazek, "png", os); + ImageIO.write(platno.getObrazek(), "png", os); os.flush(); os.close(); @@ -52,8 +52,8 @@ } private void smazGrafiku() { - getVystupniGrafika().clearRect(0, 0, platno.getWidth(), platno.getHeight()); - g.clearRect(0, 0, platno.getWidth(), platno.getHeight()); + platno.getObrazek().getGraphics().fillRect(0, 0, panel.getWidth(), panel.getHeight()); + platno.repaint(); } public void mouseDragged(MouseEvent e) { @@ -62,14 +62,12 @@ if (kresli) { vypisStav("kreslím: " + stareX + "x" + stareY + "→" + x + "x" + y); - g = platno.getGraphics(); + + Graphics g = platno.getObrazek().getGraphics(); g.setColor(Color.BLUE); g.drawLine(stareX, stareY, x, y); - platno.pa - - - getVystupniGrafika().drawLine(stareX, stareY, x, y); + platno.repaint(); } stareX = x; @@ -99,19 +97,6 @@ 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() { @@ -131,7 +116,11 @@ java.awt.EventQueue.invokeAndWait(new Runnable() { public void run() { + platno = new Platno(); + platno.repaint(); initComponents(); + panel.setLayout(new BorderLayout()); + panel.add(platno, BorderLayout.CENTER); } }); } catch (Exception e) { @@ -139,8 +128,8 @@ } /** Nastavení plátna */ - platno.addMouseMotionListener(ObrazkovyApplet.this); - platno.addMouseListener(ObrazkovyApplet.this); + panel.addMouseMotionListener(ObrazkovyApplet.this); + panel.addMouseListener(ObrazkovyApplet.this); log.log(Level.INFO, "plátno nastaveno"); } @@ -150,7 +139,7 @@ odeslat = new javax.swing.JButton(); stavovyRadek = new javax.swing.JLabel(); - platno = new javax.swing.JPanel(); + panel = new javax.swing.JPanel(); odeslat.setText("Odeslat na server"); odeslat.addActionListener(new java.awt.event.ActionListener() { @@ -161,16 +150,14 @@ 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) + javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel); + panel.setLayout(panelLayout); + panelLayout.setHorizontalGroup( + panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 388, Short.MAX_VALUE) ); - platnoLayout.setVerticalGroup( - platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + panelLayout.setVerticalGroup( + panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 255, Short.MAX_VALUE) ); @@ -181,7 +168,7 @@ .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) + .addComponent(panel, 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) @@ -192,7 +179,7 @@ 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) + .addComponent(panel, 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) @@ -210,7 +197,7 @@ } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton odeslat; - private javax.swing.JPanel platno; + private javax.swing.JPanel panel; private javax.swing.JLabel stavovyRadek; // End of variables declaration//GEN-END:variables }