JavaRanch Home
 
Front Page FAQs Ranchers Categories Recent Changes To Do Upload

Mice Problem   



This code sample draws pictures that illustrate the Mice Problem. It was discussed in this post.


import java.awt.*;
import java.util.concurrent.*;
import javax.swing.*;

public class Polygon extends JPanel implements Runnable
{
    // length in pixels of each side
    final int len = 100;
    final int[] cornersX, cornersY;

    // the number of sides of the polygon
    final int n;

    // for creating and stopping the drawing thread
    Future ft = null;
    ExecutorService exec = Executors.newSingleThreadExecutor();

    public Polygon (int n) {
        this.n = n;

        cornersX = new int[n];
        cornersY = new int[n];
        cornersX[0] = n * 15;
        cornersY[0] = 30;

        double diffAngle = 2.0 * Math.PI / n;
        double currentAngle = 0;

        for (int i=1; i<n; i++) {
            cornersX[i] = cornersX[i-1] + (intMath.round(len * Math.cos(currentAngle));
            cornersY[i] = cornersY[i-1] + (intMath.round(len * Math.sin(currentAngle));
            currentAngle += diffAngle;
        }
/*
        for (int i=0; i<n; i++)
            System.out.printf("%5f   %5f\n", cornersX[i], cornersY[i]);
*/
    }

    public void run() {
        double[] posX = new double[n], posY = new double[n];
        for (int i=0; i<n; i++) {
            posX[i] = cornersX[i];
            posY[i] = cornersY[i];
        }

        Graphics g = getGraphics();

        // terminate thread if two consecutive points are identical
        while (Math.abs(posX[0] - posX[1]) > 1
                || Math.abs(posY[0] - posY[1]) > 1) {
            for (int i=0; i<n; i++) {
                int nextI = (i == n-1) ? 0 : i+1;
                posX[nextI] = posX[nextI] - (posX[nextI] - posX[i]) / 50.0;
                posY[nextI] = posY[nextI] - (posY[nextI] - posY[i]) / 50.0;
                g.drawLine((int) posX[i], (int) posY[i], (int) posX[i], (int) posY[i]);
            }

            // slow down drawing by sleeping a bit
            try {
                Thread.sleep(Math.min(15100 / n));
            } catch (InterruptedException iex) {
                return;
            }
        }
    }

    protected void paintComponent (Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        for (int j=0; j<n; j++) {
            int nextJ = (j == n-1) ? 0 : j+1;
            g2.drawLine(cornersX[j], cornersY[j], cornersX[nextJ], cornersY[nextJ]);
        }

        // terminate currently active drawing thread, and create a new one
        if (ft != null)
            ft.cancel(true);
        ft = exec.submit(new FutureTask(thisnull));
    }

    // needs the number of sides as parameter
    public static void main (String[] args) {
        if (args.length == 0) {
            System.out.println("Please enter the number of polygon sides as parameter.");
            System.exit(1);
        }

        int n = Integer.parseInt(args[0]);
        if (n < 3) {
            System.out.println("The polygon must have at least 3 sides.");
            System.exit(1);
        }

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new Polygon(n));
        f.setSize(n*50,n*50);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}


CategoryCodeSamples CodeBarn
Front Page FAQs Ranchers Categories Recent Changes To Do Upload
Last Edited: 16 April 2008 What's Changed?
 
Copyright © 1998-2008 Paul Wheaton | Home | Contact Us | Privacy | Register