Advertisement
letsdoitjava

Ellipse

Jul 16th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. // Ellipse program that generates an oval shape with red and blue lines
  2. // Renee Waggoner
  3. // July 26, 2019
  4. // Special Requirements: None
  5. package ellipses;
  6. import javax.swing.JFrame;
  7. import javax.swing.JPanel;
  8. import java.awt.Color;
  9. import java.awt.Dimension;
  10. import java.awt.Graphics;
  11.  
  12. public class Ellipse extends JFrame {
  13.  
  14.     public Ellipse()
  15.     {
  16.         setContentPane(new DrawPane());
  17.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18.         setSize(400, 400);
  19.         setVisible(true);
  20.     }
  21.  
  22.     class DrawPane extends JPanel {
  23.         public void paintComponent(Graphics g) {
  24.             Dimension d = getSize();
  25.             int w = d.width;
  26.             // determine height of oval, larger numbers would shrink oval vertically
  27.             int h = d.height/2;
  28.             int x = 0, y = 0;
  29.  
  30.             // creating oval shape
  31.             for (w = d.width; w >= 0; w--)
  32.             {
  33.                 if (w%2 == 0)
  34.                 {
  35.                     g.setColor(Color.blue);
  36.                     // fill lines with blue
  37.                     g.fillOval(x, y, w, h);
  38.                     // this makes the blue portion evenly distributed
  39.                     x +=5;
  40.                     y +=5;
  41.                     w -=10;
  42.                     h -=10;
  43.                 } else {
  44.                     g.setColor(Color.red);
  45.                     // fill lines with red
  46.                     g.fillOval(x, y, w, h);
  47.                     // this makes the red portion evenly distributed inside the created oval shape
  48.                     x +=5;
  49.                     y +=5;
  50.                     w -=10;
  51.                     h -=10;
  52.                 }
  53.             }
  54.         }
  55.     }
  56.  
  57.     public static void main(String args[]) {
  58.         new Ellipse();
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement