Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Ellipse program that generates an oval shape with red and blue lines
- // Renee Waggoner
- // July 26, 2019
- // Special Requirements: None
- package ellipses;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Graphics;
- public class Ellipse extends JFrame {
- public Ellipse()
- {
- setContentPane(new DrawPane());
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setSize(400, 400);
- setVisible(true);
- }
- class DrawPane extends JPanel {
- public void paintComponent(Graphics g) {
- Dimension d = getSize();
- int w = d.width;
- // determine height of oval, larger numbers would shrink oval vertically
- int h = d.height/2;
- int x = 0, y = 0;
- // creating oval shape
- for (w = d.width; w >= 0; w--)
- {
- if (w%2 == 0)
- {
- g.setColor(Color.blue);
- // fill lines with blue
- g.fillOval(x, y, w, h);
- // this makes the blue portion evenly distributed
- x +=5;
- y +=5;
- w -=10;
- h -=10;
- } else {
- g.setColor(Color.red);
- // fill lines with red
- g.fillOval(x, y, w, h);
- // this makes the red portion evenly distributed inside the created oval shape
- x +=5;
- y +=5;
- w -=10;
- h -=10;
- }
- }
- }
- }
- public static void main(String args[]) {
- new Ellipse();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment