Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Wiki
Search Coderanch
Advance search
Google search
Register / Login
Bookmark Topic
Watch Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Wiki
Mice Problem
posted 8 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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] + (int) Math.round(len * Math.cos(currentAngle)); cornersY[i] = cornersY[i-1] + (int) Math.round(len * Math.sin(currentAngle)); currentAngle += diffAngle; } /* for (int i=0; i<n; i++) System.out.printf("%5f %5f\n", cornersX[i], cornersY[i]); */ } // @Override 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(15, 100 / n)); } catch (InterruptedException iex) { return; } } } @Override 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(this, null)); } // 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
Consider Paul's
rocket mass heater
.
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Please help with this program
Bouncing Ball program
Plot Sine Curve
advice on drawing a square wave pulse train
Plot Test
More...