Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. import javax.swing.SwingUtilities;
  2. import javax.swing.JFrame;
  3. import javax.swing.JPanel;
  4. import javax.swing.BorderFactory;
  5. import java.awt.Color;
  6. import java.awt.Dimension;
  7. import java.awt.Graphics;
  8. import java.awt.Image;
  9. import java.awt.MediaTracker;
  10. import java.awt.Toolkit;
  11.  
  12. public class SwingPaintDemo {
  13.    
  14.     public static void main(String[] args) {
  15.         SwingUtilities.invokeLater(new Runnable() {
  16.             public void run() {
  17.                 createAndShowGUI();
  18.             }
  19.         });
  20.     }
  21.    
  22.     private static void createAndShowGUI() {
  23.         System.out.println("Created GUI on EDT? "+
  24.         SwingUtilities.isEventDispatchThread());
  25.        
  26.         JFrame f = new JFrame("Swing Paint Demo");
  27.         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28.         f.add(new MyPanel());
  29.         f.pack();
  30.         f.setVisible(true);
  31.     }
  32. }
  33.  
  34.  
  35. class MyPanel extends JPanel {
  36.  
  37.     Image  []  img;
  38.     MediaTracker tracker;
  39.     int     index = 0;
  40.     int     maxImg;
  41.    
  42.     private void loadImages() {
  43.         img = new Image[2];  // 2 images in animation
  44.         tracker = new MediaTracker(this);
  45.         System.out.println("Loading images...");
  46.         maxImg = img.length - 1;
  47.        
  48.         try {
  49.             // images loading
  50.             img[0] = Toolkit.getDefaultToolkit().getImage("src/ill/frame0.gif");
  51.             img[1] = Toolkit.getDefaultToolkit().getImage("src/ill/frame1.gif");
  52.            
  53.             tracker.addImage(img[0],0);
  54.             tracker.addImage(img[1],1);
  55.             tracker.waitForAll();
  56.         }
  57.         catch (Exception e) {
  58.             e.printStackTrace();
  59.         }
  60.        
  61.         AnimationThread at = new AnimationThread();
  62.         at.delayedAnimation(this, 20);
  63.         at.start();
  64.        
  65.         System.out.println("Started");
  66.     }
  67.    
  68.     public MyPanel() {
  69.         setBorder(BorderFactory.createLineBorder(Color.black));
  70.         loadImages();
  71.     }
  72.  
  73.     public Dimension getPreferredSize() {
  74.         return new Dimension(800,800);
  75.     }
  76.    
  77.     public void paintComponent(Graphics g) {
  78.         super.paintComponent(g);      
  79.  
  80.         // Draw Text
  81.         g.drawString("This is my custom Panel!",10,20);
  82.        
  83.         if (img[0] != null) {
  84.             g.drawImage(img[index],0,20,this);
  85.             index = (index < maxImg) ? index + 1 : 0;
  86.         }
  87.     }
  88.    
  89.     public void animate() {
  90.         repaint();
  91.     }
  92.    
  93.     class AnimationThread extends Thread {
  94.         MyPanel animationApplet;
  95.         int delay;
  96.        
  97.         public void delayedAnimation(MyPanel a, int delay) {
  98.             this.animationApplet = a;
  99.             this.delay = delay;
  100.         }
  101.  
  102.         public void run() {
  103.             while (true) {
  104.                 try {
  105.                     sleep(delay);
  106.                     animationApplet.animate();
  107.                 }
  108.                 catch (Exception e) {
  109.                     e.printStackTrace();
  110.                 }
  111.             }
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement