001    /**
002    * @author Václav Mikolášek
003    * nicklaus@students.zcu.cz
004    */
005    
006    package animace.label;
007    
008    import animace.*;
009    import javax.swing.*;
010    import java.awt.*;
011    import java.awt.image.*;
012    import java.awt.event.*;
013    
014    /**
015     * TestLabel ukazuje použití připravených tříd PictureLabel a MoveablePicture
016     * Všiměte si, jak se v metodě initPictures() registruje posluchač udalostí, 
017     * která reprezentuje kliknutí na MoveablePicture. Je to stejný postup jako
018     * u standardních AWT.Component.
019     */
020    public class TestLabel {
021            private static JFrame frame;
022            private static PictureLabel scr;
023            private static MoveablePicture[] mPictures = new MoveablePicture[0];
024            
025            /**
026             * Rozeběhne test
027             */
028            public static void main(String[] args) {
029                    if (args.length > 0) {
030                            try {
031                                    mPictures = new MoveablePicture[Integer.valueOf(args[0]).intValue()];
032                            }
033                            catch (NumberFormatException e) {
034                                    System.out.println("Parametrem je pocet obrazku");
035                                    System.exit(1);
036                            }
037                    }
038                    initFrame();
039                    initPictures();
040                    frame.setVisible(true);
041            }
042            
043            /**
044             * Inicializační metoda pro okno a obrázky. Obrázek na pozadí se tahá z ImageSource
045             */
046            private static void initFrame() {
047                    frame = new JFrame("Test PictureLabelu - vlastni obrazky");
048                    
049                    JTextArea napoveda = new JTextArea();
050                    napoveda.setFont(new Font("Arial Bold", Font.PLAIN,12));
051                    napoveda.setBorder( new javax.swing.plaf.BorderUIResource.BevelBorderUIResource(1));
052                    napoveda.setEditable(false);
053                    napoveda.setText(" Nápověda:\n"+
054                                     " Stisknutím kláves F1 az F12 uvedete do pohybu nebo zastavíte\n" +
055                                     " obrázky s odpovídajícím pořadovým číslem. Kliknutím na konkrétní\n"+
056                                     " obrázek zobrazíte okénko s údaji o jeho číslu, FPS,\n"+
057                                     " celkovém počtu snímku a počtu posunů");
058                    
059                    /// Obrazek na pozadi :
060                    BufferedImage bg = ImageSource.square1(Color.red.darker(),Color.red.darker().darker());
061                    
062                    /// PictureLabel :
063                    scr = new PictureLabel(bg);
064                    
065                    ///nastaveni okna
066                    Container c = frame.getContentPane();  
067                    c.setLayout(new BorderLayout());
068                    c.add(scr,BorderLayout.CENTER);
069                    c.add(napoveda,BorderLayout.SOUTH);
070                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
071                    frame.setSize(400,450);
072                    scr.setPreferredSize(new Dimension(400,400));
073            }
074            
075            /**
076             * Vytvoří instance třídy MoveablePicture, zaregistruje jim posluchače na kliknutí
077             * a dá je na PictureLabel.
078             */
079            private static void initPictures() {
080                    for (int i = 0; i < mPictures.length; i++) {
081                            mPictures[i] = new MoveablePicture(scr,ImageSource.javaLogo());
082                            mPictures[i].setPaintControl(new PaintControl());
083                            ///
084                            /// ZDE SI ZAREGISTRUJEME POSLUCHACE :
085                            ///
086                            mPictures[i].addActionListener(new MPictureActionListener("F"+(i+1)));
087                            
088                            /// dame MoveablePicture na PicturePanel
089                            scr.addMoveablePicture(mPictures[i]);
090                            scr.getInputMap(scr.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F1 + i,0),"F"+(i+1));
091                            scr.getActionMap().put("F"+(i+1),new MoveablePictureKeyAction(mPictures[i]));
092                    }
093                    scr.doMPLayout();
094            }
095            
096            /**
097             * AbstractAction starající se o rozběhnutí požadovaného obrázku
098             * po stisku klávesy F1 az F12, podle počtu obrázku.
099             */
100            public static class MoveablePictureKeyAction extends AbstractAction {
101                    private MoveablePicture mp = null;
102                    private Driver driver = null;
103                    
104                    /**
105                     * Konstruktor vyžaduje objekt typu MoveablePicture
106                     * pro který bude vytvářet vlákno Driver
107                     */
108                    public MoveablePictureKeyAction(MoveablePicture mp) {
109                            this.mp = mp;
110                    }
111                    
112                    /**
113                     * Po stisku klávesy F(X) se provede test, zda je objekt již v pohybu,
114                     * potom je pozastaven, v opačném případě se inicializuje vlákno Driver
115                     * pro tento objekt a je spuštěno.
116                     */
117                    public void actionPerformed(ActionEvent e) {
118                            if (driver != null && driver.goes()) {
119                                    driver.stopDriving();
120                            }
121                            else {
122                                    PictureLabel c = (PictureLabel) e.getSource();
123                                    driver = new Driver(mp,0,0,c.getWidth() - mp.getWidth(),c.getHeight()-mp.getHeight());
124                                    driver.setPaintControl(((PaintControled) mp).getPaintControl());
125                                    driver.start();
126                            }
127                    }
128            }
129            
130            /**
131             * MPictureActionListener způsobí, že po kliknutí na MoveablePicture
132             * bude otevřeno okno s některými informacemi o objektu.
133             */
134            public static class MPictureActionListener implements ActionListener {
135                    private JFrame infoFrame = null;
136                    private JLabel nameVal;
137                    private JLabel name             = new JLabel(" Tlacitko:",JLabel.LEFT);;
138                    private JLabel fps              = new JLabel(" FPS:",JLabel.LEFT);
139                    private JLabel frames           = new JLabel(" Snimku:",JLabel.LEFT);
140                    private JLabel movesCount       = new JLabel(" Posun o:",JLabel.LEFT);
141                    private JLabel movesCountNum    = new JLabel("",JLabel.LEFT);
142                    private JLabel fpsNum           = new JLabel("",JLabel.LEFT);
143                    private JLabel framesNum        = new JLabel("",JLabel.LEFT);
144                            
145                                    
146                            
147                    public MPictureActionListener(String name) {
148                            this.nameVal = new JLabel(" " + name, JLabel.LEFT);
149                            initInfoFrame();
150                    }
151                    
152                    /// Nastavi podobu okna
153                    private void initInfoFrame() {
154                            infoFrame = new JFrame();
155                            Container c = infoFrame.getContentPane();
156                            c.setLayout(new GridLayout(0,2));
157                            c.add(name);
158                            c.add(nameVal);
159                            c.add(fps);
160                            c.add(fpsNum);
161                            c.add(frames);
162                            c.add(framesNum);
163                            c.add(movesCount);
164                            c.add(movesCountNum);
165                            
166                            c.setBackground(Color.gray.brighter());
167                    }
168                    
169                    /**
170                     * Když uživatel klikne na MoveablePicture, provede se tato obsluha, která
171                     * otevře okno s některými informacemi o objektu. Jméno, FPS, Počet snímků a počet
172                     * vykonaných posunů.
173                     */
174                    public void actionPerformed(ActionEvent e) 
175                    {
176                            
177                            MoveablePicture mp = (MoveablePicture) e.getSource();
178                            
179                            fpsNum.setText(" " + Math.round(mp.getPaintControl().getAverageFps()));
180                            framesNum.setText(" " + mp.getPaintControl().framesCount);
181                            movesCountNum.setText(" " + mp.getPaintControl().movesCount);
182                            infoFrame.pack();
183                            if (infoFrame.isVisible()) {
184                                    infoFrame.validate();  
185                            }
186                            else {
187                                    infoFrame.setVisible(true);
188                            }
189                    }
190            }
191    }