Java Language 2D Graphics in Java Example 2: Drawing and Filling Oval

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

import javax.swing.*;    
import java.awt.*;    

public class MyPanel extends JPanel {
    @Override
    public void paintComponent(Graphics g){    
        // clear the previous painting
        super.paintComponent(g);    
        Graphics2D g2 = (Graphics2D)g;    
        g2.setColor(Color.blue);    
        g2.drawOval(0, 0, 20,20);  
        g2.fillOval(50,50,20,20);    
    }
}

g2.drawOval(int x,int y,int height, int width);
This method will draw an oval at specified x and y position with given height and width.

g2.fillOval(int x,int y,int height, int width); This method will fill an oval at specified x and y position with given height and width.



Got any Java Language Question?