Jump to content

[Java] Simple Animation


Quadro
 Share

Recommended Posts

O fereastra in care apar cercuri generate de program, cu marime, pozitie si culoare aleatorie, iar raza fiecarui cerc crescand cu 1pixel/50milisecunde.

 

 

Download: http://www.mediafire...q0nuf4vlvo2o15o

Screenshot: anim.png

 

 

Sursa:

**********************************************************************************************

import java.awt.Color;

import java.awt.Graphics;

import java.awt.Image;

import java.util.ArrayList;

import java.util.Random;

 

import javax.swing.JFrame;

 

 

public class Inceput extends JFrame implements Runnable {

 

Graphics backG;

Image backImage;

Thread thread = new Thread(this);

class Cerc{

int x=100, y=100, radius=300;

Random r = new Random();

Color color = new Color(r.nextInt(250), r.nextInt(250), r.nextInt(250));

ArrayList<Cerc> listaCercuri = new ArrayList<Cerc>();

 

 

 

public void createCerc(){

if(listaCercuri.size() < 100)

listaCercuri.add(new Cerc());

else

listaCercuri.clear(); }

 

public Cerc(){

 

 

x=r.nextInt(800);

y=r.nextInt(600);

radius= (20+ r.nextInt(50));

}

}

Cerc cerc = new Cerc();

 

 

 

 

 

public static void main(String[] args){

Inceput t = new Inceput();

t.init();

}

 

 

 

public void init(){

setTitle("Animation");

setSize(800, 600);

setLocationRelativeTo(null);

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setVisible(true);

thread.start();

 

 

}

 

public void update(Graphics g){

if(backImage == null){

backImage = createImage(800, 600);

backG = backImage.getGraphics();

}

for(int i=0; i < cerc.listaCercuri.size(); i++ ){

backG.fillOval(cerc.listaCercuri.get(i).x - cerc.listaCercuri.get(i).radius, cerc.listaCercuri.get(i).y - cerc.listaCercuri.get(i).radius, 2*cerc.listaCercuri.get(i).radius, 2*cerc.listaCercuri.get(i).radius);

backG.setColor(cerc.listaCercuri.get(i).color);

cerc.listaCercuri.get(i).radius++;

}

g.drawImage(backImage, 0, 0, this);

 

}

 

public void paint(Graphics g){

 

update(g);

 

}

 

public void run(){

while(true)

{ repaint();

cerc.createCerc();

 

 

try{

Thread.sleep(50);

}catch(Exception e){

e.printStackTrace();

}

}

}

 

}

**********************************************************************************************

Edited by eB Teodor
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.