Controlling transparancy when two Swing shapes overlap

Use the class AlphaComposite as shown in following example.

Main.java:

import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.text.*;
import java.awt.*;
 
public class Main extends JFrame {
   public Main() {
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent we) {
            System.exit(1);
         }
      });
   }
 
   public void paint(Graphics g) {
      Graphics2D g2d = (Graphics2D) g;
 
      g2d.setColor(Color.blue);
      g2d.fill(new Ellipse2D.Float(50, 30, 80, 50));
      g2d.setColor(Color.red);
      g2d.fill(new Ellipse2D.Float(100, 30, 80, 50));
 
      g2d.setColor(Color.black);
      g2d.drawLine(0, 90, 250, 90);
      
      DecimalFormat decimalFormat = (DecimalFormat)NumberFormat.getInstance();
      decimalFormat.applyPattern("0.0");
 
      int yd = 80;
      for (float f = 1f; f>=0f; f-=.2f) {
         g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
         g2d.setColor(Color.black);
         g2d.drawString("AlphaComposite = " + decimalFormat.format(f), 50, 50 + yd - 10);
         g2d.setColor(Color.blue);
         g2d.fill(new Ellipse2D.Float(50, 50 + yd, 80, 50));
 
         AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, f);
         g2d.setComposite(ac);
 
         g2d.setColor(Color.red);
         g2d.fill(new Ellipse2D.Float(100, 50 + yd, 80, 50));
 
         yd += 80;
      }
   }
  
   public static void main(String []args) {
      Main main = new Main();
      main.setSize(250, 600);
      main.setVisible(true);
   }
}