Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.geom.Ellipse2D;
- import java.util.Random;
- public class PanelAnimacji extends JPanel implements Runnable
- {
- Color kolor = Color.red;
- final int rozmiar = 10;
- final int wysokosc = 500;
- final int szerokosc = 500;
- final int czas = 14;
- int x,y;
- int kier_X = 1, kier_Y = 1;
- int ilosc = 1;
- Random random = new Random();
- Thread thread;
- PanelAnimacji()
- {
- thread = new Thread(this);
- thread.start();
- this.x = random.nextInt((szerokosc)+1);
- this.y = random.nextInt((wysokosc)+1);
- }
- @Override
- public void run()
- {
- while(true)
- {
- if(x>szerokosc -(rozmiar +2)){
- kier_X = -1;
- }
- else if(x <= 0){
- kier_X = 1;
- }
- x=x+ ilosc * kier_X;
- y=y+ ilosc * kier_Y;
- repaint();
- try
- {
- Thread.sleep(czas);
- }
- catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- import javax.swing.*;
- import java.awt.*;
- public static class Animacja extends JFrame
- {
- Animacja()
- {
- super("PanelAnimacji-Animacja piłki");
- setBounds(100,100,500,500);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Image imageIconFrame = new ImageIcon(getClass().getClassLoader(ikona.png)).getImage();
- setIconImage(imageIconFrame);
- PanelAnimacji plansza_animacji = new PanelAnimacji();
- setContentPane(plansza_animacji);
- plansza_animacji.setBackground(Color.yellow);
- setVisible(true);
- }
- public static void main(String[] args){
- new PanelAnimacji();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement