Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 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.     private Boolean doprzodu;
  30.    
  31.     public Glowna() {
  32.         initComponents();
  33.        
  34.         t=0;  s_0 = 50; v_0 = 50;
  35.         doprzodu = true;
  36.     }
  37.    
  38.     public void paint(Graphics g) {
  39.         bufor = new BufferedImage(getWidth(), getHeight(),
  40.                         BufferedImage.TYPE_INT_RGB);
  41.         Graphics dc = bufor.getGraphics();
  42.         dc.setColor(Color.BLUE);
  43.         //int pozycja_obiektu = s_0+(int)( v_0*(t/1000) );
  44.         int pozycja_obiektu = s_0+(int)( v_0*(t/1000) );
  45.         dc.fillOval(pozycja_obiektu, 200, 30, 30);
  46.         g.drawImage(bufor, 0, 0, this);
  47.     }
  48.    
  49.     public void go() {
  50.         TimerTask naszTimer = new TimerTask()
  51.         {
  52.             public void run() {
  53.                 if (s_0 + t / 1000 * v_0 >= 400 || s_0 + t / 1000 * v_0 <= s_0) {
  54.                     doprzodu = !doprzodu;
  55.                 }
  56.  
  57.                 if (doprzodu) {
  58.                     t+=interwal_animacji;
  59.                 }
  60.                 else {
  61.                     t-=interwal_animacji;
  62.                 }
  63.                 SwingUtilities.invokeLater(new Runnable() {
  64.                     public void run() {
  65.                         repaint();
  66.                     } //koniec funkcji run() ze SwingUtilities
  67.                 });
  68.             }
  69.         };
  70.                 //deklaracja obiektu naszTimer
  71.                 Timer timer = new Timer();
  72.                 timer.schedule(naszTimer, 0, interwal_animacji);
  73.     }
  74.    
  75.     public static void main(String args[]) {
  76.         java.awt.EventQueue.invokeLater(new Runnable() {
  77.             public void run() {
  78.                 Glowna frame = new Glowna();
  79.                 frame.setSize(500, 500);
  80.                 frame.setResizable(false);
  81.                 frame.setVisible(true);
  82.                 frame.go();
  83.             }
  84.         });
  85.     }
  86.  
  87.     /**
  88.      * This method is called from within the constructor to initialize the form.
  89.      * WARNING: Do NOT modify this code. The content of this method is always
  90.      * regenerated by the Form Editor.
  91.      */
  92.     @SuppressWarnings("unchecked")
  93.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  94.     private void initComponents() {
  95.  
  96.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  97.  
  98.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  99.         getContentPane().setLayout(layout);
  100.         layout.setHorizontalGroup(
  101.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  102.             .addGap(0, 400, Short.MAX_VALUE)
  103.         );
  104.         layout.setVerticalGroup(
  105.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  106.             .addGap(0, 300, Short.MAX_VALUE)
  107.         );
  108.  
  109.         pack();
  110.     }// </editor-fold>                        
  111.  
  112.     /**
  113.      * @param args the command line arguments
  114.      */
  115.    
  116.  
  117.     // Variables declaration - do not modify                    
  118.     // End of variables declaration                  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement