Tutorials : Java by Example : Section 7 :
Section Seven Contents
Fun With Letters and Words
Rotating Lines and Polygons
Sorting and Shuffling

Letters - Words - Lines - Shuffle

Rotating Lines and Polygons - Rotating Polygons

Now let's rotate some polygons! This is not quite easy, we will need a few helper functions to do the conversions for us. They can handle any polygon you define and rotate it with the specified angle. Everything should become clear from the sourcecode Only your imagination is the limit now for your own creations!

//Sourcecode

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

public class Project42 extends Applet implements Runnable
{
    Thread runner;
    Image Buffer;
    Graphics gBuffer;
    //cx, cy = center of rotation
    int angle, cx, cy;
    //variables for rotation
    double radians = 0.0;
    double cos = 1.0;
    double sin = 0.0;

    public void init()
    {
        //create graphics buffer, the size of the applet
        Buffer=createImage(size().width,size().height);
        gBuffer=Buffer.getGraphics();
        cx=size().width/2;
        cy=size().height/2;
    }

    public void start()
    {
        if (runner == null)
        {
            runner = new Thread (this);
            runner.start();
        }
    }

    public void stop()
    {
        if (runner != null)
        {
            runner.stop();
            runner = null;
        }
    }

    public void run()
    {
        while(true)
        {
            try {runner.sleep(25);}
            catch (Exception e) { }

            repaint();
        }
    }

    int rotate_x(int x, int y)
    {
        return ((int) (cx + x * cos - y * sin));
    }

    int rotate_y(int x, int y)
    {
        return ((int) (cy + y * cos + x * sin));
    }

    void SetAngle(double a)
    {
        radians = (a * 2 * Math.PI) / 360;

        cos = Math.cos(radians);
        sin = Math.sin(radians);
    }

    void RotatePolygon(int x[], int y[], int n)
    {
        int new_x[] = new int [n];
        int new_y[] = new int [n];

        for(int i=0; i<n; i++)
        {
            new_x[i]=rotate_x(x[i], y[i]);
            new_y[i]=rotate_y(x[i], y[i]);
        }

        gBuffer.fillPolygon(new_x, new_y, n);
    }

    void drawStuff()
    {
        //paint background black
        gBuffer.setColor(Color.black);
        gBuffer.fillRect(0,0,size().width,size().height);

        int radius;
        int x[];
        int y[];

        //the triangle
        radius=140;
        x= new int[3];
        y= new int[3];

        x[0]=-20;
        x[1]=0;
        x[2]=20;

        y[0]=-radius+80;
        y[1]=-radius;
        y[2]=-radius+80;

        gBuffer.setColor(Color.red);
        RotatePolygon(x, y, 3);

        //the square
        radius=60;
        x= new int[4];
        y= new int[4];

        x[0]=radius;
        x[1]=radius+50;
        x[2]=radius+50;
        x[3]=radius;

        y[0]=-25;
        y[1]=-25;
        y[2]=25;
        y[3]=25;

        gBuffer.setColor(Color.blue);
        RotatePolygon(x, y, 4);

        //the top-down triangle
        radius=60;
        x= new int[3];
        y= new int[3];

        x[0]=-50;
        x[1]=0;
        x[2]=50;

        y[0]=radius+50;
        y[1]=radius;
        y[2]=radius+50;

        gBuffer.setColor(Color.green);
        RotatePolygon(x, y, 3);

        //the hexagon
        radius=120;
        x= new int[6];
        y= new int[6];

        x[0]=-radius;
        x[1]=-radius+30;
        x[2]=-radius+60;
        x[3]=-radius+60;
        x[4]=-radius+30;
        x[5]=-radius;

        y[0]=-20;
        y[1]=-35;
        y[2]=-20;
        y[3]=20;
        y[4]=35;
        y[5]=20;

        gBuffer.setColor(Color.magenta);
        RotatePolygon(x, y, 6);

        //display the angle in degrees
        gBuffer.setColor(Color.yellow);
        gBuffer.setFont(new Font("Helvetica",Font.PLAIN,13));
        gBuffer.drawString("Angle="+angle+"°",10,20);

        //the center mark
        gBuffer.setColor(Color.white);
        gBuffer.drawOval(cx-6,cy-6,12,12);
        gBuffer.drawLine(cx-12,cy,cx+12,cy);
        gBuffer.drawLine(cx,cy-12,cx,cy+12);

        if(angle<360)
            angle++;
        else
            angle=0;

        SetAngle(angle);
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        drawStuff();
        g.drawImage (Buffer,0,0, this);
    }
}

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.