ANIMATIONS
MOVE OF TIRES
/*
* 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 web;
/**
*
* @author Pantazis
*/
/*
* 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.
*/
/*
* 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.
*/
/**
*
* @author dimitris
*/
import java.applet.*;
import java.awt.*;
import java.awt.Color;
public class Tires extends Applet implements Runnable {
int m=5;
int n=20;
int width=200, height=200;
int i = 0;
Thread t = null;
boolean threadSuspended;
// Executed when the applet is first created.
public void init() {
System.out.println("init(): begin");
//width = getSize().width;
// height = getSize().height;
setBackground( Color.decode("#ffffa0") );
// System.out.println("init(): end");
}
// Executed when the applet is destroyed.
public void destroy() {
System.out.println("destroy()");
}
// Executed after the applet is created; and also whenever
// the browser returns to the page containing the applet.
public void start() {
System.out.println("start(): begin");
if ( t == null ) {
System.out.println("start(): creating thread");
t = new Thread( this );
System.out.println("start(): starting thread");
threadSuspended = false;
t.start();
}
else {
if ( threadSuspended ) {
threadSuspended = false;
System.out.println("start(): notifying thread");
synchronized( this ) {
notify();
}
}
}
System.out.println("start(): end");
}
// Executed whenever the browser leaves the page containing the applet.
public void stop() {
// System.out.println("stop(): begin");
threadSuspended = true;
}
// Executed within the thread that this applet created.
public void run() {
// System.out.println("run(): begin");
try {
while (true) {
// System.out.println("run(): awake");
// Here's where the thread does some work
i=i+2; // this is shorthand for "i = i+1;"
if ( i == 1600 ) {
i = 0;
}
showStatus( "i is " + i );
// Now the thread checks to see if it should suspend itself
if ( threadSuspended ) {
synchronized( this ) {
while ( threadSuspended ) {
System.out.println("run(): waiting");
wait();
}
}
}
// System.out.println("run(): requesting repaint");
repaint();
// System.out.println("run(): sleeping");
t.sleep(49 ); // interval given in milliseconds
}
}
catch (InterruptedException e) { }
//System.out.println("run(): end");
}
// Executed whenever the applet is asked to redraw itself.
public void paint( Graphics g ) {
for (int j = 1; j <= n; j++){
double z=Math.cos(i*Math.PI/180+2*j*Math.PI/n );
double b=100*z;
float s=Math.round(b);
int f=Math.round(s);
double z1=Math.sin(i*Math.PI/180+2*j*Math.PI/n);
double b1=100*z1;
float s1=Math.round(b1);
int f1=Math.round(s1);
double z3=Math.cos(i*Math.PI/180+2*j*Math.PI/n );
double b3=100*z3;
float s3=Math.round(b3);
int f3=Math.round(s3);
double z4=Math.sin(-(i*Math.PI/180+2*j*Math.PI/n));
double b4=100*z4;
float s4=Math.round(b4);
int f4=Math.round(s4);
for (int k = 1; k <= m; k++){
// System.out.println("paint()");
g.setColor(Color.BLUE);
g.drawOval(width+i-100+k, height-100+k, width-2*k, height-2*k);
g.setColor( Color.green );
g.drawLine( width+i, height, f+width+i, f1+height );
g.setColor(Color.BLUE);
g.drawOval(width+i-100+k, height+100+3*m/2+k, width-2*k, height-2*k);
g.setColor( Color.green );
g.drawLine( width+i, height+200+3*m/2, width+f3+i, height+200+3*m/2+f4 );
// g.setColor(Color.BLUE);
// g.drawOval(width+i-100, height-100, width-150, height);
g.setColor(Color.red);
g.drawLine( width-100, height+100+k, width+i, height+100+k );
}
}
}
}