horselurrver

Circle

Nov 26th, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import javax.swing.*;
  4. import java.lang.reflect.*;
  5.  
  6. public class Circle extends JFrame {
  7. public Circle(){
  8. super("Circle");
  9. setSize(360, 540);
  10. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11. CirclePane pane = new CirclePane(90, 90, 100, "blue");
  12. add(pane);
  13. setVisible(true);
  14. }
  15.  
  16. public static void main(String[] arguments){
  17. Circle cir = new Circle();
  18. }
  19. }
  20.  
  21. class CirclePane extends JPanel {
  22. int radius, x, y;
  23. String color;
  24. Color actualColor;
  25. public CirclePane(int radius, int x, int y, String color){
  26. this.radius = radius;
  27. this.x = x;
  28. this.y = y;
  29. this.color = color;
  30. }
  31. public void paintComponent(Graphics comp){
  32. Graphics2D comp2D = (Graphics2D)comp;
  33. try {
  34. //actualColor = Color.decode(color);
  35. Field field = Class.forName("java.awt.Color").getField(color.toLowerCase()); // toLowerCase because the color fields are RED or red, not Red
  36. actualColor = (Color)field.get(null);
  37. comp2D.setColor(actualColor);
  38. Arc2D.Float circle = new Arc2D.Float(x, y, radius*2, radius*2, 0, 360, Arc2D.OPEN);
  39. comp2D.fill(circle);
  40. } catch (NumberFormatException|IllegalAccessException|ClassNotFoundException|NoSuchFieldException err){
  41. System.out.println("Error: " + err.getMessage());
  42. }
  43.  
  44. }
  45. }
Add Comment
Please, Sign In to add comment