Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package animacja;
  7.  
  8. import java.awt.Color;
  9. import java.awt.Graphics;
  10. import java.awt.Image;
  11. import java.awt.image.BufferedImage;
  12. import java.util.Timer;
  13. import java.util.TimerTask;
  14. import javax.swing.SwingUtilities;
  15.  
  16. /**
  17.  *
  18.  * @author Krzysztof Fyrla
  19.  */
  20. public class Glowna extends javax.swing.JFrame {
  21.  
  22.     private static final int LICZBA_KLATEK = 24;
  23.     private static int interwal_animacji =
  24.             (int)(1000/LICZBA_KLATEK);
  25.     private Image bufor;
  26.     private double t; //czas [ms]
  27.     private int s_0; //punkt startowy [pixel]
  28.     private double v_0; //predkosc [pixel/s]
  29.    
  30.     public Glowna() {
  31.         initComponents();
  32.        
  33.         t=0;  s_0 = 50; v_0 = 50;
  34.     }
  35.    
  36.     public void paint(Graphics g) {
  37.         bufor = new BufferedImage(getWidth(), getHeight(),
  38.                         BufferedImage.TYPE_INT_RGB);
  39.         Graphics dc = bufor.getGraphics();
  40.         dc.setColor(Color.BLUE);
  41.         //int pozycja_obiektu = s_0+(int)( v_0*(t/1000) );
  42.         int pozycja_obiektu = s_0+(int)( v_0*(t/1000) );
  43.         dc.fillOval(pozycja_obiektu, 200, 30, 30);
  44.         g.drawImage(bufor, 0, 0, this);
  45.     }
  46.    
  47.     public void go() {
  48.         TimerTask naszTimer = new TimerTask()
  49.         {
  50.             public void run() {
  51.                 if (s_0 + t / 1000 * v_0 < 400) {
  52.                     t+=interwal_animacji;
  53.                 }
  54.                 else {
  55.                     t-=interwal_animacji;
  56.                 }
  57.                 SwingUtilities.invokeLater(new Runnable() {
  58.                     public void run() {
  59.                         repaint();
  60.                     } //koniec funkcji run() ze SwingUtilities
  61.                 });
  62.             }
  63.         };
  64.                 //deklaracja obiektu naszTimer
  65.                 Timer timer = new Timer();
  66.                 timer.schedule(naszTimer, 0, interwal_animacji);
  67.     }
  68.    
  69.     public static void main(String args[]) {
  70.         java.awt.EventQueue.invokeLater(new Runnable() {
  71.             public void run() {
  72.                 Glowna frame = new Glowna();
  73.                 frame.setSize(500, 500);
  74.                 frame.setResizable(false);
  75.                 frame.setVisible(true);
  76.                 frame.go();
  77.             }
  78.         });
  79.     }
  80.  
  81.     /**
  82.      * This method is called from within the constructor to initialize the form.
  83.      * WARNING: Do NOT modify this code. The content of this method is always
  84.      * regenerated by the Form Editor.
  85.      */
  86.     @SuppressWarnings("unchecked")
  87.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  88.     private void initComponents() {
  89.  
  90.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  91.  
  92.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  93.         getContentPane().setLayout(layout);
  94.         layout.setHorizontalGroup(
  95.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  96.             .addGap(0, 400, Short.MAX_VALUE)
  97.         );
  98.         layout.setVerticalGroup(
  99.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  100.             .addGap(0, 300, Short.MAX_VALUE)
  101.         );
  102.  
  103.         pack();
  104.     }// </editor-fold>                        
  105.  
  106.     /**
  107.      * @param args the command line arguments
  108.      */
  109.    
  110.  
  111.     // Variables declaration - do not modify                    
  112.     // End of variables declaration                  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement