turt2live

Untitled

Apr 8th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.84 KB | None | 0 0
  1. // Imports - So we can do stuff
  2. import java.awt.Color;
  3. import java.awt.Dimension;
  4. import java.awt.Graphics;
  5. import java.awt.Point;
  6.  
  7. import javax.swing.JFrame;
  8. import javax.swing.JPanel;
  9.  
  10. // Demonstration Class: CIRCLES
  11. /*
  12.  * Creates something that looks like this: http://www.local.t2ldev.com/circle.png
  13.  */
  14. public class Test extends JFrame {
  15.  
  16.     // Called by the JVM
  17.     public static void main(String args[]){
  18.         new Test("test"); // Create a new Demonstration
  19.     }
  20.  
  21.     // Creates a new demonstration
  22.     public Test(String s){
  23.         setTitle(s); // The title
  24.         setSize(new Dimension(500, 500)); // The size
  25.         add(new Panel()); // Add a Panel Object (demonstration pane)
  26.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Actually kills the program when I hit "X"
  27.         setVisible(true); // Display the demonstration
  28.     }
  29.  
  30.     // Panel Object
  31.     private class Panel extends JPanel {
  32.         @Override
  33.         // Paint Method
  34.         public void paint(Graphics g){
  35.             /*
  36.             fillRect() is the lazy way to draw a point.
  37.             */
  38.  
  39.             // Setup vars
  40.             Point center = new Point(250, 250); // Our center point
  41.             double radius = 100; // The radius
  42.             g.setColor(Color.BLUE); // For the center point
  43.             g.fillRect(center.x - 3, center.y - 3, 6, 6); // I minus 3 because I want the square to be 6 wide, but still in the correct location on screen
  44.             g.setColor(Color.CYAN); // For the points
  45.             for(int degree = 0; degree < 361; degree++){ // Loop through all possible degrees
  46.                 double x = center.x + (radius * Math.cos(degree)); // Get x. Formula: X = Cx + RCos(A)
  47.                 double y = center.y + (radius * Math.sin(degree)); // Get y. Formula: Y = Cy + RSin(A)
  48.                 g.fillRect((int) x - 3, (int) y - 3, 6, 6); // Draw the point on screen, again with the -3 stuff
  49.                 g.drawLine((int) x, (int) y, center.x - 3, center.y - 3); // Draw a fancy line from the point to the center.
  50.             }
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment