import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; //Only import the outer class. import javax.swing.JComponent; public class EllipseComponent extends JComponent { public void paintComponent(Graphics g) { //RecoverGraphics2D Graphics2D g2 = (Graphics2D) g;
//Ellipse2D.Double is an inner class of Ellipse2D. Ellipse2D.Double ellipse = new Ellipse2D.Double(5, 10, 100 ,30); g2.draw(ellipse);
//Circle: Ellipse2D.Double circle = new Ellipse2D.Double(5, 10, 100 ,100); // g2.draw(circle);}
}
import javax.swing.JFrame; public class EllipseViewer { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setSize(300, 400); frame.setTitle("Ellipse"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); EllipseComponent component = new EllipseComponent(); frame.add(component); frame.setVisible(true); } }
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import javax.swing.JComponent; public class EllipseComponent extends JComponent { public void paintComponent(Graphics g) { //RecoverGraphics2D Graphics2D g2 = (Graphics2D) g; Ellipse2D.Double ellipse = new Ellipse2D.Double(5, 10, 100 ,100); g2.setColor(Color.RED); g2.draw(ellipse); } }
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import javax.swing.JComponent; public class EllipseComponent extends JComponent { public void paintComponent(Graphics g) { //RecoverGraphics2D Graphics2D g2 = (Graphics2D) g; Ellipse2D.Double ellipse = new Ellipse2D.Double(5, 10, 100 ,100); g2.setColor(Color.RED); g2.draw(ellipse); g2.setColor(Color.YELLOW); g2.fill(ellipse); } }