Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package animacja;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.util.Timer;
- import java.util.TimerTask;
- import javax.swing.SwingUtilities;
- /**
- *
- * @author Krzysztof Fyrla
- */
- public class Glowna extends javax.swing.JFrame {
- private static final int LICZBA_KLATEK = 24;
- private static int interwal_animacji =
- (int)(1000/LICZBA_KLATEK);
- private Image bufor;
- private double t; //czas [ms]
- private int s_0; //punkt startowy [pixel]
- private double v_0; //predkosc [pixel/s]
- private Boolean doprzodu;
- public Glowna() {
- initComponents();
- t=0; s_0 = 50; v_0 = 50;
- doprzodu = true;
- }
- public void paint(Graphics g) {
- bufor = new BufferedImage(getWidth(), getHeight(),
- BufferedImage.TYPE_INT_RGB);
- Graphics dc = bufor.getGraphics();
- dc.setColor(Color.BLUE);
- //int pozycja_obiektu = s_0+(int)( v_0*(t/1000) );
- int pozycja_obiektu = s_0+(int)( v_0*(t/1000) );
- dc.fillOval(pozycja_obiektu, 200, 30, 30);
- g.drawImage(bufor, 0, 0, this);
- }
- public void go() {
- TimerTask naszTimer = new TimerTask()
- {
- public void run() {
- if (s_0 + t / 1000 * v_0 >= 400 || s_0 + t / 1000 * v_0 <= s_0) {
- doprzodu = !doprzodu;
- }
- if (doprzodu) {
- t+=interwal_animacji;
- }
- else {
- t-=interwal_animacji;
- }
- SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- repaint();
- } //koniec funkcji run() ze SwingUtilities
- });
- }
- };
- //deklaracja obiektu naszTimer
- Timer timer = new Timer();
- timer.schedule(naszTimer, 0, interwal_animacji);
- }
- public static void main(String args[]) {
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- Glowna frame = new Glowna();
- frame.setSize(500, 500);
- frame.setResizable(false);
- frame.setVisible(true);
- frame.go();
- }
- });
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 400, Short.MAX_VALUE)
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGap(0, 300, Short.MAX_VALUE)
- );
- pack();
- }// </editor-fold>
- /**
- * @param args the command line arguments
- */
- // Variables declaration - do not modify
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement