woensdag 28 december 2011

Rotating Line Example



I was preparing a swinning ball example that I once made in a one level remake of the adams family. But I had not worked with cos and sin yet. In Java the commands work with radians in stead of degrees so I needed to take that into account to. I looked up cos and sin and toTadians and quickly made this applet example. A rotating line.

 


import java.awt.*;
import java.applet.*;

public class RotatingLineExample extends Applet implements Runnable{

 Graphics bufferGraphics;
    Image offscreen;
 double x1;
 double y1;
 double x2;
 double y2;
 int angle;

 public void init() {
        setBackground(Color.black);
        offscreen = createImage(getSize().width,getSize().height);
        bufferGraphics = offscreen.getGraphics();
  x1 = getSize().width / 2;
  y1 = getSize().height / 2;
  new Thread(this).start();

 }

 public void rotateline(){
  x2 = Math.cos(Math.toRadians(angle)) * 100 + x1;
  y2 = Math.sin(Math.toRadians(angle)) * 100 + y1;
  angle++;
  if ( angle > 360 ) angle = 0;
 }

    public void run() {
        for(;;) { // animation loop never ends
   rotateline();
         repaint();
         try {
             Thread.sleep(10);
             }
             catch (InterruptedException e) {
             }
     }
    }

     public void update(Graphics g){
     bufferGraphics.clearRect(0,0,getSize().width,getSize().height);
     bufferGraphics.setColor(Color.red);
        bufferGraphics.drawString("Rotating Line Example.",10,10);
  bufferGraphics.drawLine((int)x1,(int)y1,(int)x2,(int)y2);
        g.drawImage(offscreen,0,0,this);
     }

}

Geen opmerkingen:

Een reactie posten

Opmerking: Alleen leden van deze blog kunnen een reactie posten.