Search code examples
javaapplet

Divide an oval/circle in 4 equal half's and display different colors in it using applet[JAVA]


I'm new to applet programming and i've got this assignment to divide an oval/circle in 4 equal half's and display different colors in it using applet[JAVA].

I've written the code as given below but i am confuse on how to fill each of 4 sections in the circle using different colors.

import java.applet.*;
import java.awt.*;
/*<applet code = "oval.class" width = 300 height = 300> </applet>*/
public class oval extends Applet {
public void init() {
}

public void paint(Graphics g) {
    g.drawOval(100, 100, 200, 200);
    g.setColor(Color.orange);
    g.drawLine(100, 200, 300, 200);
    g.fillColor(Color.blue);
    g.drawLine(200, 100, 200, 300);    
 }}

it colors line only


Solution

  • So i finally figured out, what i was doing wrong, i was supposed to use "arcs" instead of "oval" . Here's the simple code to the question that i asked.

    import java.applet.*;
    import java.awt.*;
    /*
    <applet code = "oval.class" width = 400 height = 400> </applet>
    */
    public class oval extends Applet {
        public void init() {
        }
            public void paint(Graphics g) {
            g.setColor(Color.orange);
            g.fillArc(100,100,200,200,0,90);
            g.setColor(Color.red);
            g.fillArc(100,100,200,200,90,90);
            g.setColor(Color.yellow);
            g.fillArc(100,100,200,200,180,90);
            g.setColor(Color.blue);
            g.fillArc(100,100,200,200,270,90);
        }
    }
    

    enter image description here