Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Toolkit;
- import javax.swing.ImageIcon;
- import javax.swing.JPanel;
- public class Board4 extends JPanel implements Runnable {
- private Image star;
- private Thread animate;
- private int x, y;
- private final int Delay = 50;
- public Board4() {
- setBackground(Color.black);
- setDoubleBuffered(true);
- ImageIcon image = new ImageIcon(this.getClass().getResource("star.png"));
- star = image.getImage();
- x = y = 10;
- }
- public void addNotifty(){
- super.addNotify();
- animate = new Thread(this);
- animate.start();
- }
- public void paint(Graphics2D g){
- super.paint(g);
- Graphics2D g2d = (Graphics2D)g;
- g2d.drawImage(star, x, y, this);
- Toolkit.getDefaultToolkit().sync();
- g.dispose();
- }
- public void cycle(){
- x += 1;
- y += 1;
- if(y > 240){
- x = -45;
- y = -45;
- }
- }
- public void run(){
- long beforeTime, timeDiff, sleep;
- beforeTime = System.currentTimeMillis();
- while(true){
- cycle();
- repaint();
- timeDiff = System.currentTimeMillis() - beforeTime;
- sleep = Delay - timeDiff;
- if (sleep < 0)
- sleep = 2;
- try
- {
- Thread.sleep(sleep);
- }
- catch (InterruptedException e)
- {
- System.out.println("interrupted");
- }
- beforeTime = System.currentTimeMillis();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement