Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.util.Random;
- @SuppressWarnings("serial")
- public class Practice extends Frame implements Runnable {
- int frameNumber;
- int delay;
- Thread animatorThread;
- static int x1=128, y1=128, x2=384, y2=128, x3=256, y3=256;
- //Flicker removal
- Image scrnBuf;
- Graphics scrnG;
- public void init() {
- scrnBuf = createImage(1024, 768);
- scrnG = scrnBuf.getGraphics();
- }
- Practice(int fps, String windowTitle) {
- super(windowTitle);
- delay = (fps > 0) ? (1000 / fps) : 100;
- }
- public void startAnimation() {
- if (animatorThread == null) {
- animatorThread = new Thread(this);
- animatorThread.start();
- }
- }
- public void stopAnimation() {
- animatorThread = null;
- }
- public boolean mouseDown(Event e, int x, int y) {
- if (animatorThread == null) {
- startAnimation();
- }
- else {
- stopAnimation();
- }
- this.dispose();
- this.setVisible(false);
- return false;
- }
- public void run() {
- long startTime = System.currentTimeMillis();
- Random moves = new Random();
- int r1=0, r2=0, r3=0, r4=0, r5=0, r6=0;
- while (Thread.currentThread() == animatorThread) {
- repaint();
- try {
- startTime += delay;
- Thread.sleep(Math.max(0,
- startTime-System.currentTimeMillis()));
- } catch (InterruptedException e) {
- break;
- }
- frameNumber++;
- // Start of application logic
- //Code for x1
- if(r1 == 0) {
- r1 = moves.nextInt(100);
- if (r1>49) {
- r1 = -r1 + 50;
- }
- }
- if(r1 != 0) {
- if(r1>0) {
- x1++;
- r1--;
- } else if(r1<0) {
- x1--;
- r1++;
- }
- }
- if(x1==0) {
- r1 = 50;
- } else if(x1==512) {
- r1 = -50;
- }
- //Code for x2
- if(r2 == 0) {
- r2 = moves.nextInt(100);
- if (r2>49) {
- r2 = -r2 + 50;
- }
- }
- if(r2 != 0) {
- if(r2>0) {
- x2++;
- r2--;
- } else if(r2<0) {
- x2--;
- r2++;
- }
- }
- if(x2==0) {
- r2 = 50;
- } else if(x2==512) {
- r2 = -50;
- }
- //Code for x3
- if(r3 == 0) {
- r3 = moves.nextInt(100);
- if (r3>49) {
- r3 = -r3 + 50;
- }
- }
- if(r3 != 0) {
- if(r3>0) {
- x3++;
- r3--;
- } else if(r3<0) {
- x3--;
- r3++;
- }
- }
- if(x3==0) {
- r3 = 50;
- } else if(x3==512) {
- r3 = -50;
- }
- //Code for y1
- if(r4 == 0) {
- r4 = moves.nextInt(100);
- if (r4>49) {
- r4 = -r4 + 50;
- }
- }
- if(r4 != 0) {
- if(r4>0) {
- y1++;
- r4--;
- } else if(r4<0) {
- y1--;
- r4++;
- }
- }
- if(y1==0) {
- r4 = 50;
- } else if(y1==384) {
- r4 = -50;
- }
- //Code for y2
- if(r5 == 0) {
- r5 = moves.nextInt(100);
- if (r5>49) {
- r5 = -r5 + 50;
- }
- }
- if(r5 != 0) {
- if(r5>0) {
- y2++;
- r5--;
- } else if(r5<0) {
- y2--;
- r5++;
- }
- }
- if(y2==0) {
- r5 = 50;
- } else if(y2==384) {
- r5 = -50;
- }
- //Code for y3
- if(r6 == 0) {
- r6 = moves.nextInt(100);
- if (r6>49) {
- r6 = -r6 + 50;
- }
- }
- if(r6 != 0) {
- if(r6>0) {
- y3++;
- r6--;
- } else if(r6<0) {
- y3--;
- r6++;
- }
- }
- if(y3==0) {
- r6 = 50;
- } else if(y3==384) {
- r6 = -50;
- }
- }
- }
- public void update(Graphics g) {
- paint(g);
- }
- public void paint(Graphics g) {
- scrnG.setColor(Color.white);
- scrnG.fillRect(0, 0, 1024, 768);
- scrnG.setColor(Color.red);
- scrnG.drawLine(512, 0, 512, 768);
- scrnG.drawLine(0, 384, 1024, 384);
- //Original, Quadrant 2
- scrnG.drawLine(x1, y1, x2, y2);
- scrnG.drawLine(x2, y2, x3, y3);
- scrnG.drawLine(x3, y3, x1, y1);
- //Quadrant 1
- scrnG.drawLine(x1+(1024-x1*2), y1, x2+(1024-x2*2), y2);
- scrnG.drawLine(x2+(1024-x2*2), y2, x3+(1024-x3*2), y3);
- scrnG.drawLine(x3+(1024-x3*2), y3, x1+(1024-x1*2), y1);
- //Quadrant 3
- scrnG.drawLine(x1, y1+(768-y1*2), x2, y2+(768-y2*2));
- scrnG.drawLine(x2, y2+(768-y2*2), x3, y3+(768-y3*2));
- scrnG.drawLine(x3, y3+(768-y3*2), x1, y1+(768-y1*2));
- //Quadrant 4
- scrnG.drawLine(x1+(1024-x1*2), y1+(768-y1*2), x2+(1024-x2*2), y2+(768-y2*2));
- scrnG.drawLine(x2+(1024-x2*2), y2+(768-y2*2), x3+(1024-x3*2), y3+(768-y3*2));
- scrnG.drawLine(x3+(1024-x3*2), y3+(768-y3*2), x1+(1024-x1*2), y1+(768-y1*2));
- g.drawImage(scrnBuf, 0, 0, this);
- }
- @SuppressWarnings("deprecation")
- public static void main(String args[]) {
- Practice animator = null;
- int fps = 20;
- // Get frames per second from the command line argument
- if (args.length > 0) {
- fps = Integer.parseInt(args[0]);
- }
- animator = new Practice(fps, "Animator");
- animator.startAnimation();
- animator.resize(200, 60);
- animator.show();
- animator.setSize(1024, 768);
- animator.setTitle("Triangles 1.1 by Franz Sarmiento");
- animator.setResizable(false);
- animator.setLocationRelativeTo(null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement