Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Imports - So we can do stuff
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Point;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- // Demonstration Class: CIRCLES
- /*
- * Creates something that looks like this: http://www.local.t2ldev.com/circle.png
- */
- public class Test extends JFrame {
- // Called by the JVM
- public static void main(String args[]){
- new Test("test"); // Create a new Demonstration
- }
- // Creates a new demonstration
- public Test(String s){
- setTitle(s); // The title
- setSize(new Dimension(500, 500)); // The size
- add(new Panel()); // Add a Panel Object (demonstration pane)
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Actually kills the program when I hit "X"
- setVisible(true); // Display the demonstration
- }
- // Panel Object
- private class Panel extends JPanel {
- @Override
- // Paint Method
- public void paint(Graphics g){
- /*
- fillRect() is the lazy way to draw a point.
- */
- // Setup vars
- Point center = new Point(250, 250); // Our center point
- double radius = 100; // The radius
- g.setColor(Color.BLUE); // For the center point
- 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
- g.setColor(Color.CYAN); // For the points
- for(int degree = 0; degree < 361; degree++){ // Loop through all possible degrees
- double x = center.x + (radius * Math.cos(degree)); // Get x. Formula: X = Cx + RCos(A)
- double y = center.y + (radius * Math.sin(degree)); // Get y. Formula: Y = Cy + RSin(A)
- g.fillRect((int) x - 3, (int) y - 3, 6, 6); // Draw the point on screen, again with the -3 stuff
- g.drawLine((int) x, (int) y, center.x - 3, center.y - 3); // Draw a fancy line from the point to the center.
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment