java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java
changeset 16 131d2652b241
parent 15 b166c6f49719
     1.1 --- a/java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java	Mon Mar 14 21:54:03 2011 +0100
     1.2 +++ b/java/ObrazkovyApplet/src/cz/frantovo/obrazkovyApplet/ObrazkovyApplet.java	Tue Apr 24 21:02:52 2012 +0200
     1.3 @@ -1,12 +1,11 @@
     1.4  package cz.frantovo.obrazkovyApplet;
     1.5  
     1.6 +import java.awt.BorderLayout;
     1.7  import java.awt.Color;
     1.8  import java.awt.Graphics;
     1.9 -import java.awt.Graphics2D;
    1.10  import java.awt.event.MouseEvent;
    1.11  import java.awt.event.MouseListener;
    1.12  import java.awt.event.MouseMotionListener;
    1.13 -import java.awt.image.BufferedImage;
    1.14  import java.io.BufferedReader;
    1.15  import java.io.InputStreamReader;
    1.16  import java.io.OutputStream;
    1.17 @@ -18,6 +17,9 @@
    1.18  import javax.swing.UIManager;
    1.19  import javax.swing.UIManager.LookAndFeelInfo;
    1.20  
    1.21 +/**
    1.22 + * @author František Kučera, podnik.frantovo.cz
    1.23 + */
    1.24  public class ObrazkovyApplet extends javax.swing.JApplet implements MouseMotionListener, MouseListener {
    1.25  
    1.26  	private static final Logger log = Logger.getLogger(ObrazkovyApplet.class.getSimpleName());
    1.27 @@ -25,9 +27,7 @@
    1.28  	private int stareX;
    1.29  	private int stareY;
    1.30  	private boolean kresli;
    1.31 -	private Graphics g;
    1.32 -	private Graphics2D vystupniGrafika;
    1.33 -	private BufferedImage obrazek;
    1.34 +	private Platno platno;
    1.35  
    1.36  	private void odesliData() {
    1.37  		try {
    1.38 @@ -36,7 +36,7 @@
    1.39  			spojeni.setDoOutput(true);
    1.40  			spojeni.connect();
    1.41  			OutputStream os = spojeni.getOutputStream();
    1.42 -			ImageIO.write(obrazek, "png", os);
    1.43 +			ImageIO.write(platno.getObrazek(), "png", os);
    1.44  			os.flush();
    1.45  			os.close();
    1.46  
    1.47 @@ -52,8 +52,8 @@
    1.48  	}
    1.49  
    1.50  	private void smazGrafiku() {
    1.51 -		getVystupniGrafika().clearRect(0, 0, platno.getWidth(), platno.getHeight());
    1.52 -		g.clearRect(0, 0, platno.getWidth(), platno.getHeight());
    1.53 +		platno.getObrazek().getGraphics().fillRect(0, 0, panel.getWidth(), panel.getHeight());
    1.54 +		platno.repaint();
    1.55  	}
    1.56  
    1.57  	public void mouseDragged(MouseEvent e) {
    1.58 @@ -62,14 +62,12 @@
    1.59  
    1.60  		if (kresli) {
    1.61  			vypisStav("kreslím: " + stareX + "x" + stareY + "→" + x + "x" + y);
    1.62 -			g = platno.getGraphics();
    1.63 +
    1.64 +			Graphics g = platno.getObrazek().getGraphics();
    1.65  			g.setColor(Color.BLUE);
    1.66  			g.drawLine(stareX, stareY, x, y);
    1.67  
    1.68 -			platno.pa
    1.69 -
    1.70 -			
    1.71 -			getVystupniGrafika().drawLine(stareX, stareY, x, y);
    1.72 +			platno.repaint();
    1.73  		}
    1.74  
    1.75  		stareX = x;
    1.76 @@ -99,19 +97,6 @@
    1.77  		kresli = false;
    1.78  	}
    1.79  
    1.80 -	private Graphics2D getVystupniGrafika() {
    1.81 -		if (obrazek == null) {
    1.82 -			obrazek = new BufferedImage(platno.getWidth(), platno.getHeight(), BufferedImage.TYPE_INT_RGB);
    1.83 -		}
    1.84 -		if (vystupniGrafika == null) {
    1.85 -			vystupniGrafika = obrazek.createGraphics();
    1.86 -			vystupniGrafika.setBackground(Color.WHITE);
    1.87 -			vystupniGrafika.setColor(Color.BLUE);
    1.88 -			vystupniGrafika.clearRect(0, 0, platno.getWidth(), platno.getHeight());
    1.89 -		}
    1.90 -		return vystupniGrafika;
    1.91 -	}
    1.92 -
    1.93  	@Override
    1.94  	public void init() {
    1.95  
    1.96 @@ -131,7 +116,11 @@
    1.97  			java.awt.EventQueue.invokeAndWait(new Runnable() {
    1.98  
    1.99  				public void run() {
   1.100 +					platno = new Platno();
   1.101 +					platno.repaint();
   1.102  					initComponents();
   1.103 +					panel.setLayout(new BorderLayout());
   1.104 +					panel.add(platno, BorderLayout.CENTER);
   1.105  				}
   1.106  			});
   1.107  		} catch (Exception e) {
   1.108 @@ -139,8 +128,8 @@
   1.109  		}
   1.110  
   1.111  		/** Nastavení plátna */
   1.112 -		platno.addMouseMotionListener(ObrazkovyApplet.this);
   1.113 -		platno.addMouseListener(ObrazkovyApplet.this);
   1.114 +		panel.addMouseMotionListener(ObrazkovyApplet.this);
   1.115 +		panel.addMouseListener(ObrazkovyApplet.this);
   1.116  		log.log(Level.INFO, "plátno nastaveno");
   1.117  	}
   1.118  
   1.119 @@ -150,7 +139,7 @@
   1.120  
   1.121          odeslat = new javax.swing.JButton();
   1.122          stavovyRadek = new javax.swing.JLabel();
   1.123 -        platno = new javax.swing.JPanel();
   1.124 +        panel = new javax.swing.JPanel();
   1.125  
   1.126          odeslat.setText("Odeslat na server");
   1.127          odeslat.addActionListener(new java.awt.event.ActionListener() {
   1.128 @@ -161,16 +150,14 @@
   1.129  
   1.130          stavovyRadek.setText(" ");
   1.131  
   1.132 -        platno.setDoubleBuffered(true);
   1.133 -
   1.134 -        javax.swing.GroupLayout platnoLayout = new javax.swing.GroupLayout(platno);
   1.135 -        platno.setLayout(platnoLayout);
   1.136 -        platnoLayout.setHorizontalGroup(
   1.137 -            platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.138 +        javax.swing.GroupLayout panelLayout = new javax.swing.GroupLayout(panel);
   1.139 +        panel.setLayout(panelLayout);
   1.140 +        panelLayout.setHorizontalGroup(
   1.141 +            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.142              .addGap(0, 388, Short.MAX_VALUE)
   1.143          );
   1.144 -        platnoLayout.setVerticalGroup(
   1.145 -            platnoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.146 +        panelLayout.setVerticalGroup(
   1.147 +            panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.148              .addGap(0, 255, Short.MAX_VALUE)
   1.149          );
   1.150  
   1.151 @@ -181,7 +168,7 @@
   1.152              .addGroup(layout.createSequentialGroup()
   1.153                  .addContainerGap()
   1.154                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.155 -                    .addComponent(platno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   1.156 +                    .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   1.157                      .addGroup(layout.createSequentialGroup()
   1.158                          .addComponent(stavovyRadek, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
   1.159                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   1.160 @@ -192,7 +179,7 @@
   1.161              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
   1.162              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
   1.163                  .addContainerGap()
   1.164 -                .addComponent(platno, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   1.165 +                .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
   1.166                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
   1.167                  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
   1.168                      .addComponent(odeslat)
   1.169 @@ -210,7 +197,7 @@
   1.170  	}
   1.171      // Variables declaration - do not modify//GEN-BEGIN:variables
   1.172      private javax.swing.JButton odeslat;
   1.173 -    private javax.swing.JPanel platno;
   1.174 +    private javax.swing.JPanel panel;
   1.175      private javax.swing.JLabel stavovyRadek;
   1.176      // End of variables declaration//GEN-END:variables
   1.177  }